Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

OrderPayServices.php 4.6KB

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