收藏
回答

支付时如何使用微信支付公钥呢?有参考代码吗?

微信支付 新申请的商户号只能用 微信支付公钥了,平台证书不能用了。支付时如何使用微信支付公钥呢?有参考代码吗?php的参考代码

最后一次编辑于  05-22
回答关注问题邀请回答
收藏

2 个回答

  • 微信支付质量运营助手
    微信支付质量运营助手
    05-23

    微信SDK已支持公钥方案,详情请了解下文档https://github.com/wechatpay-apiv3/wechatpay-php

    05-23
    有用
    回复
  • 北望沣渭
    北望沣渭
    05-23

    初始化

    请按照你的商户号所能接入的方式选择对应实例化客户端

    require_once('vendor/autoload.php');
    
    
    use WeChatPay\Builder;
    use WeChatPay\Crypto\Rsa;
    
    
    // 商户号
    $merchantId = '190000****';
    
    
    // 从本地文件中加载「商户API私钥」,用于生成请求的签名
    $merchantPrivateKeyFilePath = 'file:///path/to/merchant/apiclient_key.pem';
    $merchantPrivateKeyInstance = Rsa::from($merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);
    
    
    // 「商户API证书」的「证书序列号」
    $merchantCertificateSerial = '3775B6A45ACD588826D15E583A95F5DD********';
    
    
    // 从本地文件中加载「微信支付公钥」,用来验证微信支付应答的签名
    $platformPublicKeyFilePath = 'file:///path/to/wechatpay/publickey.pem';
    $platformPublicKeyInstance = Rsa::from($platformPublicKeyFilePath, Rsa::KEY_TYPE_PUBLIC);
    
    
    // 「微信支付公钥」的「微信支付公钥ID」
    // 需要在 商户平台 -> 账户中心 -> API安全 查询
    $platformPublicKeyId = 'PUB_KEY_ID_01142321349124100000000000********';
    
    
    // 构造一个 APIv3 客户端实例(微信支付公钥模式)
    $instance = Builder::factory([
      'mchid'      => $merchantId,
      'serial'     => $merchantCertificateSerial,
      'privateKey' => $merchantPrivateKeyInstance,
      'certs'      => [
        $platformPublicKeyId => $platformPublicKeyInstance,
      ]
    ]);
    
    

    Native下单

    $instance->v3->pay->transactions->native->postAsync([
      'json' => [
        'appid'          => 'wxd678efh567hg6787',
        'mchid'          => '1230000109',
        'description'    => 'Image形象店-深圳腾大-QQ公仔',
        'attach'         => '自定义数据',
        'out_trade_no'   => '1217752501201407033233368018',
        'amount'         => [
          'total'    => 100,
          'currency' => 'CNY',
        ],
        'time_expire'    => '2018-06-08T10:34:56+08:00',
        'notify_url'     => 'https://www.weixin.qq.com/wxpay/pay.php',
        'goods_tag'      => 'WXG',
        'limit_pay'      => ['no_balance'],
        'support_fapiao' => true,
        'detail'         => [
          'cost_price'   => 608800,
          'invoice_id'   => '微信123',
          'goods_detail' => [[
            'merchant_goods_id'  => '商品编码',
            'wechatpay_goods_id' => '1001',
            'goods_name'         => 'iPhoneX 256G',
            'quantity'           => 1,
            'unit_price'         => 828800,
          ],],
        ],
        'scene_info'     => [
          'payer_client_ip' => '14.23.150.211',
          'device_id'       => '013467007045764',
          'store_info'      => [
            'id'        => '0001',
            'name'      => '腾讯大厦分店',
            'area_code' => '440305',
            'address'   => '广东省深圳市南山区科技中一道10000号',
          ],
        ],
        'payer'          => [
          'identity' => [
            'type'   => 'IDCARD',
            'number' => '6B46824C852FA29AAC3DCE6BFD852E27',
            'name'   => '6B46824C852FA29AAC3DCE6BFD852E27',
          ],
        ],
        'settle_info'    => [
          'profit_sharing' => true,
        ],
      ],
      'headers' => [
        'Wechatpay-Serial' => 'PUB_KEY_ID_0114232134912410000000000000',
      ],
    ])
    ->then(static function(\Psr\Http\Message\ResponseInterface $response) {
      print_r(json_decode((string) $response->getBody(), true));
    })
    ->wait();
    

    摘自 https://wechatpay.im/guide/getting-startedhttps://wechatpay.im/guide/getting-started

    05-23
    有用
    回复
登录 后发表内容