YuePayServices.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\StoreOrderSuccessServices;
  15. use app\services\user\UserBillServices;
  16. use app\services\user\UserServices;
  17. use think\exception\ValidateException;
  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 ValidateException('订单不存在!');
  35. }
  36. if ($orderInfo['paid']) {
  37. throw new ValidateException('该订单已支付!');
  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. /** @var UserBillServices $userBillServices */
  54. $userBillServices = app()->make(UserBillServices::class);
  55. $res = $res && $userBillServices->income($type, $userInfo['uid'], $orderInfo['pay_price'], $userInfo['now_money'], $orderInfo['id']);
  56. /** @var StoreOrderSuccessServices $orderServices */
  57. $orderServices = app()->make(StoreOrderSuccessServices::class);
  58. $res = $res && $orderServices->paySuccess($orderInfo, PayServices::YUE_PAY);//余额支付成功
  59. break;
  60. case 'pay_member'://会员卡支付
  61. /** @var OtherOrderServices $OtherOrderServices */
  62. $OtherOrderServices = app()->make(OtherOrderServices::class);
  63. $res = $res && $OtherOrderServices->paySuccess($orderInfo, PayServices::YUE_PAY);//余额支付成功
  64. break;
  65. }
  66. if (!$res) {
  67. throw new ValidateException('余额支付失败!');
  68. }
  69. });
  70. return ['status' => true];
  71. }
  72. }