3.2. API接入(含示例代码)
本章节展示了如何使用微信支付服务端 SDK 快速接入小程序支付产品,完成与微信支付对接的部分。
注意:
文档中的代码示例是用来阐述 API 基本使用方法,代码中的示例参数需替换成商户自己账号及请求参数才能跑通。
以下接入步骤仅提供参考,请商户结合自身业务需求进行评估、修改。
3.2.1. 【服务端】小程序支付统一下单
步骤说明:用户通过商户小程序进入商户网页,当用户选择相关商品购买时,商户系统先调用该接口在微信支付服务后台生成预支付交易单。
示例代码
public void CreateOrder() throws Exception{
//请求URL
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi");
// 请求body参数
String reqdata = "{"
+ "\"time_expire\":\"2018-06-08T10:34:56+08:00\","
+ "\"amount\": {"
+ "\"total\":100,"
+ "\"currency\":\"CNY\""
+ "},"
+ "\"settle_info\": {"
+ "\"profit_sharing\":false"
+ "},"
+ "\"sp_mchid\":\"1230000109\","
+ "\"description\":\"Image形象店-深圳腾大-QQ公仔\","
+ "\"sub_appid\":\"wxd678efh567hg6999\","
+ "\"notify_url\":\"https://www.weixin.qq.com/wxpay/pay.php\","
+ "\"payer\": {"
+ "\"sp_openid\":\"oUpF8uMuAJO_M2pxb1Q9zNjWeS6o\","
+ "\"sub_openid\":\"oUpF8uMuAJO_M2pxb1Q9zNjWeS6o\""
+ "},"
+ "\"sp_appid\":\"wx8888888888888888\","
+ "\"out_trade_no\":\"1217752501201407033233368018\","
+ "\"goods_tag\":\"WXG\","
+ "\"sub_mchid\":\"1900000109\","
+ "\"attach\":\"自定义数据说明\","
+ "\"detail\": {"
+ "\"invoice_id\":\"wx123\","
+ "\"goods_detail\": ["
+ "{"
+ "\"goods_name\":\"iPhoneX 256G\","
+ "\"wechatpay_goods_id\":\"1001\","
+ "\"quantity\":1,"
+ "\"merchant_goods_id\":\"商品编码\","
+ "\"unit_price\":828800"
+ "},"
+ "{"
+ "\"goods_name\":\"iPhoneX 256G\","
+ "\"wechatpay_goods_id\":\"1001\","
+ "\"quantity\":1,"
+ "\"merchant_goods_id\":\"商品编码\","
+ "\"unit_price\":828800"
+ "}"
+ "],"
+ "\"cost_price\":608800"
+ "},"
+ "\"scene_info\": {"
+ "\"store_info\": {"
+ "\"address\":\"广东省深圳市南山区科技中一道10000号\","
+ "\"area_code\":\"440305\","
+ "\"name\":\"腾讯大厦分店\","
+ "\"id\":\"0001\""
+ "},"
+ "\"device_id\":\"013467007045764\","
+ "\"payer_client_ip\":\"14.23.150.211\""
+ "}"
+ "}";
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/pay/partner/transactions/jsapi', //请求URL
[
// JSON请求体
'json' => [
"time_expire" => "2018-06-08T10:34:56+08:00",
"amount" => [
"total" => 100,
"currency" => "CNY",
],
"settle_info" => [
"profit_sharing" => false,
],
"sp_mchid" => "1230000109",
"description" => "Image形象店-深圳腾大-QQ公仔",
"sub_appid" => "wxd678efh567hg6999",
"notify_url" => "https://www.weixin.qq.com/wxpay/pay.php",
"payer" => [
"sp_openid" => "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o",
"sub_openid" => "oUpF8uM

本文档介绍了如何使用微信支付服务端SDK接入小程序支付,包括统一下单、客户端调起支付、接收支付结果通知、查询订单、关闭订单、申请交易账单和下载账单的详细步骤与示例代码,涉及预支付交易单创建、订单状态查询和处理支付结果通知等关键操作。
最低0.47元/天 解锁文章

3551

被折叠的 条评论
为什么被折叠?



