OrderPayServices.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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\wechat\WechatUserServices;
  14. use think\exception\ValidateException;
  15. /**
  16. * 订单发起支付
  17. * Class OrderPayServices
  18. * @package app\services\pay
  19. */
  20. class OrderPayServices
  21. {
  22. /**
  23. * 支付
  24. * @var PayServices
  25. */
  26. protected $payServices;
  27. public function __construct(PayServices $services)
  28. {
  29. $this->payServices = $services;
  30. }
  31. /**
  32. * 订单发起支付
  33. * @param array $orderInfo
  34. * @return array|string
  35. */
  36. public function orderPay(array $orderInfo, string $payType)
  37. {
  38. if ($orderInfo['paid']) {
  39. throw new ValidateException('订单已支付!');
  40. }
  41. if ($orderInfo['pay_price'] <= 0) {
  42. throw new ValidateException('该支付无需支付!');
  43. }
  44. $openid = '';
  45. if (!in_array($payType, ['weixinh5', 'pc', 'store']) && !request()->isApp()) {
  46. if ($payType === 'weixin') {
  47. $userType = 'wechat';
  48. } else {
  49. $userType = $payType;
  50. }
  51. /** @var WechatUserServices $services */
  52. $services = app()->make(WechatUserServices::class);
  53. $openid = $services->uidToOpenid($orderInfo['uid'], $userType);
  54. if (!$openid) {
  55. throw new ValidateException('获取用户openid失败,无法支付');
  56. }
  57. }
  58. $site_name = sys_config('site_name');
  59. if (isset($orderInfo['member_type'])) {
  60. $body = substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
  61. $successAction = "member";
  62. } else {
  63. /** @var StoreOrderCartInfoServices $orderInfoServices */
  64. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  65. $body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
  66. $body = substrUTf8($site_name . '--' . $body, 30);
  67. $successAction = "product";
  68. }
  69. if (!$body) {
  70. throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  71. }
  72. return $this->payServices->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body);
  73. }
  74. /**
  75. * 支付宝支付
  76. * @param array $orderInfo
  77. * @param string $quitUrl
  78. * @return array|string
  79. */
  80. public function alipayOrder(array $orderInfo, string $quitUrl, bool $isCode = false)
  81. {
  82. if ($orderInfo['paid']) {
  83. throw new ValidateException('订单已支付!');
  84. }
  85. if ($orderInfo['pay_price'] <= 0) {
  86. throw new ValidateException('该支付无需支付!');
  87. }
  88. $site_name = sys_config('site_name');
  89. if (isset($orderInfo['member_type'])) {
  90. $body = substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
  91. $successAction = "member";
  92. } else {
  93. /** @var StoreOrderCartInfoServices $orderInfoServices */
  94. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  95. $body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
  96. $body = substrUTf8($site_name . '--' . $body, 30);
  97. $successAction = "product";
  98. }
  99. if (!$body) {
  100. throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  101. }
  102. return $this->payServices->pay('alipay', $quitUrl, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body, $isCode);
  103. }
  104. /**
  105. * 积分商品订单发起支付
  106. * @param array $orderInfo
  107. * @return array|string
  108. */
  109. public function orderIntegralPay(array $orderInfo, string $payType)
  110. {
  111. if ($orderInfo['paid']) {
  112. throw new ValidateException('订单已支付!');
  113. }
  114. if ($orderInfo['total_price'] <= 0) {
  115. throw new ValidateException('该支付无需支付!');
  116. }
  117. $openid = '';
  118. if (!in_array($payType, ['weixinh5', 'pc', 'store']) && !request()->isApp()) {
  119. if ($payType === 'weixin') {
  120. $userType = 'wechat';
  121. } else {
  122. $userType = $payType;
  123. }
  124. /** @var WechatUserServices $services */
  125. $services = app()->make(WechatUserServices::class);
  126. $openid = $services->uidToOpenid($orderInfo['uid'], $userType);
  127. if (!$openid) {
  128. throw new ValidateException('获取用户openid失败,无法支付');
  129. }
  130. }
  131. $site_name = sys_config('site_name');
  132. $body = $orderInfo['store_name'];
  133. $body = substrUTf8($site_name . '--' . $body, 30);
  134. $successAction = "integral";
  135. if (!$body) {
  136. throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  137. }
  138. return $this->payServices->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['total_price'], $successAction, $body);
  139. }
  140. /**
  141. * 积分商品支付宝支付
  142. * @param array $orderInfo
  143. * @param string $quitUrl
  144. * @return array|string
  145. */
  146. public function alipayIntegralOrder(array $orderInfo, string $quitUrl, bool $isCode = false)
  147. {
  148. if ($orderInfo['paid']) {
  149. throw new ValidateException('订单已支付!');
  150. }
  151. if ($orderInfo['total_price'] <= 0) {
  152. throw new ValidateException('该支付无需支付!');
  153. }
  154. $site_name = sys_config('site_name');
  155. $body = $orderInfo['store_name'];
  156. $body = substrUTf8($site_name . '--' . $body, 30);
  157. $successAction = "integral";
  158. if (!$body) {
  159. throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  160. }
  161. return $this->payServices->pay('alipay', $quitUrl, $orderInfo['order_id'], $orderInfo['total_price'], $successAction, $body, $isCode);
  162. }
  163. /**收银台会员充值
  164. * @param array $orderInfo
  165. * @param string $payType
  166. * @param string $authCode
  167. * @return array|string
  168. */
  169. public function otherRecharge(array $orderInfo, string $payType, string $authCode = '')
  170. {
  171. if (!$orderInfo) {
  172. throw new ValidateException('订单失效或者不存在');
  173. }
  174. if ($orderInfo['paid'] == 1) {
  175. throw new ValidateException('订单已支付');
  176. }
  177. $openid = '';
  178. //没有付款码,不是微信H5支付,门店支付,PC支付,不再APP端,需要判断用户openid
  179. if (!$authCode && !in_array($payType, ['weixinh5', 'store', 'pc', 'alipay']) && !request()->isApp()) {
  180. $userType = '';
  181. switch ($payType) {
  182. case 'weixin':
  183. case 'weixinh5':
  184. $userType = 'wechat';
  185. break;
  186. case 'routine':
  187. $userType = 'routine';
  188. break;
  189. }
  190. if (!$userType) {
  191. throw new ValidateException('不支持该类型方式');
  192. }
  193. /** @var WechatUserServices $wechatUser */
  194. $wechatUser = app()->make(WechatUserServices::class);
  195. $openid = $wechatUser->uidToOpenid((int)$orderInfo['uid'], $userType);
  196. if (!$openid) {
  197. throw new ValidateException('获取用户openid失败,无法支付');
  198. }
  199. }
  200. $site_name = sys_config('site_name');
  201. if (isset($orderInfo['member_type'])) {
  202. $body = substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
  203. $successAction = "member_recharge";
  204. }
  205. if (!$body) {
  206. throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  207. }
  208. return $this->payServices->setAuthCode($authCode)->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body);
  209. }
  210. }