YuePayServices.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\StoreOrderServices;
  15. use app\services\order\StoreOrderSuccessServices;
  16. use app\services\activity\integral\StoreIntegralOrderServices;
  17. use app\services\user\UserMoneyServices;
  18. use app\services\user\UserServices;
  19. use think\exception\ValidateException;
  20. /**
  21. * 余额支付
  22. * Class YuePayServices
  23. * @package app\services\pay
  24. */
  25. class YuePayServices extends BaseServices
  26. {
  27. /**
  28. * 订单余额支付
  29. * @param array $orderInfo
  30. * @param $uid
  31. * @return bool[]|string[]
  32. */
  33. public function yueOrderPay(array $orderInfo, $uid)
  34. {
  35. if (!$orderInfo) {
  36. throw new ValidateException('订单不存在');
  37. }
  38. if ($orderInfo['paid']) {
  39. throw new ValidateException('该订单已支付!');
  40. }
  41. $type = 'pay_product';
  42. if (isset($orderInfo['member_type'])) {
  43. $type = 'pay_member';
  44. }
  45. /** @var UserServices $services */
  46. $services = app()->make(UserServices::class);
  47. $userInfo = $services->getUserInfo($uid);
  48. if ($userInfo['now_money'] < $orderInfo['pay_price']) {
  49. return ['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($orderInfo['pay_price'])];
  50. }
  51. $this->transaction(function () use ($services, $orderInfo, $userInfo, $type) {
  52. $res = false !== $services->bcDec($userInfo['uid'], 'now_money', $orderInfo['pay_price'], 'uid');
  53. switch ($type) {
  54. case 'pay_product'://商品余额
  55. $id = $orderInfo['id'] ?? 0;
  56. /** @var StoreOrderServices $orderSerives */
  57. $orderSerives = app()->make(StoreOrderServices::class);
  58. $orderInfo = $orderSerives->get($id);
  59. if (!$orderInfo) {
  60. throw new ValidateException('订单不存在');
  61. }
  62. $orderInfo = $orderInfo->toArray();
  63. //写入余额记录
  64. $now_money = bcsub((string)$userInfo['now_money'], (string)$orderInfo['pay_price'], 2);
  65. $number = $orderInfo['pay_price'];
  66. /** @var UserMoneyServices $userMoneyServices */
  67. $userMoneyServices = app()->make(UserMoneyServices::class);
  68. $res = $res && $userMoneyServices->income('pay_product', $userInfo['uid'], $number, $now_money, $orderInfo['id']);
  69. /** @var StoreOrderSuccessServices $orderServices */
  70. $orderServices = app()->make(StoreOrderSuccessServices::class);
  71. $res = $res && $orderServices->paySuccess($orderInfo, PayServices::YUE_PAY, ['userInfo' => $userInfo]);//余额支付成功
  72. break;
  73. case 'pay_member'://会员卡支付
  74. /** @var OtherOrderServices $OtherOrderServices */
  75. $OtherOrderServices = app()->make(OtherOrderServices::class);
  76. $res = $res && $OtherOrderServices->paySuccess($orderInfo, PayServices::YUE_PAY, ['userInfo' => $userInfo]);//余额支付成功
  77. break;
  78. }
  79. if (!$res) {
  80. throw new ValidateException('余额支付失败!');
  81. }
  82. });
  83. return ['status' => true];
  84. }
  85. /**
  86. * 积分商品订单余额支付
  87. * @param array $orderInfo
  88. * @param $uid
  89. * @return bool[]|string[]
  90. */
  91. public function yueIntegralOrderPay(array $orderInfo, $uid)
  92. {
  93. if (!$orderInfo) {
  94. throw new ValidateException('订单不存在');
  95. }
  96. if ($orderInfo['paid']) {
  97. throw new ValidateException('该订单已支付!');
  98. }
  99. $type = 'pay_integral_product';
  100. /** @var UserServices $services */
  101. $services = app()->make(UserServices::class);
  102. $userInfo = $services->getUserInfo($uid);
  103. if($userInfo) {
  104. $userInfo = $userInfo->toArray();
  105. } else {
  106. throw new ValidateException('用户信息不存在!');
  107. }
  108. if ($userInfo['now_money'] < $orderInfo['total_price']) {
  109. return ['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($orderInfo['total_price'])];
  110. }
  111. $this->transaction(function () use ($services, $orderInfo, $userInfo, $type) {
  112. $res = false !== $services->bcDec($userInfo['uid'], 'now_money', $orderInfo['total_price'], 'uid');
  113. switch ($type) {
  114. case 'pay_integral_product'://积分商品余额
  115. $id = $orderInfo['id'] ?? 0;
  116. /** @var StoreIntegralOrderServices $orderSerives */
  117. $orderSerives = app()->make(StoreIntegralOrderServices::class);
  118. $orderInfo = $orderSerives->get($id);
  119. if (!$orderInfo) {
  120. throw new ValidateException('订单不存在');
  121. }
  122. $orderInfo = $orderInfo->toArray();
  123. //写入余额记录
  124. $now_money = bcsub((string)$userInfo['now_money'], (string)$orderInfo['total_price'], 2);
  125. $number = $orderInfo['total_price'];
  126. /** @var UserMoneyServices $userMoneyServices */
  127. $userMoneyServices = app()->make(UserMoneyServices::class);
  128. $res = $res && $userMoneyServices->income('pay_integral_product', $userInfo['uid'], $number, $now_money, $orderInfo['id']);
  129. /** @var StoreIntegralOrderServices $orderServices */
  130. $orderServices = app()->make(StoreIntegralOrderServices::class);
  131. $res = $res && $orderServices->paySuccess($orderInfo, PayServices::YUE_PAY, ['userInfo' => $userInfo]);//余额支付成功
  132. break;
  133. }
  134. if (!$res) {
  135. throw new ValidateException('余额支付失败!');
  136. }
  137. });
  138. return ['status' => true];
  139. }
  140. }