Client.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\pay;
  12. use crmeb\services\easywechat\BaseClient;
  13. use think\exception\ValidateException;
  14. use think\facade\Log;
  15. class Client extends BaseClient
  16. {
  17. protected $isService = false;
  18. public function handleNotify($callback)
  19. {
  20. $request = request();
  21. $success = $request->post('event_type') === 'TRANSACTION.SUCCESS';
  22. $data = $this->decrypt($request->post('resource', []));
  23. Log::info('支付回调参数v3:'.var_export($request->post(),1));
  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, $order)
  39. {
  40. $params = [
  41. 'appid' => $this->app['config']['app_id'],
  42. 'mchid' => $this->app['config']['payment']['merchant_id'],
  43. 'description' => $order['body'],
  44. 'out_trade_no' => $order['out_trade_no'],
  45. 'attach' => $order['attach'],
  46. 'notify_url' => $this->app['config']['payment']['notify_url'],
  47. 'amount' => [
  48. 'total' => intval($order['total_fee']),
  49. 'currency' => 'CNY'
  50. ],
  51. 'scene_info' => [
  52. 'device_id' => 'shop system',
  53. 'payer_client_ip' => request()->ip(),
  54. ],
  55. ];
  56. if ($type === 'h5') {
  57. $params['scene_info']['h5_info'] = [
  58. 'type' => $order['h5_type'] ?? 'Wap'
  59. ];
  60. }
  61. if (isset($order['openid'])) {
  62. $params['payer'] = [
  63. 'openid' => $order['openid']
  64. ];
  65. }
  66. Log::info('微信v3支付:'.var_export($params,true));
  67. $content = json_encode($params, JSON_UNESCAPED_UNICODE);
  68. $res = $this->request('/v3/pay/transactions/' . $type, 'POST', ['sign_body' => $content]);
  69. if (isset($res['code'])) {
  70. throw new ValidateException('微信接口报错:' . $res['message']);
  71. }
  72. return $res;
  73. }
  74. public function payApp($options)
  75. {
  76. $res = $this->pay('app', $options);
  77. return $this->configForAppPayment($res['prepay_id']);
  78. }
  79. /**
  80. * @param string $type 场景类型,枚举值: iOS:IOS移动应用; Android:安卓移动应用; Wap:WAP网站应用
  81. */
  82. public function payH5($options, $type = 'Wap')
  83. {
  84. $options['h5_type'] = $type;
  85. return $this->pay('h5', $options);
  86. }
  87. public function payJsapi($options)
  88. {
  89. $res = $this->pay('jsapi', $options);
  90. return $this->configForJSSDKPayment($res['prepay_id']);
  91. }
  92. public function payNative($options)
  93. {
  94. unset($options['openid']);
  95. return $this->pay('native', $options);
  96. }
  97. public function refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId = null, $type, $refundAccount, $refundReason)
  98. {
  99. $params = [
  100. $type => $orderNo,
  101. 'out_refund_no' => $refundNo,
  102. 'amount' => [
  103. 'refund' => intval($refundFee),
  104. 'total' => intval($totalFee),
  105. 'currency' => 'CNY'
  106. ]
  107. ];
  108. if (isset($refundReason)) {
  109. $params['reason'] = $refundReason;
  110. }
  111. // if (isset($refundAccount)) {
  112. // $params['refund_account'] = $refundAccount;
  113. // }
  114. $content = json_encode($params);
  115. $res = $this->request('/v3/refund/domestic/refunds', 'POST', ['sign_body' => $content], true);
  116. if (isset($res['code'])) {
  117. throw new ValidateException('微信接口报错:' . $res['message']);
  118. }
  119. return $res;
  120. }
  121. public function configForPayment($prepayId, $json = true)
  122. {
  123. $params = [
  124. 'appId' => $this->app['config']['app_id'],
  125. 'timeStamp' => strval(time()),
  126. 'nonceStr' => uniqid(),
  127. 'package' => "prepay_id=$prepayId",
  128. 'signType' => 'RSA',
  129. ];
  130. $message = $params['appId'] . "\n" .
  131. $params['timeStamp'] . "\n" .
  132. $params['nonceStr'] . "\n" .
  133. $params['package'] . "\n";
  134. openssl_sign($message, $raw_sign, $this->getPrivateKey(), 'sha256WithRSAEncryption');
  135. $sign = base64_encode($raw_sign);
  136. $params['paySign'] = $sign;
  137. return $json ? json_encode($params) : $params;
  138. }
  139. /**
  140. * Generate app payment parameters.
  141. *
  142. * @param string $prepayId
  143. *
  144. * @return array
  145. */
  146. public function configForAppPayment($prepayId)
  147. {
  148. $params = [
  149. 'appid' => $this->app['config']['app_id'],
  150. 'partnerid' => $this->app['config']['payment']['merchant_id'],
  151. 'prepayid' => $prepayId,
  152. 'noncestr' => uniqid(),
  153. 'timestamp' => time(),
  154. 'package' => 'Sign=WXPay',
  155. ];
  156. $message = $params['appid'] . "\n" .
  157. $params['timestamp'] . "\n" .
  158. $params['noncestr'] . "\n" .
  159. $params['prepayid'] . "\n";
  160. openssl_sign($message, $raw_sign, $this->getPrivateKey(), 'sha256WithRSAEncryption');
  161. $sign = base64_encode($raw_sign);
  162. $params['sign'] = $sign;
  163. return $params;
  164. }
  165. public function configForJSSDKPayment($prepayId)
  166. {
  167. $config = $this->configForPayment($prepayId, false);
  168. $config['timestamp'] = $config['timeStamp'];
  169. unset($config['timeStamp']);
  170. return $config;
  171. }
  172. }