You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\services\pay;
  3. use app\services\BaseServices;
  4. use app\services\order\OtherOrderServices;
  5. use app\services\order\StoreOrderServices;
  6. use app\services\order\StoreOrderSuccessServices;
  7. use app\services\user\UserMoneyServices;
  8. use app\services\user\UserServices;
  9. use think\exception\ValidateException;
  10. /**
  11. * 余额支付
  12. * Class YuePayServices
  13. * @package app\services\pay
  14. */
  15. class YuePayServices extends BaseServices
  16. {
  17. /**
  18. * 次卡订单余额支付
  19. * @param array $orderInfo
  20. * @param $uid
  21. * @return bool[]|string[]
  22. */
  23. public function OnceCardYueOrderPay(array $orderInfo, $uid)
  24. {
  25. /** @var UserServices $services */
  26. $services = app()->make(UserServices::class);
  27. $userInfo = $services->getUserInfo($uid);
  28. if ($userInfo['now_money'] < $orderInfo['need_pay']) {
  29. throw new ValidateException('余额不足');
  30. }
  31. $this->transaction(function () use ($services, $orderInfo, $userInfo) {
  32. $res = false !== $services->bcDec($userInfo['uid'], 'now_money', $orderInfo['need_pay'], 'uid');
  33. //写入余额记录
  34. $now_money = bcsub((string)$userInfo['now_money'], (string)$orderInfo['need_pay'], 2);
  35. $number = $orderInfo['need_pay'];
  36. /** @var UserMoneyServices $userMoneyServices */
  37. $userMoneyServices = app()->make(UserMoneyServices::class);
  38. $res = $res && $userMoneyServices->income('pay_once_card', $userInfo['uid'], $number, $now_money, $orderInfo['once_card_order_id']);
  39. if (!$res) {
  40. throw new ValidateException('余额支付失败!');
  41. }
  42. });
  43. return ['status' => true];
  44. }
  45. /**
  46. * 订单余额支付
  47. * @param array $orderInfo
  48. * @param $uid
  49. * @return bool[]|string[]
  50. */
  51. public function yueOrderPay(array $orderInfo, $uid)
  52. {
  53. if (!$orderInfo) {
  54. throw new ValidateException('订单不存在');
  55. }
  56. if ($orderInfo['paid']) {
  57. throw new ValidateException('该订单已支付!');
  58. }
  59. $type = 'pay_product';
  60. if (isset($orderInfo['member_type'])) {
  61. $type = 'pay_member';
  62. }
  63. /** @var UserServices $services */
  64. $services = app()->make(UserServices::class);
  65. $userInfo = $services->getUserInfo($uid);
  66. if ($userInfo['now_money'] < $orderInfo['pay_price']) {
  67. return ['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($orderInfo['pay_price'])];
  68. }
  69. $this->transaction(function () use ($services, $orderInfo, $userInfo, $type) {
  70. $res = false !== $services->bcDec($userInfo['uid'], 'now_money', $orderInfo['pay_price'], 'uid');
  71. switch ($type) {
  72. case 'pay_product'://商品余额
  73. $id = $orderInfo['id'] ?? 0;
  74. /** @var StoreOrderServices $orderSerives */
  75. $orderSerives = app()->make(StoreOrderServices::class);
  76. $orderInfo = $orderSerives->get($id);
  77. if (!$orderInfo) {
  78. throw new ValidateException('订单不存在');
  79. }
  80. $orderInfo = $orderInfo->toArray();
  81. //写入余额记录
  82. $now_money = bcsub((string)$userInfo['now_money'], (string)$orderInfo['pay_price'], 2);
  83. $number = $orderInfo['pay_price'];
  84. /** @var UserMoneyServices $userMoneyServices */
  85. $userMoneyServices = app()->make(UserMoneyServices::class);
  86. $res = $res && $userMoneyServices->income('pay_product', $userInfo['uid'], $number, $now_money, $orderInfo['id']);
  87. /** @var StoreOrderSuccessServices $orderServices */
  88. $orderServices = app()->make(StoreOrderSuccessServices::class);
  89. $res = $res && $orderServices->paySuccess($orderInfo, PayServices::YUE_PAY, ['userInfo' => $userInfo]);//余额支付成功
  90. break;
  91. case 'pay_member'://会员卡支付
  92. /** @var OtherOrderServices $OtherOrderServices */
  93. $OtherOrderServices = app()->make(OtherOrderServices::class);
  94. $res = $res && $OtherOrderServices->paySuccess($orderInfo, PayServices::YUE_PAY, ['userInfo' => $userInfo]);//余额支付成功
  95. break;
  96. }
  97. if (!$res) {
  98. throw new ValidateException('余额支付失败!');
  99. }
  100. });
  101. return ['status' => true];
  102. }
  103. }