1. 接口规则
2.开发环境搭建
2.1. 集成SDK
为了帮助开发者更好的调用接口,我们提供 微信支付APIv3官方SDK,请根据自身开发语言,选择对应的SDK库集成到项目,并在商户平台配置平台证书。
目前微信支付提供JAVA、PHP、GO三种语言版本的SDK,封装了签名生成、签名验证、敏感信息加/解密、媒体文件上传等基础接口功能。
各编程语言的SDK对“扫码支付”的业务接口支持不一样,如若未支持相关业务接口,你可以使用SDK中的HTTP类实现发送HTTP请求,它会自动生成签名和验证签名。
如下通用基础功能接口,已经在SDK中完成封装,可替换相关参数后快速测试。
签名生成
签名验证
敏感信息加解密
merchantPrivateKey(私钥)
wechatpayCertificates(平台证书)
APIV3Key(V3 key)
若使用的编程语言无对应的SDK,则需要按照接口规则与接口详细信息自行开发。
2.2. 接口体验
3. 扫码支付业务接入说明
3.1. 业务流程图
3.1.1. 关键步骤说明
商户可在微信内网页或小程序中通过金额控件SDK,在页面上呈现“金额控件”。(详见网页展示金额控件、小程序展示金额控件) 在页面上呈现微信支付金额控件,商家通过“展示金额控件”接口,在页面上呈现“金额控件”,通过用户对金额控件的操作,或商家调起金额控件时的设置,由SDK按逻辑自动判断是否出现。(详见下方3.2.1)
用户输入金额后,轻触“付款”按钮,金额控件SDK向商家传递付款指令(内包含付款金额),并等待商家返回拉起收银台的必要信息。
商家在此过程中获取用户输入金额,并在后台向微信支付下单。
商户服务端收到下单成功,生成调起支付签名,返回给商户前端,由商户前端调用金额控件SDK,传递签名数据,拉起支付收银台。
商户在没有接收到微信支付结果通知的情况下需要主动调用查询订单API查询支付结果。(详见下方3.2.4)
3.2.API接入(含示例代码)
文档展示了如何使用微信支付服务端 SDK 快速接入扫码支付产品,完成与微信支付对接的部分。
3.2.1.【客户端】扫码支付调起金额控件
商户在微信页面或微信小程序通过接口调用来展示微信支付金额控件。
3.2.2. 【服务端】扫码支付下单
用户输入金额后,轻触“付款”按钮,金额控件SDK向商家传递付款指令(内包含付款金额),并等待商家返回拉起收银台的必要信息。
JAVA

1public void CreateOrder() throws Exception{
2
3 HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/partner/transactions/scannedpos");
4
5 String reqdata = "{"
6 + "\"sp_appid\":\"wx8888888888888888\","
7 + "\"sp_mchid\":\"1230000109\","
8 + "\"sub_mchid\":\"1900000109\","
9 + "\"sub_appid\":\"wxd678efh567hg6999\","
10 + "\"description\":\"Image形象店-深圳腾大-QQ公仔\","
11 + "\"out_trade_no\":\"1217752501201407033233368018\","
12 + "\"time_expire\":\"2018-06-08T10:34:56+08:00\","
13 + "\"attach\":\"自定义数据说明\","
14 + "\"notify_url\":\"https://www.weixin.qq.com/wxpay/pay.php\","
15 + "\"goods_tag\":\"WXG\","
16 + "\"settle_info\": {"
17 + "\"profit_sharing\":false,"
18 + "},"
19 + "\"amount\": {"
20 + "\"total\":100,"
21 + "\"currency\":\"CNY\""
22 + "},"
23 + "\"payer\": {"
24 + "\"sp_openid\":\"oUpF8uMuAJO_M2pxb1Q9zNjWeS6o\","
25 + "\"sub_openid\":\"oUpF8uMuAJO_M2pxb1Q9zNjWeS6o\""
26 + "},"
27 + "\"detail\": {"
28 + "\"invoice_id\":\"wx123\","
29 + "\"goods_detail\": ["
30 + "{"
31 + "\"goods_name\":\"iPhoneX 256G\","
32 + "\"wechatpay_goods_id\":\"1001\","
33 + "\"quantity\":1,"
34 + "\"merchant_goods_id\":\"商品编码\","
35 + "\"unit_price\":828800"
36 + "},"
37 + "{"
38 + "\"goods_name\":\"iPhoneX 256G\","
39 + "\"wechatpay_goods_id\":\"1001\","
40 + "\"quantity\":1,"
41 + "\"merchant_goods_id\":\"商品编码\","
42 + "\"unit_price\":828800"
43 + "}"
44 + "],"
45 + "\"cost_price\":608800"
46 + "},"
47 + "\"scene_info\": {"
48 + "\"store_info\": {"
49 + "\"id\":\"0001\""
50 + "},"
51 + "\"device_id\":\"013467007045764\","
52 + "\"payer_client_ip\":\"14.23.150.211\""
53 + "}"
54 + "}";
55 StringEntity entity = new StringEntity(reqdata,"utf-8");
56 entity.setContentType("application/json");
57 httpPost.setEntity(entity);
58 httpPost.setHeader("Accept", "application/json");
59
60 CloseableHttpResponse response = httpClient.execute(httpPost);
61 try {
62 int statusCode = response.getStatusLine().getStatusCode();
63 if (statusCode == 200) {
64 System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
65 } else if (statusCode == 204) {
66 System.out.println("success");
67 } else {
68 System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
69 throw new IOException("request failed");
70 }
71 } finally {
72 response.close();
73 }
74}

