3.2. API接入(含示例代码)
本文档展示了如何使用微信支付服务端 SDK 快速接入H5支付产品,完成与微信支付对接的部分。
注意:
文档中的代码示例是用来阐述 API 基本使用方法,代码中的示例参数需替换成商户自己账号及请求参数才能跑通。
以下接入步骤仅提供参考,请商户结合自身业务需求进行评估、修改。
3.2.1. 【服务端】预受理领卡请求
步骤说明:商户在引导用户跳转先享卡领卡前,需要先请求预受理领卡请求接口,再根据返回数据引导用户跳转领卡。
示例代码
public void PrepareCard() throws Exception{
//请求URL
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/discount-card/cards");
// 请求body参数
String reqdata = "{"
+ "\"out_card_code\":\"6e8369071cd942c0476613f9d1ce9ca3\","
+ "\"card_template_id\":\"87789b2f25177433bcbf407e8e471f95\","
+ "\"appid\":\"wxd678efh567hg6787\","
+ "\"notify_url\":\"https://api.test.com\""
+ "}";
StringEntity entity = new StringEntity(reqdata);
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
//完成签名并执行请求
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) { //处理成功
System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
} else if (statusCode == 204) { //处理成功,无返回Body
System.out.println("success");
} else {
System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
throw new IOException("request failed");
}
} finally {
response.close();
}
}
try {
$resp = $client->request(
'POST',
'https://api.mch.weixin.qq.com/v3/discount-card/cards', //请求URL
[
// JSON请求体
'json' => [
"out_card_code" => "6e8369071cd942c0476613f9d1ce9ca3",
"card_template_id" => "87789b2f25177433bcbf407e8e471f95",
"appid" => "wxd678efh567hg6787",
"notify_url" => "https://api.test.com",
],
'headers' => [ 'Accept' => 'application/json' ]
]
);
$statusCode = $resp->getStatusCode();
if ($statusCode == 200) { //处理成功
echo "success,return body = " . $resp->getBody()->getContents()."\n";
} else if ($statusCode == 204) { //处理成功,无返回Body
echo "success";
}
} catch (RequestException $e) {
// 进行错误处理
echo $e->getMessage()."\n";
if ($e->hasResponse()) {
echo "failed,resp code = " . $e->getResponse()->getStatusCode() . " return body = " . $e->getResponse()->getBody() . "\n";
}
return;
}
更多参数、响应详情及错误码请参见预受理领卡请求API接口文档
3.2.2. 【客户端】APP拉起先享卡小程序领卡API
通过预受理领卡请求API拿到拉起先享卡领卡页面的必传参数“预领卡请求token”,然后我们就可以通过前端方法来拉起先享卡领卡页面了。前端方法详见APP拉起先享卡小程序领卡
3.2.3. 【服务端】用户领卡通知API
步骤说明:当用户领取先享卡成功时,微信会把相关信息异步回调的方式通知商户,商户需要接收处理,并按文档规范返回应答
注意:
支付结果通知是以POST 方法访问商户设置的通知url,通知的数据以JSON 格式通过请求主体(BODY)传输。通知的数据包括了加密的支付结果详情
加密不能保证通知请求来自微信。微信会对发送给商户的通知进行签名,并将签名值放在通知的HTTP头Wechatpay-Signature。商户应当验证签名,以确认请求来自微信,而不是其他的第三方。签名验证的算法请参考微信支付API v3签名方案
支付通知http应答码为200或204才会当作正常接收,当回调处理异常时,应答的HTTP状态码应为500,或者4xx
商户成功接收到回调通知后应返回成功的http应答码为200或204
同样的通知可能会多次发送给商户系统。商户系统必须能够正确处理重复的通知。 推荐的做法是,当商户系统收到通知进行处理时,先检查对应业务数据的状态,并判断该通知是否已经处理。如果未处理,则再进行处理;如果已处理,则直接返回结果成功。在对业务数据进行状态检查和处理之前,要采用数据锁进行并发控制,以避免函数重入造成的数据混乱
如果在所有通知频率(4小时)后没有收到微信侧回调,商户应调用查询先享卡订单接口确认订单状态。
更多参数、响应详情及错误码请参见 用户领卡通知API接口文档
3.2.4. 【服务端】查询先享卡订单API
步骤说明:商户长时间没有收到通知,可通过查询接口主动查询订单状态。
示例代码:
public void QueryCard() throws Exception{
//请求URL
HttpGet httpGet = new HttpGet("https://api.mch.weixin.qq.com/v3/discount-card/cards/233bcbf407e87789b8e471f251774f95");
HttpGet.setHeader("Accept", "application/json");
//完成签名并执行请求
CloseableHttpResponse response = httpClient.execute(HttpGet);
try {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) { //处理成功
System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
} else if (statusCode == 204) { //处理成功,无返回Body
System.out.println("success");
} else {
System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
throw new IOException("request failed");
}
} finally {
response.close();
}
}
try {
$resp = $client->request(
'GET',
'https://api.mch.weixin.qq.com/v3/discount-card/cards/233bcbf407e87789b8e471f251774f95', //请求URL
[
'headers' => [ 'Accept' => 'application/json']
]
);
$statusCode = $resp->getStatusCode();
if ($statusCode == 200) { //处理成功
echo "success,return body = " . $resp->getBody()->getContents()."\n";
} else if ($statusCode == 204) { //处理成功,无返回Body
echo "success";
}
} catch (RequestException $e) {
// 进行错误处理
echo $e->getMessage()."\n";
if ($e->hasResponse()) {
echo "failed,resp code = " . $e->getResponse()->getStatusCode() . " return body = " . $e->getResponse()->getBody() . "\n";
}
return;
}
更多参数、响应详情及错误码请参见 查询先享卡订单API接口文档
3.2.5. 【服务端】增加用户记录API
步骤说明:每新增一条用户消费记录,商户都要将消费信息同步站给微信。
示例代码:
public void AddUserRecords() throws Exception{
//请求URL
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/discount-card/cards/{out_card_code}/add-user-records");
// 请求body参数
String reqdata = "{"
+ "\"card_template_id\":\"87789b2f25177433bcbf407e8e471f95\","
+ "\"reward_usage_records\": ["
+ "{"
+ "\"usage_count\":100,"
+ "\"amount\":1,"
+ "\"usage_type\":\"INCREASE\","
+ "\"usage_time\":\"2015-05-20T13:29:35.120+08:00\","
+ "\"reward_usage_serial_no\":\"578354\","
+ "\"description\":\"购买商品\","
+ "\"reward_id\":\"123456\","
+ "\"remark\":\"特价商品\""
+ "}"
+ "],"
+ "\"objective_completion_records\": ["
+ "{"
+ "\"completion_time\":\"2015-05-20T13:29:35.120+08:00\","
+ "\"objective_completion_serial_no\":\"578354545\","
+ "\"description\":\"购买商品/取消购买商品\","
+ "\"completion_count\":1,"
+ "\"remark\":\"特价商品\","
+ "\"completion_type\":\"INCREASE\","
+ "\"objective_id\":\"123456\""
+ "},"
+ "{"
+ "\"completion_time\":\"2015-05-20T13:29:35.120+08:00\","
+ "\"objective_completion_serial_no\":\"578354545\","
+ "\"description\":\"购买商品/取消购买商品\","
+ "\"completion_count\":1,"
+ "\"remark\":\"特价商品\","
+ "\"completion_type\":\"INCREASE\","
+ "\"objective_id\":\"123456\""
+ "}"
+ "]"
+ "}";
StringEntity entity = new StringEntity(reqdata);
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
//完成签名并执行请求
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) { //处理成功
System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
} else if (statusCode == 204) { //处理成功,无返回Body
System.out.println("success");
} else {
System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
throw new IOException("request failed");
}
} finally {
response.close();
}
}
try {
$resp = $client->request(
'POST',
'https://api.mch.weixin.qq.com/v3/discount-card/cards/{out_card_code}/add-user-records', //请求URL
[
// JSON请求体
'json' => [
"card_template_id" => "87789b2f25177433bcbf407e8e471f95",
"reward_usage_records" => [
[
"usage_count" => 100,
"amount" => 1,
"usage_type" => "INCREASE",
"usage_time" => "2015-05-20T13:29:35.120+08:00",
"reward_usage_serial_no" => "578354",
"description" => "购买商品",
"reward_id" => "123456",
"remark" => "特价商品",
],
],
"objective_completion_records" => [
[
"completion_time" => "2015-05-20T13:29:35.120+08:00",
"objective_completion_serial_no" => "578354545",
"description" => "购买商品/取消购买商品",
"completion_count" => 1,
"remark" => "特价商品",
"completion_type" => "INCREASE",
"objective_id" => "123456",
],
[
"completion_time" => "2015-05-20T13:29:35.120+08:00",
"objective_completion_serial_no" => "578354545",
"description" => "购买商品/取消购买商品",
"completion_count" => 1,
"remark" => "特价商品",
"completion_type" => "INCREASE",
"objective_id" => "123456",
],
]
],
'headers' => [ 'Accept' => 'application/json' ]
]
);
$statusCode = $resp->getStatusCode();
if ($statusCode == 200) { //处理成功
echo "success,return body = " . $resp->getBody()->getContents()."\n";
} else if ($statusCode == 204) { //处理成功,无返回Body
echo "success";
}
} catch (RequestException $e) {
// 进行错误处理
echo $e->getMessage()."\n";
if ($e->hasResponse()) {
echo "failed,resp code = " . $e->getResponse()->getStatusCode() . " return body = " . $e->getResponse()->getBody() . "\n";
}
return;
}
更多参数、响应详情及错误码请参见 增加用户记录API接口文档
3.2.6. 【服务端】扣费状态变化通知
步骤说明:用户完成所有卡服务后,微信会对用户进行扣款,并将扣款状态回调通知给商户
注意:
支付结果通知是以POST 方法访问商户设置的通知url,通知的数据以JSON 格式通过请求主体(BODY)传输。通知的数据包括了加密的支付结果详情
加密不能保证通知请求来自微信。微信会对发送给商户的通知进行签名,并将签名值放在通知的HTTP头Wechatpay-Signature。商户应当验证签名,以确认请求来自微信,而不是其他的第三方。签名验证的算法请参考微信支付API v3签名方案
支付通知http应答码为200或204才会当作正常接收,当回调处理异常时,应答的HTTP状态码应为500,或者4xx
商户成功接收到回调通知后应返回成功的http应答码为200或204
同样的通知可能会多次发送给商户系统。商户系统必须能够正确处理重复的通知。 推荐的做法是,当商户系统收到通知进行处理时,先检查对应业务数据的状态,并判断该通知是否已经处理。如果未处理,则再进行处理;如果已处理,则直接返回结果成功。在对业务数据进行状态检查和处理之前,要采用数据锁进行并发控制,以避免函数重入造成的数据混乱
如果在所有通知频率(4小时)后没有收到微信侧回调,商户应调用查询先享卡订单接口确认订单状态。
更多参数、响应详情及错误码请参见 扣费状态变化通知API接口文档
3.2.7. 【服务端】守约状态变化通知
步骤说明:用户主动提前结束卡服务,微信会该消息回调通知给商户
注意:
支付结果通知是以POST 方法访问商户设置的通知url,通知的数据以JSON 格式通过请求主体(BODY)传输。通知的数据包括了加密的支付结果详情
加密不能保证通知请求来自微信。微信会对发送给商户的通知进行签名,并将签名值放在通知的HTTP头Wechatpay-Signature。商户应当验证签名,以确认请求来自微信,而不是其他的第三方。签名验证的算法请参考微信支付API v3签名方案
支付通知http应答码为200或204才会当作正常接收,当回调处理异常时,应答的HTTP状态码应为500,或者4xx
商户成功接收到回调通知后应返回成功的http应答码为200或204
同样的通知可能会多次发送给商户系统。商户系统必须能够正确处理重复的通知。 推荐的做法是,当商户系统收到通知进行处理时,先检查对应业务数据的状态,并判断该通知是否已经处理。如果未处理,则再进行处理;如果已处理,则直接返回结果成功。在对业务数据进行状态检查和处理之前,要采用数据锁进行并发控制,以避免函数重入造成的数据混乱。
如果在所有通知频率(4小时)后没有收到微信侧回调,商户应调用查询先享卡订单接口确认订单状态。
更多参数、响应详情及错误码请参见 守约状态变化通知API接口文档