评论

香港及海外接入点,使用 wechatpay-php 支付开发包 basic guideline

作为 guideline,简述一下非大陆区使用 wechatpay/wechatpay 开发包的step1.2.3

前置条件

已有商户号、商户私钥、商户证书、APIV3密钥,通过此包内置的 平台证书下载器,已取得平台证书。

安装软件

composer require wechatpay/wechatpay

WeChatPay\Builder::factory 工厂方法

在工厂方法内,只需传入指定的接入点base_uri即可支持,例如:

$instance = \WeChatPay\Builder::factory([
    'base_uri' => 'https://api.mch.weixin.qq.com/hk/',
    'mchid' => '1234567',
    'serial' => '7654321',
    'privateKey' => \WeChatPay\Util\PemUtil::loadPrivateKey('/path/to/apiclient_key.pem'),
    'certs' => [
        '7890123' => \WeChatPay\Util\PemUtil::loadCertificate('/path/to/wechatpay_7890123.pem'),
    ]
]);

融合钱包-请求下单

$instance->v3->pay->transactions->micropay
->postAsync(['json' => [
    'mchid' => '1234567',
    'appid' => 'wx8888888888888888',
    'out_trade_no' => 'your_out_trade_no',
    'trade_type' => 'MICROPAY',
    'description' => 'QQ Doll',
    'amount' => [
        'total' => 528800,
        'currency' => 'HKD',
    ]
]])
->then(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some successful procedure
})
->otherwise(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some fail procedure
});

融合钱包-查单

$instance->v3->pay->transactions->transaction_id->'{transaction_id}'
->getAsync([
    'transaction_id' => 'your_achived_transaction_id',
])
->then(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some successful procedure
})
->otherwise(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some fail procedure
});

OR

$instance->v3->pay->transactions->outTradeNo->'{out_trade_no}'
->getAsync([
    'out_trade_no' => 'your_out_trade_no',
])
->then(function(\Psr\Http\Message\ResponseInterface) {
    // do some successful procedure
})
->otherwise(function(\Psr\Http\Message\ResponseInterface) {
    // do some fail procedure
});

融合钱包-撤销订单

$instance->v3->pay->transactions->transaction_id->'{transaction_id}'->reverse
->postAsync([
    'transaction_id' => 'your_achived_transaction_id',
])
->then(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some successful procedure
})
->otherwise(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some fail procedure
});

OR

$instance->v3->pay->transactions->outTradeNo->'{out_trade_no}'->reverse
->postAsync([
    'out_trade_no' => 'your_out_trade_no',
])
->then(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some successful procedure
})
->otherwise(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some fail procedure
});

融合钱包-退款

$instance->v3->refunds->postAsync(['json' => [
    'mchid' => '1234567',
    'appid' => 'wx8888888888888888',
    'out_trade_no' => 'your_out_trade_no',
    'out_refund_no' => 'your_out_refund_no',
    'trade_type' => 'MICROPAY',
    'description' => 'QQ Doll',
    'amount' => [
        'refund' => 528800,
        'total' => 528800,
        'currency' => 'HKD',
    ]
]])
->then(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some successful procedure
})
->otherwise(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some fail procedure
});

融合钱包-查询单笔退款

$instance->v3->refunds->transaction_id->'{transaction_id}'
->getAsync([
    'transaction_id' => 'your_achived_transaction_id',
])
->then(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some successful procedure
})
->otherwise(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some fail procedure
});

OR

$instance->v3->refunds->outTradeNo->'{out_trade_no}'
->getAsync([
    'out_trade_no' => 'your_out_trade_no',
])
->then(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some successful procedure
})
->otherwise(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some fail procedure
});

融合钱包-查询所有退款

$instance->v3->refunds->getAsync(['query' => [
    'mchid' => '1234567',
    'offset' => 0,
    'count' => 20,
]])
->then(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some successful procedure
})
->otherwise(function(\Psr\Http\Message\ResponseInterface $response) {
    // do some fail procedure
});

最后

对照着文档,写代码就是如此简单。。。

Repo: https://github.com/TheNorthMemory/wechatpay-php 欢迎 Star .

最后一次编辑于  2021-06-29  
点赞 5
收藏
评论

3 个评论

  • YT春雨
    YT春雨
    2021-10-23

    public function actionCancelOrder(){

    $instance->v3->pay->transactions->transaction_id->'{transaction_id}'->reverse

    ->postAsync([

    'transaction_id' => 'your_achived_transaction_id',

    ])

    ->then(function(\Psr\Http\Message\ResponseInterface $response) {

    // do some successful procedure

    //我如果要封装成函数,这里该如何写返回值给调用者呢

    return ?????

    })

    ->otherwise(function(\Psr\Http\Message\ResponseInterface $response) {

    // do some fail procedure

    });

    }

    2021-10-23
    赞同
    回复 1
    • 北望沣渭
      北望沣渭
      2021-10-25
      我是这么用的,供参考:
      2021-10-25
      回复
  • 青寒
    青寒
    发表于移动端
    2021-06-29
    技术狂人啊!
    2021-06-29
    赞同
    回复
  • 老张
    老张
    2021-06-25

    nodejs的有吗?

    2021-06-25
    赞同
    回复 1
    • 北望沣渭
      北望沣渭
      2021-06-25
      wechatpay-axios-plugin 几乎是一样用法
      2021-06-25
      回复
登录 后发表内容