1try {
2 $resp = $client->request(
3 'POST',
4 'https://api.mch.weixin.qq.com/v3/pay/partner/transactions/scannedpos',
5 [
6
7 'json' => [
8 "time_expire" => "2018-06-08T10:34:56+08:00",
9 "amount" => [
10 "total" => 100,
11 "currency" => "CNY",
12 ],
13 "mchid" => "1230000109",
14 "description" => "Image形象店-深圳腾大-QQ公仔",
15 "notify_url" => "https://www.weixin.qq.com/wxpay/pay.php",
16 "payer" => [
17 "openid" => "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o",
18 ],
19 "out_trade_no" => "1217752501201407033233368018",
20 "goods_tag" => "WXG",
21 "appid" => "wxd678efh567hg6787",
22 "attach" => "自定义数据说明",
23 "detail" => [
24 "invoice_id" => "wx123",
25 "goods_detail" => [
26 [
27 "goods_name" => "iPhoneX 256G",
28 "wechatpay_goods_id" => "1001",
29 "quantity" => 1,
30 "merchant_goods_id" => "商品编码",
31 "unit_price" => 828800,
32 ],
33 [
34 "goods_name" => "iPhoneX 256G",
35 "wechatpay_goods_id" => "1001",
36 "quantity" => 1,
37 "merchant_goods_id" => "商品编码",
38 "unit_price" => 828800,
39 ],
40 ],
41 "cost_price" => 608800,
42 ],
43 "scene_info" => [
44 "store_info" => [
45 "id" => "0001",
46 ],
47 "device_id" => "013467007045764",
48 "payer_client_ip" => "14.23.150.211",
49 ]
50 ],
51 'headers' => [ 'Accept' => 'application/json' ]
52 ]
53 );
54 $statusCode = $resp->getStatusCode();
55 if ($statusCode == 200) {
56 echo "success,return body = " . $resp->getBody()->getContents()."\n";
57 } else if ($statusCode == 204) {
58 echo "success";
59 }
60 } catch (RequestException $e) {
61
62 echo $e->getMessage()."\n";
63 if ($e->hasResponse()) {
64 echo "failed,resp code = " . $e->getResponse()->getStatusCode() . " return body = " . $e->getResponse()->getBody() . "\n";
65 }
66 return;
67}
out_trade_no:商户系统内部订单号,只能是数字、大小写字母_-*且在同一个商户号下唯一
description:商品描述
notify_url:支付回调通知URL,该地址必须为直接可访问的URL,不允许携带查询串
total:订单总金额,单位为分
OpenID:OpenID是微信用户在AppID下的唯一用户标识(AppID不同,则获取到的OpenID就不同),可用于永久标记一个用户。OpenID获取方式请参考以下文档小程序获取OpenID 、公众号获取OpenID、App获取OpenID
3.2.3.【服务端】接收支付结果通知
当用户完成支付,微信会把相关支付结果将通过异步回调的方式通知商户,商户需要接收处理,并按文档规范返回应答。
| 注意 支付结果通知是以POST 方法访问商户设置的通知URL,通知的数据以JSON 格式通过请求主体(BODY)传输。通知的数据包括了加密的支付结果详情。 加密不能保证通知请求来自微信。微信会对发送给商户的通知进行签名,并将签名值放在通知的HTTP头Wechatpay-Signature。商户应当验证签名,以确认请求来自微信,而不是其他的第三方。签名验证的算法请参考 《微信支付API v3签名验证》。 支付通知HTTP应答码为200或204才会当作正常接收,当回调处理异常时,应答的HTTP状态码应为500,或者4xx。 商户成功接收到回调通知后应返回成功的HTTP应答码为200或204。 同样的通知可能会多次发送给商户系统。商户系统必须能够正确处理重复的通知。 推荐的做法是,当商户系统收到通知进行处理时,先检查对应业务数据的状态,并判断该通知是否已经处理。如果未处理,则再进行处理;如果已处理,则直接返回结果成功。在对业务数据进行状态检查和处理之前,要采用数据锁进行并发控制,以避免函数重入造成的数据混乱。 对后台通知交互时,如果微信收到商户的应答不符合规范或超时,微信认为通知失败,微信会通过一定的策略定期重新发起通知,尽可能提高通知的成功率,但微信不保证通知最终能成功。(通知频率为15s/15s/30s/3m/10m/20m/30m/30m/30m/60m/3h/3h/3h/6h/6h - 总计 24h4m)。
|
|
3.2.4. 【服务端】查询订单
当商户后台、网络、服务器等出现异常,商户系统最终未接收到支付通知时,商户可通过查询订单接口核实订单支付状态。
JAVA

