优化小程序支付

main
fengxinyhyl 10 months ago
parent 77080f71b0
commit d147648efc
  1. 2
      app/common/repositories/store/order/StoreOrderRepository.php
  2. 21
      crmeb/services/MiniProgramService.php
  3. 6
      crmeb/services/PayService.php

@ -112,7 +112,9 @@ class StoreOrderRepository extends BaseRepository
event('order.pay.before', compact('groupOrder', 'type', 'isApp')); event('order.pay.before', compact('groupOrder', 'type', 'isApp'));
if (in_array($type, ['weixin', 'weixinApp', 'routine', 'h5', 'weixinQr'], true) && systemConfig('open_wx_combine')) { if (in_array($type, ['weixin', 'weixinApp', 'routine', 'h5', 'weixinQr'], true) && systemConfig('open_wx_combine')) {
$service = new CombinePayService($type, $groupOrder->getCombinePayParams()); $service = new CombinePayService($type, $groupOrder->getCombinePayParams());
Log::info("CombinePayService");
} else { } else {
Log::info("PayService and {$type}");
$service = new PayService($type, $groupOrder->getPayParams($type === 'alipay' ? $return_url : '')); $service = new PayService($type, $groupOrder->getPayParams($type === 'alipay' ? $return_url : ''));
} }
$config = $service->pay($user); $config = $service->pay($user);

@ -256,6 +256,7 @@ class MiniProgramService
public function paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = []) public function paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
{ {
$order = $this->paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options); $order = $this->paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
Log::info("MiniProgramService paymentPrepare order" );
// 获取配置 判断是否为新支付 // 获取配置 判断是否为新支付
if (isset($this->config['pay_routine_new_mchid']) && $this->config['pay_routine_new_mchid']) { if (isset($this->config['pay_routine_new_mchid']) && $this->config['pay_routine_new_mchid']) {
$result = $this->service->minipay->createorder($order); $result = $this->service->minipay->createorder($order);
@ -265,12 +266,17 @@ class MiniProgramService
throw new ValidateException('微信支付错误返回:' . $result->return_msg); throw new ValidateException('微信支付错误返回:' . $result->return_msg);
} }
} else { } else {
if ($this->v3Pay()) { Log::info("MiniProgramService paymentPrepare order2".json_encode($this->v3Pay()) );
if ($trade_type == 'MWEB') $trade_type = 'H5';
$payFunction = 'pay'.ucfirst($trade_type); // if ($this->v3Pay()) {
return $this->service->v3Pay->{$payFunction}($order); // Log::info("MiniProgramService paymentPrepare order4" );
} else { // if ($trade_type == 'MWEB') $trade_type = 'H5';
// $payFunction = 'pay'.ucfirst($trade_type);
// return $this->service->v3Pay->{$payFunction}($order);
// } else {
Log::info("MiniProgramService paymentPrepare order3" );
$result = $this->paymentService()->prepare($order); $result = $this->paymentService()->prepare($order);
Log::info("MiniProgramService paymentPrepare ".json_encode($result) );
if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') { if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
return $result->prepay_id; return $result->prepay_id;
} else { } else {
@ -282,7 +288,7 @@ class MiniProgramService
throw new ValidateException('没有获取微信支付的预支付ID,请重新发起支付!'); throw new ValidateException('没有获取微信支付的预支付ID,请重新发起支付!');
} }
} }
} // }
} }
} }
@ -300,7 +306,10 @@ class MiniProgramService
*/ */
public function jsPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = []) public function jsPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
{ {
Log::info('小程序支付参数版本V3');
$paymentPrepare = $this->paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options); $paymentPrepare = $this->paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
Log::info('小程序支付参数' . json_encode($paymentPrepare));
if ($this->isV3()) { if ($this->isV3()) {
return $paymentPrepare; return $paymentPrepare;
} }

@ -17,6 +17,7 @@ use app\common\model\user\User;
use app\common\repositories\wechat\WechatUserRepository; use app\common\repositories\wechat\WechatUserRepository;
use think\exception\ValidateException; use think\exception\ValidateException;
use think\facade\Cache; use think\facade\Cache;
use think\facade\Log;
class PayService class PayService
{ {
@ -60,9 +61,14 @@ class PayService
{ {
$wechatUserRepository = app()->make(WechatUserRepository::class); $wechatUserRepository = app()->make(WechatUserRepository::class);
$openId = $wechatUserRepository->idByRoutineId($user['wechat_user_id']); $openId = $wechatUserRepository->idByRoutineId($user['wechat_user_id']);
Log::info("payRoutine openId: " . $openId);
if (!$openId) if (!$openId)
throw new ValidateException('请关联微信小程序!'); throw new ValidateException('请关联微信小程序!');
try {
$config = MiniProgramService::create()->jsPay($openId, $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body']); $config = MiniProgramService::create()->jsPay($openId, $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body']);
} catch (\Exception $e) {
Log::info("payRoutine error: " . $e->getMessage());
}
return compact('config'); return compact('config');
} }

Loading…
Cancel
Save