PHP code example of qinii / wechat-payment

1. Go to this page and download the library: Download qinii/wechat-payment library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

qinii / wechat-payment example snippets


use Qinii\WechatPayment\Enum\ApiVersion;

$config = [
    'default' => [
        'official_account' => [
            'app_id' => 'wx-official-account-appid',
        ],
        'mini_program' => [
            'app_id' => 'wx-mini-program-appid',
        ],
        'app' => [
            'app_id' => 'wx-app-appid',
        ],

        // 账号组级 HTTP 配置会与 payment/partner.http 合并。
        'http' => [
            'http_version' => '1.1',
            'timeout' => 5,
        ],

        'payment' => [
            'version' => ApiVersion::V3,
            'mch_id' => '1900000001',
            'notify_url' => 'https://merchant.example.com/wechat/notify',
            'default_application' => 'official_account',
            'spbill_create_ip' => '203.0.113.10',

            // API V2。
            'v2_secret_key' => 'APIv2-key',

            // API V3。private_key 是商户私钥,certificate 是商户证书。
            'secret_key' => '32-byte-APIv3-key',
            'private_key' => '/absolute/path/apiclient_key.pem',
            'certificate' => '/absolute/path/apiclient_cert.pem',
            'serial_no' => 'MERCHANT_CERTIFICATE_SERIAL_NO',

            // 微信支付公钥模式,和平台证书模式二选一即可。
            'public_key_id' => 'PUB_KEY_ID_...',
            'public_key' => '/absolute/path/wechatpay_pubkey.pem',
            'platform_certs' => [
                // 'PLATFORM_CERT_SERIAL_NO' => '/absolute/path/wechatpay_cert.pem',
            ],
            'platform_certs_cache_dir' => 'storage/wechat-payment/platform-certs',

            // H5 默认场景,可由 DTO 中的 scene_info 覆盖。
            'h5' => [
                'type' => 'Wap',
                'wap_url' => 'https://merchant.example.com',
                'wap_name' => 'Merchant',
            ],
        ],

        'partner' => [
            'version' => ApiVersion::V3,
            // 服务商配置仍使用统一字段名;请求时会映射为 sp_mchid/sp_appid。
            'mch_id' => '1900000099',
            'notify_url' => 'https://merchant.example.com/wechat/partner-notify',
            'default_application' => 'official_account',
            'spbill_create_ip' => '203.0.113.10',
            'v2_secret_key' => 'partner-APIv2-key',
            'secret_key' => '32-byte-partner-APIv3-key',
            'private_key' => '/absolute/path/partner_apiclient_key.pem',
            'certificate' => '/absolute/path/partner_apiclient_cert.pem',
            'serial_no' => 'PARTNER_CERTIFICATE_SERIAL_NO',
            'public_key_id' => 'PUB_KEY_ID_...',
            'public_key' => '/absolute/path/wechatpay_pubkey.pem',
            'platform_certs' => [],
            //平台证书模式,和微信支付公钥模式二选一即可,此字段是证书模式存放位置,可自定义路径
            'platform_certs_cache_dir' => 'storage/wechat-payment/platform-certs',
        ],
    ],
];

use Qinii\WechatPayment\Payment;

$payment = Payment::create($config);

$defaultPayment = Payment::create($config);
$shopPayment = $defaultPayment->account('shop_2');

use Qinii\WechatPayment\Enum\PayType;
use Qinii\WechatPayment\Payment\PaymentDto;

$result = $payment->payment()->create(PaymentDto::fromArray([
    'description' => '订单 202609010001',
    'out_trade_no' => 'ORDER_202609010001',
    'total_fee' => 100, // 单位:分
    'pay_type' => PayType::MINI_PROGRAM,
    'openid' => 'user-openid',
]));

use Qinii\WechatPayment\Enum\PayType;
use Qinii\WechatPayment\Partner\PartnerDto;

$result = $payment->partner()->create(PartnerDto::fromArray([
    'sub_mchid' => '1900000002',
    'description' => '服务商订单 202609010001',
    'out_trade_no' => 'PARTNER_202609010001',
    'total_fee' => 100,
    'pay_type' => PayType::OFFICIAL,
    'openid' => 'openid-under-sp-appid',
]));

// 普通商户。
$order = $payment->payment()->queryByOutTradeNo('ORDER_202609010001');
$order = $payment->payment()->queryByTransactionId('420000000000000001');
$payment->payment()->close('ORDER_202609010001');

// 服务商。
$order = $payment->partner()->queryByOutTradeNo(
    'PARTNER_202609010001',
    '1900000002',
);
$payment->partner()->close('PARTNER_202609010001', '1900000002');

use Qinii\WechatPayment\Partner\PartnerRefundDto;
use Qinii\WechatPayment\Payment\PaymentRefundDto;

$result = $payment->payment()->refund(PaymentRefundDto::fromArray([
    'out_trade_no' => 'ORDER_202609010001',
    'out_refund_no' => 'REFUND_202609010001',
    'total_fee' => 100,
    'refund_fee' => 100,
]));

$result = $payment->partner()->refund(PartnerRefundDto::fromArray([
    'sub_mchid' => '1900000002',
    'out_trade_no' => 'PARTNER_202609010001',
    'out_refund_no' => 'PARTNER_REFUND_202609010001',
    'total_fee' => 100,
    'refund_fee' => 100,
]));

use Qinii\WechatPayment\Transfer\TransferDto;

$result = $payment->transfer()->application('mini_program')->create(
    TransferDto::fromArray([
        'out_bill_no' => 'TRANSFER202609010001',
        'transfer_scene_id' => '1000',
        'openid' => 'openid-under-mini-program-appid',
        'transfer_amount' => 100,
        'transfer_remark' => '订单结算',
        'transfer_scene_report_infos' => [
            ['info_type' => '活动名称', 'info_content' => '订单结算'],
        ],
    ]),
);

use Qinii\WechatPayment\Enum\PayMode;

// 普通商户支付通知。
$notification = $payment->notify()->payment($psrRequest);

// 服务商支付通知必须显式选择 partner 配置。
$notification = $payment->notify()->payment(
    $psrRequest,
    PayMode::PARTNER,
);

$data = $notification->data();

try {
    // 校验订单金额,并按商户订单号做幂等处理。
    return $notification->success();
} catch (Throwable $exception) {
    return $notification->fail($exception->getMessage());
}

use Qinii\WechatCore\Http\SwooleHttpClient;

$payment->setHttpClient(new SwooleHttpClient());

use Qinii\WechatPayment\Exception\InvalidConfigException;
use Qinii\WechatPayment\Exception\PaymentException;
use Qinii\WechatPayment\Exception\UnsupportedModeException;

try {
    $result = $payment->payment()->create($dto);
} catch (InvalidConfigException $exception) {
    // 配置、证书路径或密钥错误。
} catch (UnsupportedModeException $exception) {
    // 当前 API 版本或业务模式不支持该操作。
} catch (PaymentException $exception) {
    // DTO 校验、签名验签、微信接口或响应解析错误。
}