Wechat.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\libs\wechat\pay;
  4. // use EasyWeChat\MiniProgram\Application;
  5. // use EasyWeChat\MiniApp\Application;
  6. // use EasyWeChat\Pay\Application as PayApplication;
  7. use EasyWeChat\Payment\Application;
  8. use EasyWeChat\Kernel\Support\Str;
  9. // use EasyWeChat\Payment\LegacySignature;
  10. // use GuzzleHttp\Client;
  11. // use Hyperf\HttpServer\Contract\RequestInterface;
  12. // use Hyperf\Utils\ApplicationContext;
  13. use Illuminate\Http\Request;
  14. use EasyWeChat\Factory;
  15. class Wechat
  16. {
  17. // public static function app() {
  18. // $config = [
  19. // 'app_id' => 'wx93e902f62d243388',
  20. // 'secret' => '80d6e14f55d0593a53d131e93215ea20',
  21. // // 'oauth' => [
  22. // // 'scopes' => ['snsapi_userinfo'],
  23. // // 'callback' => '/oauth/callback',
  24. // // ],
  25. //
  26. // // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  27. // 'response_type' => 'array',
  28. //
  29. // //...
  30. // ];
  31. //
  32. // return new Application($config);
  33. // }
  34. public static function pay()
  35. {
  36. $arr = config('swdz.wechatPay');
  37. $config = [
  38. // 必要配置
  39. 'app_id' => $arr['appid'],
  40. 'mch_id' => $arr['mch_id'],
  41. 'key' => $arr['key'], // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
  42. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  43. 'cert_path' => __DIR__ . '/ssl/apiclient_cert.pem', // XXX: 绝对路径!!!!
  44. 'key_path' => __DIR__ . '/ssl/apiclient_key.pem', // XXX: 绝对路径!!!!
  45. 'notify_url' => 'https://hb.swdz.com/api/wechat/pay/aiCallback', // 你也可以在下单时单独设置来想覆盖它
  46. ];
  47. $app = Factory::payment($config);
  48. // $config = [
  49. // 'mch_id' => 1536361101,
  50. // 'app_id' => 'wx93e902f62d243388',
  51. // // 商户证书
  52. // 'private_key' => __DIR__ . '/ssl/apiclient_key.pem',
  53. // 'certificate' => __DIR__ . '/ssl/apiclient_cert.pem',
  54. //
  55. // // v3 API 秘钥
  56. // 'secret_key' => '43A03299A3C3FED3D8CE7B820Fxxxxx',
  57. //
  58. // // v2 API 秘钥
  59. // 'v2_secret_key' => 'pfbrMsAU5v9OFvJdqrg1c8YSIdtdPl8Z',
  60. //
  61. // // 平台证书:微信支付 APIv3 平台证书,需要使用工具下载
  62. // // 下载工具:https://github.com/wechatpay-apiv3/CertificateDownloader
  63. // 'platform_certs' => [
  64. // // 请使用绝对路径
  65. // // '/path/to/wechatpay/cert.pem',
  66. // ],
  67. //
  68. // /**
  69. // * 接口请求相关配置,超时时间等,具体可用参数请参考:
  70. // * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php
  71. // */
  72. // 'http' => [
  73. // 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
  74. // 'timeout' => 5.0,
  75. // // 'base_uri' => 'https://api.mch.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
  76. // ]
  77. // ];
  78. // var_dump(__DIR__ . '/ssl/apiclient_key.pem');
  79. // $app = new Application($config);
  80. return $app;
  81. }
  82. public static function addOrder($order, $user)
  83. {
  84. $result = self::pay()->order->unify([
  85. 'body' => '小程序购买钻石次数',
  86. 'device_info' => $user->id,
  87. 'nonce_str' => Str::random(),
  88. 'out_trade_no' => $order->order,
  89. 'total_fee' => $order->amount * 10000,
  90. 'spbill_create_ip' => '47.93.12.114', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
  91. 'notify_url' => 'https://hb.swdz.com/api/wechat/pay/aiCallback', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  92. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  93. 'openid' => $user->open_id,
  94. ]);
  95. // $params = [
  96. // "appid" => $config['app_id'],
  97. // "mch_id" => $config['mch_id'],
  98. // "device_info" => $user->id,
  99. // "nonce_str" => Str::random(),
  100. // "openid" => $user->open_id,
  101. // "body" => "小程序购买钻石次数",
  102. // "out_trade_no"=> $order->order,
  103. // "total_fee"=> $order->amount,
  104. // "spbill_create_ip" => "47.93.12.114",
  105. // // "time_start"=> $order->created_at->format('YmdHis'),
  106. // // "time_expire"=> $order->created_at->addMinutes(30)->format('YmdHis'),
  107. // "notify_url"=> 'https://t18.9026.com/api/wechat/pay/aiCallback',
  108. // "trade_type"=> 'JSAPI',
  109. // "detail"=> json_encode(['goods_detail'=> []])
  110. // ];
  111. // $params['sign'] = (new LegacySignature($this->merchant))->sign($params);
  112. // $response = $api->request('POST', '/pay/unifiedorder', [
  113. // 'xml' => $params
  114. // ]);
  115. return $result;
  116. }
  117. public static function order($order, $user)
  118. {
  119. $app = self::pay();
  120. $api = $app->getClient();
  121. $config = $app->getConfig();
  122. $detail = [];
  123. foreach ($order->shops as $key => $value) {
  124. $money = $value->price;
  125. $detail[] = [
  126. 'goods_id' => $value->id,
  127. 'wxpay_goods_id' => $value->id,
  128. 'goods_name' => $value->material->type_name,
  129. 'quantity' => 1,
  130. 'price' => $money,
  131. 'goods_category' => $value->material->type_name,
  132. 'body' => $value->material->type_name . '-' . $value->material->material_name,
  133. ];
  134. }
  135. $params = [
  136. 'appid' => $config['app_id'],
  137. 'mch_id' => $config['mch_id'],
  138. 'device_info' => $user->id,
  139. 'nonce_str' => Str::random(),
  140. 'openid' => $user->mini_openid,
  141. 'body' => '小程序购买商品',
  142. 'out_trade_no' => $order->out_trade_no,
  143. 'total_fee' => $order->amount,
  144. 'spbill_create_ip' => '47.93.12.114',
  145. // "time_start"=> $order->created_at->format('YmdHis'),
  146. // "time_expire"=> $order->created_at->addMinutes(30)->format('YmdHis'),
  147. 'notify_url' => 'https://t28.9026.com/api/wechat/pay/callback',
  148. 'trade_type' => 'JSAPI',
  149. 'detail' => json_encode(['goods_detail' => $detail]),
  150. ];
  151. // $params['sign'] = (new LegacySignature($this->merchant))->sign($params);
  152. $response = $api->request('POST', '/pay/unifiedorder', [
  153. 'xml' => $params,
  154. ]);
  155. return $response->toArray(false);
  156. }
  157. public static function queryOrder($out_trade_no)
  158. {
  159. $app = self::pay();
  160. $api = $app->getClient();
  161. $config = $app->getConfig();
  162. $params = [
  163. 'appid' => $config['app_id'],
  164. 'mch_id' => $config['mch_id'],
  165. 'out_trade_no' => $out_trade_no,
  166. 'nonce_str' => Str::random(),
  167. ];
  168. $response = $api->request('POST', '/pay/orderquery', [
  169. 'xml' => $params,
  170. ]);
  171. return $response->toArray(false);
  172. }
  173. public static function build_order_no()
  174. {
  175. return date('Ymd') . substr(implode('', array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
  176. }
  177. }