Client.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\easywechat\combinePay;
  12. use app\common\model\store\order\StoreRefundOrder;
  13. use crmeb\services\easywechat\BaseClient;
  14. use think\exception\ValidateException;
  15. use think\facade\Route;
  16. use function EasyWeChat\Payment\generate_sign;
  17. class Client extends BaseClient
  18. {
  19. public function handleNotify($callback)
  20. {
  21. $request = request();
  22. $success = $request->post('event_type') === 'TRANSACTION.SUCCESS';
  23. $data = $this->decrypt($request->post('resource', []));
  24. $handleResult = call_user_func_array($callback, [json_decode($data, true), $success]);
  25. if (is_bool($handleResult) && $handleResult) {
  26. $response = [
  27. 'code' => 'SUCCESS',
  28. 'message' => 'OK',
  29. ];
  30. } else {
  31. $response = [
  32. 'code' => 'FAIL',
  33. 'message' => $handleResult,
  34. ];
  35. }
  36. return response($response, 200, [], 'json');
  37. }
  38. public function pay($type, array $order)
  39. {
  40. $params = [
  41. 'combine_out_trade_no' => $order['order_sn'],
  42. 'combine_mchid' => $this->app['config']['service_payment']['merchant_id'],
  43. 'combine_appid' => $this->app['config']['app_id'],
  44. 'scene_info' => [
  45. 'device_id' => 'shop system',
  46. 'payer_client_ip' => request()->ip(),
  47. ],
  48. 'sub_orders' => [],
  49. 'notify_url' => rtrim(systemConfig('site_url'), '/') . Route::buildUrl($this->app['config']['service_payment']['type'] . 'CombinePayNotify', ['type' => $order['attach']])->build(),
  50. ];
  51. if ($type === 'h5') {
  52. $params['scene_info']['h5_info'] = [
  53. 'type' => $order['h5_type'] ?? 'Wap'
  54. ];
  55. }
  56. foreach ($order['sub_orders'] as $sub_order) {
  57. $subOrder = [
  58. 'mchid' => $this->app['config']['service_payment']['merchant_id'],
  59. 'amount' => [
  60. 'total_amount' => intval($sub_order['pay_price'] * 100),
  61. 'currency' => 'CNY',
  62. ],
  63. 'settle_info' => [
  64. 'profit_sharing' => true
  65. ],
  66. 'out_trade_no' => $sub_order['order_sn'],
  67. 'sub_mchid' => $sub_order['sub_mchid']
  68. ];
  69. $subOrder['attach'] = $sub_order['attach'] ?? $order['attach'] ?? '';
  70. $subOrder['description'] = $sub_order['body'] ?? $order['body'] ?? '';
  71. $params['sub_orders'][] = $subOrder;
  72. }
  73. if (isset($order['openid'])) {
  74. $params['combine_payer_info'] = [
  75. 'openid' => $order['openid'],
  76. ];
  77. }
  78. $content = json_encode($params, JSON_UNESCAPED_UNICODE);
  79. $res = $this->request('/v3/combine-transactions/' . $type, 'POST', ['sign_body' => $content]);
  80. if (isset($res['code'])) {
  81. throw new ValidateException('微信接口报错:' . $res['message']);
  82. }
  83. return $res;
  84. }
  85. public function payApp(array $options)
  86. {
  87. $res = $this->pay('app', $options);
  88. return $this->configForAppPayment($res['prepay_id']);
  89. }
  90. /**
  91. * @param string $type 场景类型,枚举值: iOS:IOS移动应用; Android:安卓移动应用; Wap:WAP网站应用
  92. */
  93. public function payH5(array $options, $type = 'Wap')
  94. {
  95. $options['h5_type'] = $type;
  96. return $this->pay('h5', $options);
  97. }
  98. public function payJs($openId, array $options)
  99. {
  100. $options['openid'] = $openId;
  101. $res = $this->pay('jsapi', $options);
  102. return $this->configForJSSDKPayment($res['prepay_id']);
  103. }
  104. public function payNative(array $options)
  105. {
  106. return $this->pay('native', $options);
  107. }
  108. public function profitsharingOrder(array $options, bool $finish = false)
  109. {
  110. $params = [
  111. 'appid' => $this->app['config']['app_id'],
  112. 'sub_mchid' => $options['sub_mchid'],
  113. 'transaction_id' => $options['transaction_id'],
  114. 'out_order_no' => $options['out_order_no'],
  115. 'receivers' => [],
  116. 'finish' => $finish
  117. ];
  118. foreach ($options['receivers'] as $receiver) {
  119. $data = [
  120. 'amount' => intval($receiver['amount'] * 100),
  121. 'description' => $receiver['body'] ?? $options['body'] ?? '',
  122. ];
  123. $data['receiver_account'] = $receiver['receiver_account'];
  124. if (isset($receiver['receiver_name'])) {
  125. $data['receiver_name'] = $receiver['receiver_name'];
  126. $data['type'] = 'PERSONAL_OPENID';
  127. } else {
  128. $data['type'] = 'MERCHANT_ID';
  129. }
  130. $params['receivers'][] = $data;
  131. }
  132. $content = json_encode($params);
  133. $res = $this->request('/v3/ecommerce/profitsharing/orders', 'POST', ['sign_body' => $content]);
  134. if (isset($res['code'])) {
  135. throw new ValidateException('微信接口报错:' . $res['message']);
  136. }
  137. return $res;
  138. }
  139. public function profitsharingFinishOrder(array $params)
  140. {
  141. $content = json_encode($params);
  142. $res = $this->request('/v3/ecommerce/profitsharing/finish-order', 'POST', ['sign_body' => $content]);
  143. if (isset($res['code'])) {
  144. throw new ValidateException('微信接口报错:' . $res['message']);
  145. }
  146. return $res;
  147. }
  148. public function payOrderRefund(string $order_sn, array $options)
  149. {
  150. $params = [
  151. 'sub_mchid' => $options['sub_mchid'],
  152. 'sp_appid' => $this->app['config']['app_id'],
  153. 'out_trade_no' => $options['order_sn'],
  154. 'out_refund_no' => $options['refund_order_sn'],
  155. 'amount' => [
  156. 'refund' => intval($options['refund_price'] * 100),
  157. 'total' => intval($options['pay_price'] * 100),
  158. 'currency' => 'CNY'
  159. ]
  160. ];
  161. if (isset($options['reason'])) {
  162. $params['reason'] = $options['reason'];
  163. }
  164. if (isset($options['refund_account'])) {
  165. $params['refund_account'] = $options['refund_account'];
  166. }
  167. $content = json_encode($params);
  168. $res = $this->request('/v3/ecommerce/refunds/apply', 'POST', ['sign_body' => $content], true);
  169. if (isset($res['code'])) {
  170. throw new ValidateException('微信接口报错:' . $res['message']);
  171. }
  172. return $res;
  173. }
  174. public function returnAdvance($refund_id, $sub_mchid)
  175. {
  176. $res = $this->request('/v3/ecommerce/refunds/' . $refund_id . '/return-advance', 'POST', ['sign_body' => json_encode(compact('sub_mchid'))], true);
  177. if (isset($res['code'])) {
  178. throw new ValidateException('微信接口报错:' . $res['message']);
  179. }
  180. return $res;
  181. }
  182. public function configForPayment($prepayId, $json = true)
  183. {
  184. $params = [
  185. 'appId' => $this->app['config']['app_id'],
  186. 'timeStamp' => strval(time()),
  187. 'nonceStr' => uniqid(),
  188. 'package' => "prepay_id=$prepayId",
  189. 'signType' => 'RSA',
  190. ];
  191. $message = $params['appId'] . "\n" .
  192. $params['timeStamp'] . "\n" .
  193. $params['nonceStr'] . "\n" .
  194. $params['package'] . "\n";
  195. openssl_sign($message, $raw_sign, $this->getPrivateKey(), 'sha256WithRSAEncryption');
  196. $sign = base64_encode($raw_sign);
  197. $params['paySign'] = $sign;
  198. return $json ? json_encode($params) : $params;
  199. }
  200. /**
  201. * Generate app payment parameters.
  202. *
  203. * @param string $prepayId
  204. *
  205. * @return array
  206. */
  207. public function configForAppPayment($prepayId)
  208. {
  209. $params = [
  210. 'appid' => $this->app['config']['app_id'],
  211. 'partnerid' => $this->app['config']['service_payment']['merchant_id'],
  212. 'prepayid' => $prepayId,
  213. 'noncestr' => uniqid(),
  214. 'timestamp' => time(),
  215. 'package' => 'Sign=WXPay',
  216. ];
  217. $message = $params['appid'] . "\n" .
  218. $params['timestamp'] . "\n" .
  219. $params['noncestr'] . "\n" .
  220. $params['prepayid'] . "\n";
  221. openssl_sign($message, $raw_sign, $this->getPrivateKey(), 'sha256WithRSAEncryption');
  222. $sign = base64_encode($raw_sign);
  223. $params['sign'] = $sign;
  224. return $params;
  225. }
  226. public function configForJSSDKPayment($prepayId)
  227. {
  228. $config = $this->configForPayment($prepayId, false);
  229. $config['timestamp'] = $config['timeStamp'];
  230. unset($config['timeStamp']);
  231. return $config;
  232. }
  233. }