[ '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('未知情况'); } } }