Skip to content

Commit

Permalink
Merge pull request #138 from Pay-Group/develop
Browse files Browse the repository at this point in the history
1.3.7
  • Loading branch information
lly835 authored Aug 17, 2022
2 parents ba7194c + c95accb commit aed49f3
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 36 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
示例 https://github.com/Pay-Group/best-pay-demo

## 版本更新
#### 1.3.7(2022.8.18)
1. 新增支付app
2. 修复支付宝wap回调参数的bug

#### 1.3.3(2020.5.23)
2. 修正支付沙盒

Expand All @@ -12,8 +16,6 @@
2. 新增支付宝WAP支付
3. PayRequest增加参数returnUrl, 优先级高于PayConfig.returnUrl
4. 修复:查询订单,微信订单未支付的情况下timeEnd会返回空
#### 1.3.0(2019.10.29)
1. 正式版发布

更多更新记录 https://github.com/Pay-Group/best-pay-sdk/releases

Expand Down Expand Up @@ -41,7 +43,7 @@
<dependency>
<groupId>cn.springboot</groupId>
<artifactId>best-pay-sdk</artifactId>
<version>1.3.0</version>
<version>请使用最新版</version>
</dependency>
```

Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cn.springboot</groupId>
<artifactId>best-pay-sdk</artifactId>
<version>1.3.4-SNAPSHOT</version>
<version>1.3.7</version>
<packaging>jar</packaging>
<name>best-pay-sdk</name>
<description>最好的支付sdk</description>
Expand Down Expand Up @@ -48,13 +48,13 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
<version>1.18.24</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.1</version>
</dependency>

<dependency>
Expand All @@ -66,7 +66,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
<version>4.5.13</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -225,4 +225,4 @@
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public interface AliPayConstants {
String ALIPAY_TRADE_BARCODE_PAY = "alipay.trade.pay";

/**
* 支付宝APP支付接口名称 method参数
* 支付宝APP支付
*/
String ALIPAY_TRADE_APP_PAY = "alipay.trade.app.pay";

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/lly835/bestpay/model/PayResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.lly835.bestpay.enums.BestPayPlatformEnum;
import lombok.Data;
import org.simpleframework.xml.Element;

import java.net.URI;
import java.util.Map;

/**
* 支付时的同步/异步返回参数
Expand Down Expand Up @@ -80,8 +82,5 @@ public class PayResponse {

private String prepayId;

/**
* 支付宝App支付返回的请求参数信息
*/
private String orderInfo;
private Map<String, String> map;
}
13 changes: 8 additions & 5 deletions src/main/java/com/lly835/bestpay/model/alipay/AliPayApi.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.lly835.bestpay.model.alipay;


import com.lly835.bestpay.model.alipay.request.AliPayTradeCreateRequest;
import com.lly835.bestpay.model.alipay.response.*;
import retrofit2.Call;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.*;

import java.util.Map;

Expand All @@ -18,7 +16,7 @@ public interface AliPayApi {

@FormUrlEncoded
@Headers("Content-Type:application/x-www-form-urlencoded; charset=utf-8")
@POST("gateway.do")
@POST("/gateway.do")
Call<AliPayOrderCreateResponse> tradeCreate(@FieldMap Map<String, String> map);

@FormUrlEncoded
Expand All @@ -33,4 +31,9 @@ public interface AliPayApi {
@POST("gateway.do")
Call<AliPayBankResponse> payBank(@FieldMap Map<String, String> map);

// @FormUrlEncoded
@Headers("Content-Type:application/x-www-form-urlencoded; charset=utf-8")
@POST("/gateway.do")
Call<AliPayOrderCreateResponse> tradeAppPay(@Body AliPayTradeCreateRequest request);

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,12 @@ public static class BizContent {
* 是否异步支付,传入true时,表明本次期望走异步支付,会先将支付请求受理下来,再异步推进。商户可以通过交易的异步通知或者轮询交易的状态来确定最终的交易结果
*/
private Boolean isAsyncPay;

private String timeExpire;

/**
* 异步回传参数,attach
*/
private String passbackParams;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ public class AliPayAsyncResponse implements Serializable {
* 交易结束时间
*/
private String gmtClose;

/**
* 附加/回传参数
*/
private String passbackParams;
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public PayResponse pay(PayRequest request) {
AlipayBarCodeServiceImpl alipayBarCodeService = new AlipayBarCodeServiceImpl();
alipayBarCodeService.setAliPayConfig(aliPayConfig);
return alipayBarCodeService.pay(request);
} else if (request.getPayTypeEnum() == BestPayTypeEnum.ALIPAY_APP) {
AlipayAppServiceImpl appService = new AlipayAppServiceImpl();
appService.setAliPayConfig(aliPayConfig);
return appService.pay(request);
}else if (request.getPayTypeEnum() == BestPayTypeEnum.ALIPAY_APP) {
AlipayAppServiceImpl alipayAppService = new AlipayAppServiceImpl();
alipayAppService.setAliPayConfig(aliPayConfig);
return alipayAppService.pay(request);
}
Map<String, String> requestParams = new HashMap<>();
requestParams.put("out_trade_no", request.getOrderId());
Expand All @@ -104,6 +104,7 @@ public PayResponse pay(PayRequest request) {
}
requestParams.put("total_amount", String.valueOf(request.getOrderAmount()));
requestParams.put("subject", String.valueOf(request.getOrderName()));
requestParams.put("passback_params", request.getAttach());

aliPayRequest.setAppId(aliPayConfig.getAppId());
aliPayRequest.setCharset("utf-8");
Expand Down Expand Up @@ -262,6 +263,7 @@ private PayResponse buildPayResponse(AliPayAsyncResponse response) {
payResponse.setOrderAmount(Double.valueOf(response.getTotalAmount()));
payResponse.setOrderId(response.getOutTradeNo());
payResponse.setOutTradeNo(response.getTradeNo());
payResponse.setAttach(response.getPassbackParams());
return payResponse;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,86 @@
package com.lly835.bestpay.service.impl.alipay;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.GsonBuilder;
import com.lly835.bestpay.constants.AliPayConstants;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.alipay.AliPayApi;
import com.lly835.bestpay.model.alipay.request.AliPayTradeCreateRequest;
import com.lly835.bestpay.model.alipay.response.AliPayOrderCreateResponse;
import com.lly835.bestpay.utils.JsonUtil;
import com.lly835.bestpay.utils.MapUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.Map;

/**
* @author zicheng
* @date 2020/12/2
* Description: https://opendocs.alipay.com/apis/api_1/alipay.trade.app.pay
* 支付APP支付
* Created by 廖师兄
* https://opendocs.alipay.com/open/01dcc0
*/
@Slf4j
public class AlipayAppServiceImpl extends AliPayServiceImpl {

private final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AliPayConstants.ALIPAY_GATEWAY_OPEN)
.addConverterFactory(GsonConverterFactory.create(
//下划线驼峰互转
new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
))
.client(new OkHttpClient.Builder()
.addInterceptor((new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)))
.followRedirects(false) //禁制OkHttp的重定向操作,我们自己处理重定向
.followSslRedirects(false)
.build()
)
.build();

private final Retrofit devRetrofit = new Retrofit.Builder()
.baseUrl(AliPayConstants.ALIPAY_GATEWAY_OPEN_DEV)
.addConverterFactory(GsonConverterFactory.create(
//下划线驼峰互转
new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
))
.client(new OkHttpClient.Builder()
.addInterceptor((new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)))
.build()
)
.build();

@Override
public PayResponse pay(PayRequest request) {
AliPayTradeCreateRequest payRequest = new AliPayTradeCreateRequest();
payRequest.setMethod(AliPayConstants.ALIPAY_TRADE_APP_PAY);
payRequest.setAppId(aliPayConfig.getAppId());
payRequest.setTimestamp(LocalDateTime.now().format(formatter));
payRequest.setNotifyUrl(aliPayConfig.getNotifyUrl());
AliPayTradeCreateRequest aliPayOrderQueryRequest = new AliPayTradeCreateRequest();
aliPayOrderQueryRequest.setMethod(AliPayConstants.ALIPAY_TRADE_APP_PAY);
aliPayOrderQueryRequest.setAppId(aliPayConfig.getAppId());
aliPayOrderQueryRequest.setTimestamp(LocalDateTime.now().format(formatter));
aliPayOrderQueryRequest.setNotifyUrl(aliPayConfig.getNotifyUrl());
AliPayTradeCreateRequest.BizContent bizContent = new AliPayTradeCreateRequest.BizContent();
bizContent.setOutTradeNo(request.getOrderId());
bizContent.setTotalAmount(request.getOrderAmount());
bizContent.setSubject(request.getOrderName());
bizContent.setIsAsyncPay(true);
bizContent.setPassbackParams(request.getAttach());

payRequest.setBizContent(JsonUtil.toJsonWithUnderscores(bizContent).replaceAll("\\s*", ""));
payRequest.setSign(AliPaySignature.sign(MapUtil.object2MapWithUnderline(payRequest), aliPayConfig.getPrivateKey()));
aliPayOrderQueryRequest.setBizContent(JsonUtil.toJsonWithUnderscores(bizContent).replaceAll("\\s*", ""));
String sign = AliPaySignature.sign(MapUtil.object2MapWithUnderline(aliPayOrderQueryRequest), aliPayConfig.getPrivateKey());
aliPayOrderQueryRequest.setSign(URLEncoder.encode(sign));

Map<String, String> stringStringMap = MapUtil.object2MapWithUnderline(aliPayOrderQueryRequest);
String body = MapUtil.toUrl(stringStringMap);
PayResponse payResponse = new PayResponse();
payResponse.setOrderInfo(MapUtil.toUrlWithSortAndEncode(MapUtil.removeEmptyKeyAndValue(MapUtil.object2MapWithUnderline(payRequest))));
payResponse.setBody(body);
return payResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public PayResponse pay(PayRequest request) {
bizContent.setSubject(request.getOrderName());
bizContent.setBuyerLogonId(request.getBuyerLogonId());
bizContent.setBuyerId(request.getBuyerId());
bizContent.setPassbackParams(request.getAttach());

//必须传一个
if (StringUtil.isEmpty(bizContent.getBuyerId())
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/lly835/bestpay/utils/MapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public static String toUrl(Map<String, String> map){

String url = "";
for(Map.Entry<String, String> entry : map.entrySet()){
if (StringUtil.isEmpty(entry.getValue())) {
continue;
}
url += entry.getKey() + "=" + entry.getValue() + "&";
}

Expand Down Expand Up @@ -176,8 +179,8 @@ public static String toUrlWithSortAndEncode(Map<String, String> map){
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = map.get(key);
if (value == null) {
break;
if (StringUtil.isEmpty(value)) {
continue;
}
if (i == keys.size() - 1) {//拼接时,不包括最后一个&字符
prestr = prestr + key + "=" + URLEncoder.encode(value);
Expand Down

0 comments on commit aed49f3

Please sign in to comment.