BalancePaymentService.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\data\service\payment;
  3. use app\data\model\DataUserBalance;
  4. use app\data\model\ShopOrder;
  5. use app\data\service\PaymentService;
  6. use app\data\service\UserBalanceService;
  7. use think\admin\Exception;
  8. use think\admin\extend\CodeExtend;
  9. /**
  10. * 账号余额支付参数处理
  11. * Class BalancePaymentService
  12. * @package app\data\service\payment
  13. */
  14. class BalancePaymentService extends PaymentService
  15. {
  16. /**
  17. * 订单信息查询
  18. * @param string $orderNo
  19. * @return array
  20. */
  21. public function query(string $orderNo): array
  22. {
  23. return [];
  24. }
  25. /**
  26. * 支付通知处理
  27. * @return string
  28. */
  29. public function notify(): string
  30. {
  31. return 'SUCCESS';
  32. }
  33. /**
  34. * 创建订单支付参数
  35. * @param string $openid 用户OPENID
  36. * @param string $orderNo 交易订单单号
  37. * @param string $payAmount 交易订单金额(元)
  38. * @param string $payTitle 交易订单名称
  39. * @param string $payRemark 订单订单描述
  40. * @param string $payReturn 完成回跳地址
  41. * @param string $payImage 支付凭证图片
  42. * @return array
  43. * @throws \think\admin\Exception
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payRemark, string $payReturn = '', string $payImage = ''): array
  49. {
  50. $order = ShopOrder::mk()->where(['order_no' => $orderNo])->find();
  51. if (empty($order)) throw new Exception("订单不存在");
  52. if ($order['status'] !== 2) throw new Exception("不可发起支付");
  53. // 创建支付行为
  54. $this->createPaymentAction($orderNo, $payTitle, $payAmount);
  55. // 检查能否支付
  56. [$total, $count] = UserBalanceService::amount($order['uuid'], [$orderNo]);
  57. if ($payAmount > $total - $count) throw new Exception("可抵扣余额不足");
  58. try {
  59. // 扣减用户余额
  60. $this->app->db->transaction(function () use ($order, $payAmount) {
  61. // 更新订单余额
  62. ShopOrder::mk()->where(['order_no' => $order['order_no']])->update([
  63. 'payment_balance' => $payAmount,
  64. ]);
  65. // 扣除余额金额
  66. DataUserBalance::mUpdate([
  67. 'uuid' => $order['uuid'],
  68. 'code' => "KC{$order['order_no']}",
  69. 'name' => "账户余额支付",
  70. 'remark' => "支付订单 {$order['order_no']} 的扣除余额 {$payAmount} 元",
  71. 'amount' => -$payAmount,
  72. ], 'code');
  73. // 更新支付行为
  74. $this->updatePaymentAction($order['order_no'], CodeExtend::uniqidDate(20), $payAmount, '账户余额支付');
  75. });
  76. // 刷新用户余额
  77. UserBalanceService::amount($order['uuid']);
  78. return ['code' => 1, 'info' => '余额支付完成'];
  79. } catch (\Exception $exception) {
  80. return ['code' => 0, 'info' => $exception->getMessage()];
  81. }
  82. }
  83. }