PayService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Master\Framework\Library\Easywechat;
  3. use EasyWeChat\Pay\Application;
  4. use EasyWeChat\Kernel\Contracts\Config;
  5. class PayService extends EasyModule
  6. {
  7. protected Application $app;
  8. protected Config $config;
  9. /**
  10. * 实例化
  11. */
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->app = $this->pay();
  16. $this->config = $this->app->getConfig();
  17. }
  18. /**
  19. * 支付 server
  20. * @return \EasyWeChat\Kernel\Contracts\Server|\EasyWeChat\Pay\Server
  21. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  22. * @throws \ReflectionException
  23. * @throws \Throwable
  24. */
  25. public function getServer()
  26. {
  27. return $this->app->getServer();
  28. }
  29. /**
  30. * 统一下单
  31. *
  32. * @param string $openid
  33. * @param string $out_trade_no
  34. * @param int $fee
  35. * @param string $description
  36. * @param string $notify_url
  37. * @return bool
  38. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  39. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  40. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  41. */
  42. public function jsapi(string $openid, string $out_trade_no, int $fee = 0, string $description = '', string $notify_url = '')
  43. {
  44. $app = $this->app;
  45. $api = $app->getClient();
  46. $response = $api->postJson("v3/pay/transactions/jsapi", [
  47. 'mchid' => (string)$this->config['mch_id'], // <---- 请修改为您的【子商户号/二级商户号】由微信支付生成并下发。
  48. "out_trade_no" => $out_trade_no, // 外部商户订单号
  49. "appid" => (string)$this->config['app_id'], // <---- 请修改为【子商户号/二级商户号】服务号的 appid
  50. "description" => $description,
  51. "notify_url" => $notify_url,// 回调地址
  52. "amount" => [
  53. "total" => $fee,
  54. "currency" => "CNY"
  55. ],
  56. "payer" => [
  57. "openid" => $openid, // <---- 【用户子标识】 用户在子商户AppID下的唯一标识。若传sub_openid,那sub_appid必填。下单前需获取到用户的OpenID
  58. ]
  59. ]);
  60. if (!$this->response($response)) {
  61. return false;
  62. }
  63. if (empty($this->data['prepay_id'])) {
  64. return false;
  65. }
  66. $appId = (string)$this->config['app_id'];
  67. $signType = 'RSA'; // 默认RSA,v2要传MD5
  68. $utils = $app->getUtils();
  69. $this->data = $utils->buildBridgeConfig($this->data['prepay_id'], $appId, $signType); // 返回数组
  70. return true;
  71. }
  72. /**
  73. * App下单
  74. * TODO 有待测试
  75. * @param string $out_trade_no
  76. * @param int $fee
  77. * @param string $description
  78. * @param string $notify_url
  79. * @return bool
  80. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  81. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  82. */
  83. public function appPay(string $out_trade_no, int $fee = 0, string $description = '', string $notify_url = '')
  84. {
  85. $app = $this->app;
  86. $api = $app->getClient();
  87. $response = $api->postJson("v3/pay/transactions/app", [
  88. "appid" => $this->config['app_id'], // <---- 请修改为【子商户号/二级商户号】服务号的 appid
  89. 'mchid' => $this->config['mch_id'], // <---- 请修改为您的【子商户号/二级商户号】由微信支付生成并下发。
  90. "description" => $description,
  91. "out_trade_no" => $out_trade_no,// 外部商户订单号
  92. "notify_url" => $notify_url, // 回调地址
  93. "amount" => [
  94. "total" => $fee,
  95. "currency" => "CNY"
  96. ]
  97. ])->throw(false);
  98. if (!$this->response($response)) {
  99. return false;
  100. }
  101. if (empty($this->data['prepay_id'])) {
  102. return false;
  103. }
  104. $appId = $this->config['app_id'];
  105. $signType = 'RSA'; // 默认RSA,v2要传MD5
  106. $utils = $app->getUtils();
  107. $this->data = $utils->buildBridgeConfig($this->data['prepay_id'], $appId, $signType); // 返回数组
  108. return true;
  109. }
  110. /**
  111. * H5下单
  112. * TODO 有待完善
  113. * @param string $out_trade_no
  114. * @param int $fee
  115. * @param string $description
  116. * @param string $notify_url
  117. * @return bool
  118. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  119. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  120. */
  121. public function h5Pay(string $out_trade_no, int $fee = 0, string $description = '', string $notify_url = '')
  122. {
  123. $app = $this->app;
  124. $api = $app->getClient();
  125. $response = $api->postJson("v3/pay/transactions/h5", [
  126. "appid" => $this->config['app_id'], // <---- 请修改为【子商户号/二级商户号】服务号的 appid
  127. 'mchid' => $this->config['mch_id'], // <---- 请修改为您的【子商户号/二级商户号】由微信支付生成并下发。
  128. "description" => $description,
  129. "out_trade_no" => $out_trade_no,// 外部商户订单号
  130. "notify_url" => $notify_url, // 回调地址
  131. "amount" => [
  132. "total" => $fee,
  133. "currency" => "CNY"
  134. ],
  135. "scene_info" => [
  136. "payer_client_ip" => $_SERVER['HTTP_X_REAL_IP'] ?? Request::capture()->getClientIp(),
  137. ""
  138. ]
  139. ])->throw(false);
  140. if (!$this->response($response)) {
  141. return false;
  142. }
  143. if (empty($this->data['prepay_id'])) {
  144. return false;
  145. }
  146. $appId = $this->config['app_id'];
  147. $signType = 'RSA'; // 默认RSA,v2要传MD5
  148. $utils = $app->getUtils();
  149. $this->data = $utils->buildBridgeConfig($this->data['prepay_id'], $appId, $signType); // 返回数组
  150. return true;
  151. }
  152. public function handlePaid(callable $function)
  153. {
  154. if (!$function()){
  155. return $this;
  156. }
  157. }
  158. }