前置条件
已有商户号、商户私钥、商户证书、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 .
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
});
}
<?php
function trans() {
return static function(\Psr\Http\Message\ResponseInterface $response) {
return (array)\json_decode($response->getBody()->getContents(), true);
};
}
$res = $instance->v3
->pay->transactions
->transaction_id->'{transaction_id}'
->reverse
->postAsync([
'transaction_id' => 'your_achived_transaction_id',
])
->then(trans())
->wait();
print_r($res);
nodejs的有吗?