1public void QueryOrder() throws Exception {
2
3
4 URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/pay/transactions/id/4200000745202011093730578574");
5 uriBuilder.setParameter("mchid", mchId);
6
7
8 HttpGet httpGet = new HttpGet(uriBuilder.build());
9 httpGet.addHeader("Accept", "application/json");
10 CloseableHttpResponse response = httpClient.execute(httpGet);
11
12 try {
13 int statusCode = response.getStatusLine().getStatusCode();
14 if (statusCode == 200) {
15 System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
16 } else if (statusCode == 204) {
17 System.out.println("success");
18 } else {
19 System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
20 throw new IOException("request failed");
21 }
22 } finally {
23 response.close();
24 }
25}

1try {
2 $resp = $client->request(
3 'GET',
4 'https://api.mch.weixin.qq.com/v3/pay/transactions/id/1217752501201407033233368018?mchid=1230000109',
5 [
6 'headers' => [ 'Accept' => 'application/json']
7 ]
8 );
9 $statusCode = $resp->getStatusCode();
10 if ($statusCode == 200) {
11 echo "success,return body = " . $resp->getBody()->getContents()."\n";
12 } else if ($statusCode == 204) {
13 echo "success";
14 }
15} catch (RequestException $e) {
16
17 echo $e->getMessage()."\n";
18 if ($e->hasResponse()) {
19 echo "failed,resp code = " . $e->getResponse()->getStatusCode() . " return body = " . $e->getResponse()->getBody() . "\n";
20 }
21 return;
22}
3.2.5. 【服务端】关闭订单
当商户订单支付失败需要生成新单号重新发起支付,要对原订单号调用关单,避免重复支付;系统下单后,用户支付超时,系统退出不再受理,避免用户继续,请调用关单接口
JAVA

1public void CloseOrder() throws Exception {
2
3
4 HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/sdkphp12345678920201028112429/close");
5
6 String reqdata ="{\"mchid\": \""+mchId+"\"}";
7
8 StringEntity entity = new StringEntity(reqdata,"utf-8");
9 entity.setContentType("application/json");
10 httpPost.setEntity(entity);
11 httpPost.setHeader("Accept", "application/json");
12
13
14 CloseableHttpResponse response = httpClient.execute(httpPost);
15 try {
16 int statusCode = response.getStatusLine().getStatusCode();
17 if (statusCode == 200) {
18 System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
19 } else if (statusCode == 204) {
20 System.out.println("success");
21 } else {
22 System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
23 throw new IOException("request failed");
24 }
25 } finally {
26 response.close();
27 }
28 }

1try {
2 $resp = $client->request(
3 'POST',
4 'https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}/close',
5 [
6
7 'json' => [
8 "mchid " => "1230000109",
9 ],
10 'headers' => [ 'Accept' => 'application/json' ]
11 ]
12 );
13 $statusCode = $resp->getStatusCode();
14 if ($statusCode == 200) {
15 echo "success,return body = " . $resp->getBody()->getContents()."\n";
16 } else if ($statusCode == 204) {
17 echo "success";
18 }
19 } catch (RequestException $e) {
20
21 echo $e->getMessage()."\n";
22 if ($e->hasResponse()) {
23 echo "failed,resp code = " . $e->getResponse()->getStatusCode() . " return body = " . $e->getResponse()->getBody() . "\n";
24 }
25 return;
26 }
进一步阅读