OrderPayServices.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\order\StoreOrderCartInfoServices;
  13. use app\services\order\StoreOrderServices;
  14. use app\services\wechat\WechatUserServices;
  15. use crmeb\utils\Str;
  16. use think\exception\ValidateException;
  17. /**
  18. * 订单发起支付
  19. * Class OrderPayServices
  20. * @package app\services\pay
  21. */
  22. class OrderPayServices
  23. {
  24. /**
  25. * 支付
  26. * @var PayServices
  27. */
  28. protected $payServices;
  29. public function __construct(PayServices $services)
  30. {
  31. $this->payServices = $services;
  32. }
  33. /**
  34. * 订单发起支付
  35. * @param array $orderInfo
  36. * @return array|string
  37. */
  38. public function orderPay(array $orderInfo, string $payType)
  39. {
  40. if ($orderInfo['paid']) {
  41. throw new ValidateException('订单已支付!');
  42. }
  43. if ($orderInfo['pay_price'] <= 0) {
  44. throw new ValidateException('该支付无需支付!');
  45. }
  46. $openid = '';
  47. if (!in_array($payType, ['weixinh5', 'pc']) && !request()->isApp()) {
  48. if ($payType === 'weixin') {
  49. $userType = 'wechat';
  50. } else {
  51. $userType = $payType;
  52. }
  53. /** @var WechatUserServices $services */
  54. $services = app()->make(WechatUserServices::class);
  55. $openid = $services->uidToOpenid($orderInfo['pay_uid'] ?? $orderInfo['uid'], $userType);
  56. if (!$openid) {
  57. throw new ValidateException('获取用户openid失败,无法支付');
  58. }
  59. }
  60. $site_name = sys_config('site_name');
  61. if (isset($orderInfo['member_type'])) {
  62. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
  63. $successAction = "member";
  64. } else {
  65. /** @var StoreOrderCartInfoServices $orderInfoServices */
  66. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  67. $body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id'], $orderInfo['cart_id']);
  68. $body = Str::substrUTf8($site_name . '--' . $body, 30);
  69. $successAction = "product";
  70. }
  71. if (!$body) {
  72. throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  73. }
  74. /** @var StoreOrderServices $orderServices */
  75. $orderServices = app()->make(StoreOrderServices::class);
  76. $orderServices->update($orderInfo['id'], ['pay_type' => 'weixin']);
  77. return $this->payServices->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body);
  78. }
  79. /**
  80. * 支付宝支付
  81. * @param array $orderInfo
  82. * @param string $quitUrl
  83. * @return array|string
  84. */
  85. public function alipayOrder(array $orderInfo, string $quitUrl, bool $isCode = false)
  86. {
  87. if ($orderInfo['paid']) {
  88. throw new ValidateException('订单已支付!');
  89. }
  90. if ($orderInfo['pay_price'] <= 0) {
  91. throw new ValidateException('该支付无需支付!');
  92. }
  93. $site_name = sys_config('site_name');
  94. if (isset($orderInfo['member_type'])) {
  95. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
  96. $successAction = "member";
  97. } else {
  98. /** @var StoreOrderCartInfoServices $orderInfoServices */
  99. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  100. $body = $orderInfoServices->getCarIdByProductTitle($orderInfo['id'], $orderInfo['cart_id']);
  101. $body = Str::substrUTf8($site_name . '--' . $body, 30);
  102. $successAction = "product";
  103. }
  104. if (!$body) {
  105. throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  106. }
  107. /** @var StoreOrderServices $orderServices */
  108. $orderServices = app()->make(StoreOrderServices::class);
  109. $orderServices->update($orderInfo['id'], ['pay_type' => 'alipay']);
  110. return $this->payServices->pay('alipay', $quitUrl, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body, $isCode);
  111. }
  112. }