Wechat.php 7.2 KB

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