PayService.php 6.2 KB

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