easywechat6 v3支付 JSAPI下单示例+流程

Achun

提交订单示例代码

use EasyWeChat\Pay\Application;
use WebmanTech\Logger\Logger;

public function submit(Request $request)
{
    $params = OrderSubmitRequest::validate($request->all());

    $lock_name = 'order:submit:' . $request->uid;
    $lock = Redis::setEx($lock_name, 3, 1);
    if (!$lock) return fail('请勿重复提交');

    $price = Config::cacheValue('fee_money');
    $data['price'] = $price;
    $data['order_sn'] = 'WA' . get_order_sn();
    $data['uid'] = $request->uid;
    $data['type'] = 1;
    $res = Order::create($data);
    if (!$res) return fail('创建订单失败');

    $config = config('wechat.payment.default');
    $order = [
        "mchid" => $config['mch_id'],
        "out_trade_no" => $data['order_sn'],
        "appid" => $config['app_id'],
        "description" => '微信支付测试',
        "notify_url" => 'https://'.request()->host() . "/xxx/callback", // 支付成功后回调通知URL
        "amount" => [
            "total" => (int)bcmul($price,100),
            "currency" => "CNY",
        ],
        "payer" => [
            "openid" => $request->user->openid,
        ],
    ];

    try {
        $app = new Application($config);
        $res = $app->getClient()->postJson("v3/pay/transactions/jsapi", $order);
        $res = $res->toArray();
    } catch (\Throwable $e) {
        Logger::info($e->getMessage());
        return fail($e->getMessage());
    }

    if (!isset($res['prepay_id'])) {
        Logger::info($res);
        return fail('获取prepay_id失败');
    }

    $prepayId = $res['prepay_id'];
    $utils = $app->getUtils();
    $config = $utils->buildMiniAppConfig($prepayId, $config['app_id'], 'RSA');
    Redis::del($lock_name);
    return success($config);
}

微信支付配置

截图

微信商户平台申请商户证书以及v3_api key

截图

微信支付V3,平台证书(EasyWechat的platform_certs参数)下载

https://learnku.com/laravel/t/68840

查询微信商户序列号

openssl x509 -in apiclient_cert.pem -noout -serial

190 0 1
0个评论

Achun

320
积分
0
获赞数
0
粉丝数
2023-12-04 加入
×
🔝