YuePayServices.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\pay;
  12. use app\services\BaseServices;
  13. use app\services\order\OtherOrderServices;
  14. use app\services\order\StoreOrderSuccessServices;
  15. use app\services\user\UserMoneyServices;
  16. use app\services\user\UserServices;
  17. use crmeb\exceptions\ApiException;
  18. /**
  19. * 余额支付
  20. * Class YuePayServices
  21. * @package app\services\pay
  22. */
  23. class YuePayServices extends BaseServices
  24. {
  25. /**
  26. * 订单余额支付
  27. * @param $order_id
  28. * @param $uid
  29. * @return bool
  30. */
  31. public function yueOrderPay(array $orderInfo, $uid)
  32. {
  33. if (!$orderInfo) {
  34. throw new ApiException(410173);
  35. }
  36. if ($orderInfo['paid']) {
  37. throw new ApiException(410174);
  38. }
  39. $type = 'pay_product';
  40. if (isset($orderInfo['member_type'])) {
  41. $type = 'pay_member';
  42. }
  43. /** @var UserServices $services */
  44. $services = app()->make(UserServices::class);
  45. $userInfo = $services->getUserInfo($uid);
  46. if ($userInfo['now_money'] < $orderInfo['pay_price']) {
  47. return ['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($orderInfo['pay_price'])];
  48. }
  49. $this->transaction(function () use ($services, $orderInfo, $userInfo, $type) {
  50. $res = false !== $services->bcDec($userInfo['uid'], 'now_money', $orderInfo['pay_price'], 'uid');
  51. /** @var UserMoneyServices $userMoneyServices */
  52. $userMoneyServices = app()->make(UserMoneyServices::class);
  53. //写入余额记录
  54. $now_money = bcsub((string)$userInfo['now_money'], (string)$orderInfo['pay_price'], 2);
  55. $number = $orderInfo['pay_price'];
  56. switch ($type) {
  57. case 'pay_product'://商品余额
  58. $res = $res && $userMoneyServices->income('pay_product', $userInfo['uid'], $number, $now_money, $orderInfo['id']);
  59. /** @var StoreOrderSuccessServices $orderServices */
  60. $orderServices = app()->make(StoreOrderSuccessServices::class);
  61. $res = $res && $orderServices->paySuccess($orderInfo, PayServices::YUE_PAY);//余额支付成功
  62. break;
  63. case 'pay_member'://会员卡支付
  64. $res = $res && $userMoneyServices->income('pay_member', $userInfo['uid'], $number, $now_money, $orderInfo['id']);
  65. /** @var OtherOrderServices $OtherOrderServices */
  66. $OtherOrderServices = app()->make(OtherOrderServices::class);
  67. $res = $res && $OtherOrderServices->paySuccess($orderInfo, PayServices::YUE_PAY);//余额支付成功
  68. break;
  69. }
  70. if (!$res) {
  71. throw new ApiException(410279);
  72. }
  73. });
  74. return ['status' => true];
  75. }
  76. }