BalancePyamentService.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\data\service\payment;
  3. use app\data\service\PaymentService;
  4. use app\data\service\UserService;
  5. use think\admin\extend\CodeExtend;
  6. use think\Exception;
  7. /**
  8. * 账号余额支付参数处理
  9. * Class BalancePyamentService
  10. * @package app\data\service\payment
  11. */
  12. class BalancePyamentService extends PaymentService
  13. {
  14. /**
  15. * 订单信息查询
  16. * @param string $orderNo
  17. * @return array
  18. */
  19. public function query(string $orderNo): array
  20. {
  21. return [];
  22. }
  23. /**
  24. * 支付通知处理
  25. * @return string
  26. */
  27. public function notify(): string
  28. {
  29. return 'SUCCESS';
  30. }
  31. /**
  32. * 创建余额支付
  33. * @param string $openid
  34. * @param string $orderNo
  35. * @param string $paymentAmount
  36. * @param string $paymentTitle
  37. * @param string $paymentRemark
  38. * @param string $paymentReturn
  39. * @return array
  40. * @throws Exception
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function create(string $openid, string $orderNo, string $paymentAmount, string $paymentTitle, string $paymentRemark, string $paymentReturn = ''): array
  46. {
  47. $order = $this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->find();
  48. if (empty($order)) throw new Exception("订单不存在");
  49. if ($order['status'] !== 2) throw new Exception("不可发起支付");
  50. // 创建支付行为
  51. $this->createPaymentAction($orderNo, $paymentTitle, $paymentAmount);
  52. // 扣减用户余额
  53. [$total, $used] = UserService::instance()->balance($order['uid'], [$orderNo]);
  54. if ($paymentAmount > $total - $used) throw new Exception("可抵扣余额不足");
  55. $this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->update(['amount_balance' => $paymentAmount]);
  56. // 更新支付行为
  57. $this->updatePaymentAction($orderNo, CodeExtend::uniqidDate(20), $paymentAmount, '账户余额支付');
  58. // 刷新用户余额
  59. UserService::instance()->balance($order['uid']);
  60. return ['info' => '余额支付完成'];
  61. }
  62. }