简介
Native支付是指商户系统按微信支付协议生成支付二维码,用户再用微信“扫一扫”完成支付的模式。
应用场景
Native支付适用于PC网站、实体店单品或订单、媒体广告支付等场景,用户扫描商户展示在各种场景的二维码进行支付。聚体步骤如下:
1.商户根据微信支付的规则,为不同商品生成不同的二维码,展示在各种场景,用于用户扫描购买
2.用户使用微信“扫一扫”扫描二维码后,获取商品支付信息,引导用户完成支付。
3.用户确认支付,输入支付密码
4.支付完成后会提示用户支付成功,商户后台得到支付成功的通知,然后进行发货处理
接入前准备
直接跳转微信支付商户平台 https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_7_1.shtml
生成密钥文件:
配置文件:
wx:
appId: appId
keyPath: apiclient_key.pem
certPath: apiclient_cert.pem
certP12Path: 暂不用
platformCertPath: platform_cert.pem
mchId: mchId
apiKey3: 暂不用
apiKey: apiKey
domain: https://hous.exchang.cn
serialNo: 序列号
pom文件:
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
<version>0.2.2</version>
</dependency>
代码:
配置类:
//获取yml中微信配置
@Data
@ConfigurationProperties(prefix = "wx")
public class WechatProperties {
private String appId;
private String keyPath;
private String certPath;
private String platformCertPath;
private String mchId;
private String apiKey;
private String domain;
private String serialNo;
}
//配置类
@Configuration
@EnableConfigurationProperties(WechatProperties.class)
public class WechatNativeConfig {
private final WechatProperties wechatProperties;
public WechatNativeConfig(WechatProperties wechatProperties) {
this.wechatProperties = wechatProperties;
}
@Bean
public RSAConfig rsaConfig() throws IOException {
ClassPathResource keyResource = new ClassPathResource(wechatProperties.getKeyPath());
String apiClientKey = keyResource.getFile().getPath();
ClassPathResource certResource = new ClassPathResource(wechatProperties.getPlatformCertPath());
String platformCertResourceKey = certResource.getFile().getPath();
/*String apiClientKey = wechatProperties.getKeyPath();
// ClassPathResource certResource = new ClassPathResource(wechatProperties.getPlatformCertPath());
String platformCertResourceKey = wechatProperties.getPlatformCertPath();*/
return new RSAConfig.Builder()
.merchantId(wechatProperties.getMchId())
.privateKeyFromPath(apiClientKey)
.merchantSerialNumber(wechatProperties.getSerialNo())
//平台证书
.wechatPayCertificatesFromPath(platformCertResourceKey)
.build();
}
@Bean
public NativePayService nativePayService() throws IOException {
return new NativePayService.Builder()
.config(this.rsaConfig())
.build();
}
@Bean
public NotificationParser notificationParser() throws IOException{
ClassPathResource certResource = new ClassPathResource(wechatProperties.getPlatformCertPath());
String platformCertResourceKey = certResource.getFile().getPath();
//String platformCertResourceKey = wechatProperties.getPlatformCertPath();
NotificationConfig config = new RSANotificationConfig.Builder()
.apiV3Key(wechatProperties.getApiKey())
.certificatesFromPath(platformCertResourceKey)
.build();
// 初始化 NotificationParser
return new NotificationParser(config);
}
@Bean
public RefundService refundService() throws IOException {
return new RefundService.Builder().config(this.rsaConfig()).build();
}
}
创建支付单:
@Slf4j
@Service
@EnableConfigurationProperties(WechatProperties.class)
public class WechatNativePayServiceImpl implements BasePayService {
@Resource
private WechatProperties wechatProperties;
@Resource
private NativePayService nativePayService;
@Resource
private RedisCache redisCache;
@Value("${spring.application.name:scm-ofc-system}")
private String serviceName;
@Override