1、微信领取分介绍及守旧
- 产物介绍:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_0.shtml
- 接入前预备:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_1.shtml
- 测试号设置装备摆设:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_5.shtml
2、免确认形式开辟
参考网址:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_3.shtml
- 步调1 用户正在商户侧下单购置产物或者效劳,此时,咱们需求先对于用户的受权形态停止查问
- 步调2 领导用户开启受权效劳
- 步调3 创立领取分定单
- 步调4 商户为用户供给效劳,待效劳完毕后,商户挪用结束定单接口结束以后定单。
- 步调5 收到用户扣款成功告诉,营业流程完毕
3、SDK相关
- 民间文档:https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay6_0.shtml
- wechatpay-php(推选):https://github.com/wechatpay-apiv3/wechatpay-php
4、代码示例
/**
* Notes: 步调1 用户正在商户侧下单购置产物或者效劳,此时,咱们需求先对于用户的受权形态停止查问
* User: XXX
* DateTime: 2021/7/27 9:59
*/
public function getAuthStatus(string $cid)
{
$openid = $this->getOpenid($cid);
if (!$openid) {
return false;
}
try {
$resp = $this->instance->v3->payscore->permissions->openid->{'{openid}'}
->get(
[
'query' => [
'appid' => $this->appid,
'service_id' => $this->serviceId,
],
// uri_template 字面量参数
'openid' => $openid,
]
);
$res = json_decode($resp->getBody()->getContents(), true);
if ($res['authorization_state'] == 'AVAILABLE') {
return true;
} else {
return false;
}
} catch (\\Exception $e) {
return false;
/*echo($e->getResponse()->getStatusCode());
// 停止过错处置
echo $e->getMessage()->getReasonPhrase(), PHP_EOL;
if ($e instanceof \\Psr\\Http\\Message\\ResponseInterface && $e->hasResponse()) {
echo $e->getResponse()->getStatusCode() . ' ' . $e->getResponse()->getReasonPhrase(), PHP_EOL;
echo $e->getResponse()->getBody();
}*/
}
}
/**
* Notes:步调2 领导用户开启受权效劳-获得预受权码
* User: XXX
* DateTime: 2021/7/27 18:37
*/
public function openAuthStatus()
{
try {
$resp = $this->instance->v3->payscore->permissions->post(
[
'json' => [
'service_id' => $this->serviceId,
'appid' => $this->appid,
'authorization_code' => $this->getRandStr(12), // 受权和谈号,相似定单号
//'notify_url' => 'https://weixin.qq.com/',
]
]
);
$res = json_decode($resp->getBody(), true);
return $res['apply_permissions_token'];
} catch (\\Exception $e) {
// 停止过错处置
/*if ($e->hasResponse()) {
echo $e->getResponse()->getBody();
}*/
return false;
}
}
/**
* Notes: 步调3 创立领取分定单
* User: xxx
* DateTime: 2021/7/27 19:21
* @param string $cid 用户ID
* @param string $orderSn 定单号
*/
public function makeOrder(string $cid, string $orderSn)
{
// 定单信息
....
$openid = $this->getOpenid($cid);
if (!$openid) {
return [
'code' => -1,
'msg' => 'openid不成觉得空',
];
}
// 异步告诉地点,偶然候发明莫名的酿成了localhost,这里先牢固
$notiryUrl = route('api.v1.wxpayPointsNotify');
$json = [
'out_order_no' => $orderSn, // 商户效劳定单号
'appid' => $this->appid, // 使用ID
'service_id' => $this->serviceId, // 效劳ID
'service_introduction' => '换电用度', // 效劳信息,用于介绍本定单所供给的效劳 ,当参数长度超越20个字符时,报错处置
'time_range' => [
'start_time' => $startTime, //'20210729160710',
],
'risk_fund' => [
'name' => 'ESTIMATE_ORDER_COST', // 危害金称号
'amount' => 300, // 危害金额 数字,必需>0(单元分)
],
'attach' => $orderSn,// 商户数据包
'notify_url' => $notiryUrl,
'openid' => $openid,// 用户标识
'need_user_confirm' => false,// 能否需求用户确认
];
try {
$resp = $this->instance->v3->payscore->serviceorder->post(
[
'json' => $json
]
);
$res = json_decode($resp->getBody(), true);
// 入库领取分定单
...
return [
'code' => 0,
'msg' => '领取分定单创立实现',
];
} catch (\\Exception $e) {
// 停止过错处置
if ($e->hasResponse()) {
$body = $e->getResponse()->getBody();
if ($body) {
return [
'code' => -1,
'msg' => (string)$body,
];
}
}
return '';
}
}
结束领取分定单、撤消领取分定单、查问领取分定单 相似,这里再也不写进去。
/**
* Notes: 异步告诉
* User: XXX
* DateTime: 2021/8/3 14:20
*/
public function notify()
{
// 获得前往的信息
$responseBody = file_get_contents("php://input");
$responseArr = json_decode($responseBody, true);
if ($responseArr) {
$res = AesGcm::decrypt($responseArr['resource']['ciphertext'], 'xxxapi密钥', $responseArr['resource']['nonce'], $responseArr['resource']['associated_data']);
$resArr = json_decode($res, true);
if ($resArr) {
// 记载日记
...
// 营业逻辑处置
...
// 定单日记记载
...
} else {
return [
'code' => -1,
'msg' => '剖析有误',
];
}
} else {
return [
'code' => -1,
'msg' => 'nothing post',
];
}
}
5、留意事变
- 严厉遵照文档中的参数请求,呈现成绩第临时间比拟传入参数以及民间示例的差别
-
领取分定单必需撤消,或者结束
推选进修:《PHP视频教程》
以上便是一文详解PHP若何接入微信领取分(代码示例)的具体内容,更多请存眷酷吧易资源网别的相关文章!
有用么