| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace addons\YunfastPay\common\services;
- use addons\YunfastPay\common\enums\YunfastPayTypeEnum;
- use addons\YunfastPay\common\models\PaymentOrder;
- use addons\YunfastPay\common\services\pays\cashier\ScanByAliPay;
- use addons\YunfastPay\common\services\pays\cashier\ScanByMiniProgramPay;
- use addons\YunfastPay\common\services\pays\cashier\ScanByWechatPay;
- use addons\YunfastPay\common\services\pays\order\AppPay;
- use addons\YunfastPay\common\services\pays\order\H5Pay;
- use addons\YunfastPay\common\services\pays\order\MiniProgramPay;
- use addons\YunfastPay\common\services\pays\order\WechatPay;
- use addons\YunfastPay\common\services\pays\PayInterface;
- use addons\YunfastPay\common\services\trans\TransAbstract;
- use common\models\common\PaymentTrade;
- use Yii;
- /**
- * 支付
- * @package addons\YunfastPay\common\services
- */
- class PayService extends BaseService
- {
- /**
- * @var TransAbstract
- */
- protected $trans;
- /**
- * @var string
- */
- protected $tradeType;
- /**
- * @var PaymentTrade
- */
- protected $trade;
- /**
- * @var YunPayService
- */
- protected $yunPay;
- /**
- * @var array
- */
- protected $result;
- /**
- * @var string
- */
- protected $openid;
- /**
- * @var array
- */
- protected $orders;
- /**
- * @var PayInterface
- */
- protected $pay;
- /**
- * @var integer
- */
- protected $storeId = 0;
- /**
- * @var array
- */
- protected $map = [
- YunfastPayTypeEnum::ALIPAY_APP => [
- 'class' => AppPay::class,
- ],
- YunfastPayTypeEnum::WEIXIN_H5 => [
- 'class' => H5Pay::class,
- ],
- YunfastPayTypeEnum::WEIXIN_GZH => [
- 'class' => WechatPay::class,
- ],
- YunfastPayTypeEnum::WEIXIN_XCX => [
- 'class' => MiniProgramPay::class,
- ],
- YunfastPayTypeEnum::ALIPAY_SCAN => [
- 'class' => ScanByAliPay::class,
- ],
- YunfastPayTypeEnum::WEIXIN_SCAN => [
- 'class' => ScanByWechatPay::class,
- ],
- YunfastPayTypeEnum::W_MINI_PROGRAM_SCAN => [
- 'class' => ScanByMiniProgramPay::class,
- ],
- ];
- protected function setParams(array $params)
- {
- $this->tradeType = $params['trade_type'];
- $this->trade = $params['trade'];
- }
- /**
- * 订单支付
- *
- * @param array $params
- * @return void
- */
- public function orderPay(
- PaymentTrade $trade,
- string $trade_type,
- string $openid
- )
- {
- $this->trade = $trade;
- $this->tradeType = $trade_type;
- $this->openid = $openid;
- $this->yunPay = new YunPayService(['mall_id' => $this->mall_id]);
- // $method = $this->getMethodByTradeType();
- // if (is_null($method)) throw new \Exception('未找到对应支付方式');
- $this->setTrans();
- // $this->setSplitBill();
- $this->sendPayRq();
- $this->addPayLog();
- return $this->getResult();
- }
- /**
- * 获取返回数据
- *
- * @return array
- */
- public function getResult()
- {
- return $this->result;
- }
- /**
- * @return PaymentTrade
- */
- public function getTrade()
- {
- return $this->trade;
- }
- /**
- * @return YunPayService
- */
- public function getYunPay()
- {
- return $this->yunPay;
- }
- /**
- * @return string
- */
- public function getOpenid()
- {
- return $this->openid;
- }
- /**
- * @return TransAbstract|null
- */
- protected function getClassByTradeType()
- {
- return !empty($this->map[$this->tradeType])
- ? $this->map[$this->tradeType]['class']
- : null;
- }
- /**
- * @return string|null
- */
- protected function getMethodByTradeType()
- {
- return !empty($this->map[$this->tradeType])
- ? $this->map[$this->tradeType]['method']
- : null;
- }
- /**
- * 设置支付trans
- *
- * @return void
- */
- protected function setTrans()
- {
- /**
- * @var PayInterface
- */
- $class = $this->getClassByTradeType();
- if (is_null($class)) throw new \Exception('未找到对应支付方式');
- /**
- * @var PayInterface
- */
- $this->pay = new $class($this);
- $this->trans = $this->pay->getTrans();
- }
- /**
- * 设置门店ID
- *
- * @return void
- */
- public function setStoreId(int $storeId)
- {
- $this->storeId = $storeId;
- }
- /**
- * 发送请求
- *
- * @return void
- */
- protected function sendPayRq()
- {
- $this->yunPay->setStoreId($this->storeId);
- $result = $this->yunPay->sendPayRq($this->trans);
- $this->result = $result;
- }
- /**
- * 设置分账
- *
- * @return void
- */
- protected function setSplitBill()
- {
- // 获取订单
- // $this->orders = PaymentTradeOrder::getStoreInfoByTradeId($this->trade->id);
- // 设置分账
- // $this->yunPay->setSplitBill($this->orders, $this->trans);
- }
- /**
- * 添加支付日志
- *
- * @return void
- */
- protected function addPayLog()
- {
- // 支付请求数据
- $reqData = $this->yunPay->getReqData();
- PaymentOrder::addLog([
- 'mall_id' => $this->mall_id,
- 'user_id' => $this->trade->user_id,
- 'trade_id' => $this->trade->id,
- 'amount' => $this->trade->pay_fee,
- 'order_no' => $reqData['outOrderId'],
- 'req_data' => $reqData,
- 'res_data' => $this->result,
- 'status' => !empty($this->result['transStatus']) ? $this->result['transStatus'] : '0',
- ]);
- }
- /**
- * 获取
- *
- * @return array
- */
- public function getPayData()
- {
- return $this->pay->getPayData(
- $this->getResult()
- );
- }
- /**
- * 支付回调处理
- *
- * @param array $respData
- * @return boolean
- */
- public function notify(array $respData)
- {
- /**
- * @var PaymentOrder
- */
- $order = PaymentOrder::getOrderByNo($this->mall_id, $respData['outOrderId']);
- if ($order->status == '100') return true;
- if ('100' == $respData['transStatus']) {
- // 对比金额
- if (bccomp($order->trade->pay_fee, $respData['transAmt'], 2) == 0) {
- $order->success($respData['orderCd']);
- return true;
- }
- } else if ('102' == $respData['transStatus']) {
- // 交易失败
- $order->fail();
- return $this->error('交易失败');
- } else {
- // 未知结果,这种情况不会有。
- return $this->error('未知情况');
- }
- }
- }
|