查看: 220|回复: 0

[fastadmin] fastadmin支付插件中使用v3支付的方法

[复制链接]

748

主题

39

回帖

3907

积分

管理员

总舵主

积分
3907
QQ
发表于 2026-5-21 14:31:51 | 显示全部楼层 |阅读模式
当前微信支付宝整合插件中微信支付v3配置仍需要配置v2证书,否则v3支付失败,这可能是个bug,等官方后续更新。

v3微信支付需要配置的参数包括:

我们在需要调用V3支付的地方执行:
  1. set_addon_config('epay', ['version'=>'v3'], false);
复制代码
将支付版本改为使用V3。
1、需要使用到的支付类:
  1. use addons\epay\library\Service;
  2. use Yansongda\Pay\Exceptions\BusinessException;
  3. use Yansongda\Pay\Exceptions\GatewayException;
  4. use Yansongda\Pay\Exceptions\InvalidSignException;
  5. use addons\epay\library\Collection;
  6. use GuzzleHttp\Psr7\Response;
复制代码
2、构造支付参数发起支付:
  1. // 支付参数
  2.                 $pay_params = [
  3.                     'amount'    => $order->payfee,
  4.                     'orderid'   => $order->ordersn,
  5.                     'type'      => 'wechat',
  6.                     'title'     => '商城购买商品消费',
  7.                     'returnurl' => null,
  8.                     'method'    => 'miniapp',
  9.                     'openid'    => $third->openid
  10.                 ];
  11.                 // 回调通知地址
  12.                 $pay_params['notifyurl'] = $this->request->root(true) . "/addons/eshop/api.goods/notify";
  13.                 \think\Log::record($pay_params);
  14.                 // 调用支付
  15.                 try {
  16.                     $response = Service::submitOrder($pay_params);
  17.                     \think\Log::record($response);
  18.                     $this->success('请求成功', $response);
  19.                 } catch (BusinessException|GatewayException|InvalidSignException|Collection $e) {
  20.                     \think\Log::record($e);
  21.                     $this->error($e->getMessage());
  22.                 }
复制代码
3、支付回调通知:
  1. public function notify()
  2.     {
  3.         $params = $this->request->param();
  4.         \think\Log::record($params);
  5.         $pay = Service::checkNotify('wechat');
  6.         if (!$pay) {
  7.             echo '签名错误';
  8.             return;
  9.         }
  10.         \think\Log::record(Service::isVersionV3());
  11.         $data = Service::isVersionV3() ? $pay->callback() : $pay->verify();
  12.         \think\Log::record($data);
  13.         $notifydata = json_decode($data, true);
  14.         try {
  15.             // 订单表单
  16.             $orderdata = [
  17.                 'paytype' => 2,
  18.                 'paytime' => time(),
  19.                 'status' => 1
  20.             ];
  21.             if (Service::isVersionV3()) {
  22.                 $out_trade_no = $notifydata['resource']['ciphertext']['out_trade_no'];
  23.                 $orderdata['transid'] = $notifydata['resource']['ciphertext']['transaction_id'];
  24.                 $orderdata['cash'] = $notifydata['resource']['ciphertext']['amount']['payer_total'] / 100;
  25.                 $orderdata['realpayfee'] = $notifydata['resource']['ciphertext']['amount']['payer_total'] / 100;
  26.             } else {
  27.                 $out_trade_no = $data->out_trade_no;
  28.                 $orderdata['cash'] = $data->total_fee / 100;
  29.                 $orderdata['transid'] = $data->transaction_id;
  30.                 $orderdata['realpayfee'] = $data->total_fee / 100;
  31.             }
  32.             // 查询订单
  33.             $order = Order::where(['ordersn'=>$out_trade_no])->find();
  34.             // 待付款的才可以执行
  35.             if ($order->status == 0) {
  36.                 // 更新订单
  37.                 $result = Order::update($orderdata, ['id'=>$order->id]);
  38.                 if ($result) {
  39.                     // 处理订单商品状态
  40.                     $ordergoods = Ordergoods::where(['order_id'=>$order->id])->find();
  41.                     if ($ordergoods) {
  42.                         $ordergoods->allowField(true)->save(['status'=>1]);
  43.                     }
  44.                     // 积分兑换处理积分
  45.                     if ($order->paystyle == 1) {
  46.                         User::score(-$order->score, $order->user_id, '商城购买商品消费');
  47.                     }
  48.                     // 商品/规格信息
  49.                     if ($ordergoods->option_id) {
  50.                         // 规格
  51.                         $option = Option::get($ordergoods->option_id);
  52.                         if (!empty($option)) {
  53.                             // 更新库存
  54.                             $stock = $option->stock - $ordergoods->total;
  55.                             $option->allowField(true)->save(['stock'=>$stock]);
  56.                         }
  57.                     } else {
  58.                         // 商品
  59.                         $goods = Goods::get($ordergoods->goods_id);
  60.                         if (!empty($goods)) {
  61.                             // 更新库存
  62.                             $stock = $goods->stock - $ordergoods-total;
  63.                             $goods->allowField(true)->save(['stock'=>$stock]);
  64.                         }
  65.                     }
  66.                 }
  67.             }
  68.         } catch (Exception $e) {
  69.             $this->error($e->getMessage());
  70.         }
  71.         //下面这句必须要执行,且在此之前不能有任何输出
  72.         if (Service::isVersionV3()) {
  73.             return $pay->success()->getBody()->getContents();
  74.         } else {
  75.             return $pay->success()->send();
  76.         }
  77.     }
