YuePayServices.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. switch ($type) {
  52. case 'pay_product'://商品余额
  53. //写入余额记录
  54. $now_money = bcsub((string)$userInfo['now_money'], (string)$orderInfo['pay_price'], 2);
  55. $number = $orderInfo['pay_price'];
  56. /** @var UserMoneyServices $userMoneyServices */
  57. $userMoneyServices = app()->make(UserMoneyServices::class);
  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. /** @var OtherOrderServices $OtherOrderServices */
  65. $OtherOrderServices = app()->make(OtherOrderServices::class);
  66. $res = $res && $OtherOrderServices->paySuccess($orderInfo, PayServices::YUE_PAY);//余额支付成功
  67. break;
  68. }
  69. if (!$res) {
  70. throw new ApiException(410279);
  71. }
  72. });
  73. return ['status' => true];
  74. }
  75. }