diff --git a/app/api/controller/order/RetailOrderController.php b/app/api/controller/order/RetailOrderController.php index 13f4736..36c48ef 100644 --- a/app/api/controller/order/RetailOrderController.php +++ b/app/api/controller/order/RetailOrderController.php @@ -7,11 +7,14 @@ use app\admin\logic\retail\CashierclassLogic; use app\api\logic\order\OrderLogic; use app\api\controller\BaseApiController; use app\api\lists\order\RetailOrderList; +use app\api\service\WechatUserService; use app\common\logic\order\RetailOrderLogic; use app\common\enum\PayEnum; +use app\common\logic\PaymentLogic; use app\common\model\order\Cart; use app\common\model\retail\Cashierclass; use app\common\model\user\User; +use app\common\service\wechat\WeChatConfigService; class RetailOrderController extends BaseApiController { @@ -59,7 +62,6 @@ class RetailOrderController extends BaseApiController * @notes 创建零售订单 */ public function createOrder(){ - $user=User::where('id',$this->request->userId)->find(); $cartId = (Array)$this->request->post('cart_id', []); $mer_id = (Array)$this->request->post('mer_id', 0); @@ -87,6 +89,13 @@ class RetailOrderController extends BaseApiController }else{ return $this->fail(RetailOrderLogic::getError()); } + }elseif($pay_type==PayEnum::WECHAT_PAY){ + $redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; + $result = PaymentLogic::pay($pay_type,'', $order, $this->userInfo['terminal'], $redirectUrl); + if (false === $result) { + return $this->fail(PaymentLogic::getError(), $params); + } + return $this->success('', $result); } return $this->data(['order_id'=>$order->id]); }else{ diff --git a/app/common/logic/PaymentLogic.php b/app/common/logic/PaymentLogic.php new file mode 100644 index 0000000..5efa536 --- /dev/null +++ b/app/common/logic/PaymentLogic.php @@ -0,0 +1,92 @@ + PayEnum::BALANCE_PAY]; + } + switch ($payWay) { + case PayEnum::WECHAT_PAY: + $payService = (new WeChatPayService($terminal, $order['uid'] ?? null)); + $order['pay_sn'] = $paySn; + $order['redirect_url'] = $redirectUrl; + $result = $payService->pay($from, $order); + break; + default: + self::$error = '订单异常'; + $result = false; + } + + if (false === $result && !self::hasError()) { + d($payService->getError()); + self::setError($payService->getError()); + } + return $result; + } + + + + + /** + * @notes 设置订单号 支付回调时截取前面的单号 18个 + * @param $orderSn + * @param $terminal + * @return string + * @author 段誉 + * @date 2023/3/1 16:31 + * @remark 回调时使用了不同的回调地址,导致跨客户端支付时(例如小程序,公众号)可能出现201,商户订单号重复错误 + */ + public static function formatOrderSn($orderSn, $terminal) + { + $suffix = mb_substr(time(), -4); + return $orderSn . $terminal . $suffix; + } + +} \ No newline at end of file diff --git a/app/common/service/pay/WeChatPayService.php b/app/common/service/pay/WeChatPayService.php index 33c124b..799d40c 100644 --- a/app/common/service/pay/WeChatPayService.php +++ b/app/common/service/pay/WeChatPayService.php @@ -91,6 +91,7 @@ class WeChatPayService extends BasePayService 'pay_way' => PayEnum::WECHAT_PAY ]; } catch (\Exception $e) { + d($e); $this->setError($e->getMessage()); return false; } @@ -117,7 +118,7 @@ class WeChatPayService extends BasePayService "out_trade_no" => $order['pay_sn'], "notify_url" => $this->config['notify_url'], "amount" => [ - "total" => intval($order['order_amount'] * 100), + "total" => intval($order['actual'] * 100), ], "payer" => [ "openid" => $this->auth['openid'] @@ -186,7 +187,7 @@ class WeChatPayService extends BasePayService 'order' => '商品', 'recharge' => '充值', ]; - return $desc[$from] ?? '商品'; + return '商品';//$desc[$from] ?? '商品'; } diff --git a/app/common/service/wechat/WeChatConfigService.php b/app/common/service/wechat/WeChatConfigService.php index bc40813..7b80ba6 100644 --- a/app/common/service/wechat/WeChatConfigService.php +++ b/app/common/service/wechat/WeChatConfigService.php @@ -85,8 +85,7 @@ class WeChatConfigService */ public static function getPayConfigByTerminal($terminal) { - $notifyUrl ='//'.request()->host().'/pay/notifyMnp'; - + $notifyUrl ='https://'.request()->host().'/pay/notifyMnp'; $pay = PayConfig::where(['pay_way' => PayEnum::WECHAT_PAY])->findOrEmpty()->toArray(); //判断是否已经存在证书文件夹,不存在则新建