复制代码
4、退款:
  1. use Yansongda\Pay\Pay;
  2. use addons\epay\library\Service;
  3. use Yansongda\Pay\Exceptions\BusinessException;
  4. use fast\Random;
复制代码
  1. public function refund($ids = null)
  2.     {
  3.         $row = $this->model->get($ids);
  4.         if (!$row) {
  5.             $this->error(__('No Results were found'));
  6.         }
  7.         $adminIds = $this->getDataLimitAdminIds();
  8.         if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  9.             $this->error(__('You have no permission'));
  10.         }
  11.         if (false === $this->request->isPost()) {
  12.             $this->view->assign('row', $row);
  13.             return $this->view->fetch();
  14.         }
  15.         $params = $this->request->post('row/a');
  16.         if (empty($params)) {
  17.             $this->error(__('Parameter %s can not be empty', ''));
  18.         }
  19.         $params = $this->preExcludeFields($params);
  20.         // \think\Log::record($params);
  21.         if ($row->status < 1 || $row->status > 3) {
  22.             $this->error('当前订单状态不允许退款');
  23.         }
  24.         // 判断退款金额
  25.         if (bccomp($params['refundfee'], $row->realpayfee, 2) > 0) {
  26.             // \think\Log::record('退款金额不得高于支付金额');
  27.             $this->error('退款金额不得高于支付金额');
  28.         }
  29.         if (bccomp($params['refundfee'], '0.00', 2) <= 0) {
  30.             // \think\Log::record('退款金额不合法');
  31.             $this->error('退款金额不合法');
  32.         }
  33.         // 积分+现金支付
  34.         if ($row->paystyle == 1) {
  35.             if (bccomp($params['refundscore'], $row->score, 0) > 0) {
  36.                 // \think\Log::record('退款金额不得高于支付金额');
  37.                 $this->error('退款积分不得高于支付积分');
  38.             }
  39.             if (bccomp($params['refundscore'], '0', 0) <= 0) {
  40.                 // \think\Log::record('退款金额不合法');
  41.                 $this->error('退款积分不合法');
  42.             }
  43.         }
  44.         
  45.         // 退款单号
  46.         $params['refundsn'] = 'T'.date('YmdHis').Random::numeric();
  47.         // 退款时间
  48.         $params['refundtime'] = time();
  49.         // 退款状态
  50.         $params['status'] = 5;
  51.         // 支付方式
  52.         if ($row->paytype == 1) {
  53.             // \think\Log::record($params);
  54.             // 余额支付
  55.             User::money($params['refundfee'], $row->user_id, '商城购物订单退款');
  56.             // 退回积分
  57.             if ($params['refundscore'] > 0) {
  58.                 User::score($params['refundscore'], $row->user_id, '商城购物订单退款');
  59.             }
  60.             // 更新订单状态
  61.             $result = $row->allowField(true)->save($params);
  62.             // \think\Log::record($result);
  63.             if ($result) {
  64.                 $this->success('退款成功');
  65.             }
  66.             $this->error('退款失败,请核实用户余额和积分记录');
  67.         } elseif ($row->paytype == 2) {
  68.             $result = false;
  69.             Db::startTrans();
  70.             // 退款参数
  71.             $order = [
  72.                 '_action' => 'mini',
  73.                 'out_trade_no' => $row->ordersn,
  74.                 'out_refund_no' => $params['refundsn'],
  75.                 'amount' => [
  76.                     'refund' => $params['refundfee'] * 100,
  77.                     'total' => $row->realpayfee * 100,
  78.                     'currency' => 'CNY',
  79.                 ],
  80.                 'reason' => $params['reason']
  81.             ];
  82.             $config = Service::getConfig('wechat');
  83.             // \think\Log::record($config);
  84.             $result = false;
  85.             $erromsg = '退款成功';
  86.             // 微信支付
  87.             try {
  88.                 // 调用支付接口
  89.                 $ret = Pay::wechat($config)->refund($order);
  90.                 \think\Log::record($ret);
  91.                 $response = json_decode($ret, true);
  92.                 \think\Log::record($response);
  93.                 if ($response['status'] == 'SUCCESS' || $response['status'] == 'PROCESSING') {
  94.                     // 支付平台退款单号
  95.                     $params['refund_id'] = $response['refund_id'];
  96.                     $result = $row->allowField(true)->save($params);
  97.                     if ($result) {
  98.                         // 退回积分
  99.                         if ($params['refundscore'] > 0) {
  100.                             User::score($params['refundscore'], $row->user_id, '商城购物订单退款');
  101.                         }
  102.                         Db::commit();
  103.                     } else {
  104.                         $erromsg = '退款成功,但订单状态更新失败,请核实用户积分记录!';
  105.                     }
  106.                 } else {
  107.                     $erromsg = '退款失败,请稍后重试!';
  108.                 }
  109.             } catch (Exception $e) {
  110.                 Db::rollback();
  111.                 $this->error($e->getMessage());
  112.             }
  113.             if (false === $result) {
  114.                 $this->error($erromsg);
  115.             }
  116.             $this->success($erromsg);
  117.         }
  118.     }
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
道不同,不相为谋!理想要有,万一实现了呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:2513533699@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鲁ICP备19000917号|鲁公网安备37078302000320号

在本版发帖
关注公众号
QQ客服返回顶部