相关文章
1、介绍
微信有多种支付产品,如付款码支付、公众号支付、H5 支付、小程序支付、Native 支付、APP 支付等。其中公众号支付、小程序支付最为常用。
微信官方产品介绍
微信支付接入有两种模式:
- 直联模式:对接微信官方接口
- 间联模式:通过第三方支付公司,费率低、支持灵活结算。
2、微信直联接入准备
2.1、域名
准备一个域名,必须要有 https 证书,并且 icp 备案了。
2.2、微信小程序
-
申请微信小程序
-
微信认证
微信小程序需要微信认证,才能使用支付相关的功能。微信认证服务审核费300元/次/年。 -
小程序备案
因法律法规要求,小程序必须备案。 -
获取AppId与AppSecret
AppId、AppSecret为微信小程序的开发者ID与密钥,获取微信openId需要用到。
2.3、微信商户
2.3.1、申请微信商户
接入微信支付,需要申请一个微信商户。
申请微信商户
3、微信直联技术对接
业务流程图
重点步骤说明:
步骤3 用户下单发起支付,商户可通过 JSAPI下单 创建支付订单。
步骤8 商户可在微信浏览器内通过 JSAPI调起支付API 调起微信支付,发起支付请求。
步骤15 用户支付成功后,商户可接收到微信支付支付结果通知支付结果通知API 。
步骤20 商户在没有接收到微信支付结果通知的情况下需要主动调用查询订单API 查询支付结果。
4、获取微信用户OpenID
5、Java接入
5.1、java demo下载(包含直联、间联)
5.1、微信直联Java代码
这里只提供下单的测试代码,订单查询、退款、回调等相关代码,请查看java demo。
5.1.1、maven引用
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
<version>0.2.15</version>
</dependency>
5.1.2、测试类
import com.alibaba.fastjson.JSON;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import org.junit.Test;
/**
* 微信直联测试
*/
public class WechatDirectTest {
//微信JSAPI测试
@Test
public void jsapi(){
Config config = getWechatConfig();
// 构建service
JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();
PrepayRequest request = new PrepayRequest();
Amount amount = new Amount();
//金额转分
amount.setTotal(1);
request.setAmount(amount);
request.setAppid("公众号或小程序appId");
Payer payer = new Payer();
payer.setOpenid("用户openId");
request.setPayer(payer);
request.setMchid("微信商户号");
// 设置订单标题
request.setDescription("测试");
// 设置商户订单号
request.setOutTradeNo(System.currentTimeMillis() + "");
String label = "微信-JSAPI下单接口";
try {
System.out.println(label + " 请求参数:" + JSON.toJSONString(request));
// 调用下单方法,得到应答
PrepayWithRequestPaymentResponse response = service.prepayWithRequestPayment(request);
System.out.println(label + " 返回参数:" + JSON.toJSONString(response));
} catch (Exception e) { // 发送HTTP请求失败
System.err.println(e);
}
}
/**
* 初始化商户配置
*/
private Config getWechatConfig(){
//使用微信新的公钥模式(新入网的,强制使用这种模式)
RSAPublicKeyConfig.Builder builder = new RSAPublicKeyConfig.Builder()
.merchantId("") //微信商户号
.publicKeyId("") //微信公钥ID
.publicKeyFromPath("") //微信公钥证书路径
.merchantSerialNumber("") //微信证书序列号
.privateKeyFromPath("") //微信私钥证书路径
.apiV3Key(""); //微信API V3密钥
return builder.build();
}
}