Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
29DCH authored Jan 18, 2019
1 parent 5cd595c commit 7156d0a
Show file tree
Hide file tree
Showing 28 changed files with 2,022 additions and 0 deletions.
41 changes: 41 additions & 0 deletions springboot-pay-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 功能
该example主要集成了支付宝支付和微信支付,其中支付宝支付使用的是沙箱环境,微信支付使用的是demo中的测试账号,目前已经接入的支付类型如下:


支付宝支付接口:

- AlipayController 支付宝-通用接口
- AlipayF2FPayController 支付宝-当面付
- AlipayPagePayController 支付宝-电脑网站支付
- AlipayWAPPayController 支付宝-手机网站支付

微信支付接口:

- WXPayController 微信支付-通用接口
- WXPayMicroPayController 微信支付-刷卡支付
- WXPayPrecreateController 微信支付-扫码支付
- WXPayH5PayController 微信支付-H5支付


# 关于测试账号
支付宝支付的账号可以自己在开放平台上直接申请,个人很容易通过,通过后直接使用沙箱环境即可。

微信支付如果要申请账号必须是服务号,还要每年缴300元,申请过程比较麻烦,这里直接使用demo中的账号用来测试,如果公司已经申请了账号最好使用公司的账号来测试


# 支付集成步骤
支付宝支付和微信支付具体集成步骤已经详细的记录在博客里了,如果不熟悉支付宝集成或者微信支付集成的,请移步下面博客

注意:微信支付坑很多很多,而且sdk写得过于简陋,很多必要的功能更都没有给出实现,关于坑和一些必要的逻辑实现在example和博客中都指出来。

