OrderPayServices.php 9.1 KB

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