123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- declare(strict_types=1);
- namespace App\libs\wechat\pay;
- // use EasyWeChat\MiniProgram\Application;
- // use EasyWeChat\MiniApp\Application;
- // use EasyWeChat\Pay\Application as PayApplication;
- use EasyWeChat\Payment\Application;
- use EasyWeChat\Kernel\Support\Str;
- // use EasyWeChat\Payment\LegacySignature;
- // use GuzzleHttp\Client;
- // use Hyperf\HttpServer\Contract\RequestInterface;
- // use Hyperf\Utils\ApplicationContext;
- use Illuminate\Http\Request;
- use EasyWeChat\Factory;
- class Wechat
- {
- // public static function app() {
- // $config = [
- // 'app_id' => 'wx93e902f62d243388',
- // 'secret' => '80d6e14f55d0593a53d131e93215ea20',
- // // 'oauth' => [
- // // 'scopes' => ['snsapi_userinfo'],
- // // 'callback' => '/oauth/callback',
- // // ],
- //
- // // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
- // 'response_type' => 'array',
- //
- // //...
- // ];
- //
- // return new Application($config);
- // }
- public static function pay()
- {
- $arr = config('swdz.wechatPay');
- $config = [
- // 必要配置
- 'app_id' => $arr['appid'],
- 'mch_id' => $arr['mch_id'],
- 'key' => $arr['key'], // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
- // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
- 'cert_path' => __DIR__ . '/ssl/apiclient_cert.pem', // XXX: 绝对路径!!!!
- 'key_path' => __DIR__ . '/ssl/apiclient_key.pem', // XXX: 绝对路径!!!!
- 'notify_url' => 'https://hb.swdz.com/api/wechat/pay/aiCallback', // 你也可以在下单时单独设置来想覆盖它
- ];
- $app = Factory::payment($config);
- // $config = [
- // 'mch_id' => 1536361101,
- // 'app_id' => 'wx93e902f62d243388',
- // // 商户证书
- // 'private_key' => __DIR__ . '/ssl/apiclient_key.pem',
- // 'certificate' => __DIR__ . '/ssl/apiclient_cert.pem',
- //
- // // v3 API 秘钥
- // 'secret_key' => '43A03299A3C3FED3D8CE7B820Fxxxxx',
- //
- // // v2 API 秘钥
- // 'v2_secret_key' => 'pfbrMsAU5v9OFvJdqrg1c8YSIdtdPl8Z',
- //
- // // 平台证书:微信支付 APIv3 平台证书,需要使用工具下载
- // // 下载工具:https://github.com/wechatpay-apiv3/CertificateDownloader
- // 'platform_certs' => [
- // // 请使用绝对路径
- // // '/path/to/wechatpay/cert.pem',
- // ],
- //
- // /**
- // * 接口请求相关配置,超时时间等,具体可用参数请参考:
- // * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php
- // */
- // 'http' => [
- // 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
- // 'timeout' => 5.0,
- // // 'base_uri' => 'https://api.mch.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
- // ]
- // ];
- // var_dump(__DIR__ . '/ssl/apiclient_key.pem');
- // $app = new Application($config);
- return $app;
- }
- public static function addOrder($order, $user)
- {
- $result = self::pay()->order->unify([
- 'body' => '小程序购买钻石次数',
- 'device_info' => $user->id,
- 'nonce_str' => Str::random(),
- 'out_trade_no' => $order->order,
- 'total_fee' => $order->amount * 10000,
- 'spbill_create_ip' => '47.93.12.114', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
- 'notify_url' => 'https://hb.swdz.com/api/wechat/pay/aiCallback', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
- 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
- 'openid' => $user->open_id,
- ]);
- // $params = [
- // "appid" => $config['app_id'],
- // "mch_id" => $config['mch_id'],
- // "device_info" => $user->id,
- // "nonce_str" => Str::random(),
- // "openid" => $user->open_id,
- // "body" => "小程序购买钻石次数",
- // "out_trade_no"=> $order->order,
- // "total_fee"=> $order->amount,
- // "spbill_create_ip" => "47.93.12.114",
- // // "time_start"=> $order->created_at->format('YmdHis'),
- // // "time_expire"=> $order->created_at->addMinutes(30)->format('YmdHis'),
- // "notify_url"=> 'https://t18.9026.com/api/wechat/pay/aiCallback',
- // "trade_type"=> 'JSAPI',
- // "detail"=> json_encode(['goods_detail'=> []])
- // ];
- // $params['sign'] = (new LegacySignature($this->merchant))->sign($params);
- // $response = $api->request('POST', '/pay/unifiedorder', [
- // 'xml' => $params
- // ]);
- return $result;
- }
- public static function order($order, $user)
- {
- $app = self::pay();
- $api = $app->getClient();
- $config = $app->getConfig();
- $detail = [];
- foreach ($order->shops as $key => $value) {
- $money = $value->price;
- $detail[] = [
- 'goods_id' => $value->id,
- 'wxpay_goods_id' => $value->id,
- 'goods_name' => $value->material->type_name,
- 'quantity' => 1,
- 'price' => $money,
- 'goods_category' => $value->material->type_name,
- 'body' => $value->material->type_name . '-' . $value->material->material_name,
- ];
- }
- $params = [
- 'appid' => $config['app_id'],
- 'mch_id' => $config['mch_id'],
- 'device_info' => $user->id,
- 'nonce_str' => Str::random(),
- 'openid' => $user->mini_openid,
- 'body' => '小程序购买商品',
- 'out_trade_no' => $order->out_trade_no,
- 'total_fee' => $order->amount,
- 'spbill_create_ip' => '47.93.12.114',
- // "time_start"=> $order->created_at->format('YmdHis'),
- // "time_expire"=> $order->created_at->addMinutes(30)->format('YmdHis'),
- 'notify_url' => 'https://t28.9026.com/api/wechat/pay/callback',
- 'trade_type' => 'JSAPI',
- 'detail' => json_encode(['goods_detail' => $detail]),
- ];
- // $params['sign'] = (new LegacySignature($this->merchant))->sign($params);
- $response = $api->request('POST', '/pay/unifiedorder', [
- 'xml' => $params,
- ]);
- return $response->toArray(false);
- }
- public static function queryOrder($out_trade_no)
- {
- $app = self::pay();
- $api = $app->getClient();
- $config = $app->getConfig();
- $params = [
- 'appid' => $config['app_id'],
- 'mch_id' => $config['mch_id'],
- 'out_trade_no' => $out_trade_no,
- 'nonce_str' => Str::random(),
- ];
- $response = $api->request('POST', '/pay/orderquery', [
- 'xml' => $params,
- ]);
- return $response->toArray(false);
- }
- public static function build_order_no()
- {
- return date('Ymd') . substr(implode('', array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
- }
- }
|