- [Spring Boot入门教程(三十五):支付宝集成-准备工作](https://blog.csdn.net/vbirdbest/article/details/80635194)

- [Spring Boot入门教程(三十六):支付宝集成-当面付](https://blog.csdn.net/vbirdbest/article/details/80655716)
- [Spring Boot入门教程(三十七):支付宝集成-手机网站支付](https://blog.csdn.net/vbirdbest/article/details/80684460)
- [Spring Boot入门教程(三十八):支付宝集成-电脑网站支付](https://blog.csdn.net/vbirdbest/article/details/80696690)
- [Spring Boot入门教程(三十九):微信支付集成-申请服务号和微信支付](https://blog.csdn.net/vbirdbest/article/details/80717905)
- [Spring Boot入门教程(四十):微信支付集成-刷卡支付](https://blog.csdn.net/vbirdbest/article/details/80720138)
- [Spring Boot入门教程(四十一):微信支付集成-扫码支付](https://blog.csdn.net/vbirdbest/article/details/80723991)
- [Spring Boot入门教程(四十二):微信支付集成-H5支付](https://blog.csdn.net/vbirdbest/article/details/80726616)


91 changes: 91 additions & 0 deletions springboot-pay-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>springboot-pay-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springboot-pay-example</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- alipay begin -->
<dependency>
<groupId>com.alipay</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>20170725114550</version>
</dependency>
<dependency>
<groupId>com.alipay</groupId>
<artifactId>alipay-trade-sdk</artifactId>
<version>20161215</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<!-- alipay end -->

<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.pay;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootPayExampleApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootPayExampleApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.pay.configuration;


import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* 两个支付宝客户端,用户可以使用任意一个.
*
* alipay-trade-sdk 是对alipay-sdk-java的封装,建议使用alipay-trade-sdk.
*
*/
@Configuration
@EnableConfigurationProperties(AlipayProperties.class)
public class AlipayConfiguration {

@Autowired
private AlipayProperties properties;

/**
* alipay-trade-sdk
* @return
*/
@Bean
public AlipayTradeService alipayTradeService() {
return new AlipayTradeServiceImpl.ClientBuilder()
.setGatewayUrl(properties.getGatewayUrl())
.setAppid(properties.getAppid())
.setPrivateKey(properties.getAppPrivateKey())
.setAlipayPublicKey(properties.getAlipayPublicKey())
.setSignType(properties.getSignType())
.build();
}

/**
* alipay-sdk-java
* @return
*/
@Bean
public AlipayClient alipayClient(){
return new DefaultAlipayClient(properties.getGatewayUrl(),
properties.getAppid(),
properties.getAppPrivateKey(),
properties.getFormate(),
properties.getCharset(),
properties.getAlipayPublicKey(),
properties.getSignType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.example.pay.configuration;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;

/**
* 支付宝支付的参数配置
*
*/
@Data
@Slf4j
@ConfigurationProperties(prefix = "pay.alipay")
public class AlipayProperties {

/** 支付宝gatewayUrl */
private String gatewayUrl;
/** 商户应用id */
private String appid;
/** RSA私钥,用于对商户请求报文加签 */
private String appPrivateKey;
/** 支付宝RSA公钥,用于验签支付宝应答 */
private String alipayPublicKey;
/** 签名类型 */
private String signType = "RSA2";

/** 格式 */
private String formate = "json";
/** 编码 */
private String charset = "UTF-8";

/** 同步地址 */
private String returnUrl;

/** 异步地址 */
private String notifyUrl;

/** 最大查询次数 */
private static int maxQueryRetry = 5;
/** 查询间隔(毫秒) */
private static long queryDuration = 5000;
/** 最大撤销次数 */
private static int maxCancelRetry = 3;
/** 撤销间隔(毫秒) */
private static long cancelDuration = 3000;

private AlipayProperties() {}

/**
* PostContruct是spring框架的注解,在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法。
*/
@PostConstruct
public void init() {
log.info(description());
}

public String description() {
StringBuilder sb = new StringBuilder("\nConfigs{");
sb.append("支付宝网关: ").append(gatewayUrl).append("\n");
sb.append(", appid: ").append(appid).append("\n");
sb.append(", 商户RSA私钥: ").append(getKeyDescription(appPrivateKey)).append("\n");
sb.append(", 支付宝RSA公钥: ").append(getKeyDescription(alipayPublicKey)).append("\n");
sb.append(", 签名类型: ").append(signType).append("\n");

sb.append(", 查询重试次数: ").append(maxQueryRetry).append("\n");
sb.append(", 查询间隔(毫秒): ").append(queryDuration).append("\n");
sb.append(", 撤销尝试次数: ").append(maxCancelRetry).append("\n");
sb.append(", 撤销重试间隔(毫秒): ").append(cancelDuration).append("\n");
sb.append("}");
return sb.toString();
}

private String getKeyDescription(String key) {
int showLength = 6;
if (StringUtils.isNotEmpty(key) && key.length() > showLength) {
return new StringBuilder(key.substring(0, showLength)).append("******")
.append(key.substring(key.length() - showLength)).toString();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.example.pay.configuration;

import com.github.wxpay.sdk.WXPayConfig;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
* 微信支付的参数配置
*
*/
@Data
@Slf4j
@ConfigurationProperties(prefix = "pay.wxpay")
public class MyWXPayConfig implements WXPayConfig{

/** 公众账号ID */
private String appID;

/** 商户号 */
private String mchID;

/** API 密钥 */
private String key;

/** API 沙箱环境密钥 */
private String sandboxKey;

/** API证书绝对路径 */
private String certPath;

/** 退款异步通知地址 */
private String notifyUrl;

private Boolean useSandbox;

/** HTTP(S) 连接超时时间,单位毫秒 */
private int httpConnectTimeoutMs = 8000;

/** HTTP(S) 读数据超时时间,单位毫秒 */
private int httpReadTimeoutMs = 10000;


/**
* 获取商户证书内容
*
* @return 商户证书内容
*/
@Override
public InputStream getCertStream() {
File certFile = new File(certPath);
InputStream inputStream = null;
try {
inputStream = new FileInputStream(certFile);
} catch (FileNotFoundException e) {
log.error("cert file not found, path={}, exception is:{}", certPath, e);
}
return inputStream;
}

@Override
public String getKey(){
if (useSandbox) {
return sandboxKey;
}

return key;
}

}
Loading

0 comments on commit 7156d0a

Please sign in to comment.