Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
A
Afrishop refactored project
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Whispa
Afrishop refactored project
Commits
32dcd1eb
提交
32dcd1eb
authored
11月 27, 2020
作者:
吴德鹏
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
paypal payment
上级
4a2f2a0c
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
294 行增加
和
168 行删除
+294
-168
pom.xml
pom.xml
+20
-0
PaypalContoller.java
.../com/example/afrishop_v3/controllers/PaypalContoller.java
+262
-164
WebSecurityConfig.java
...a/com/example/afrishop_v3/security/WebSecurityConfig.java
+1
-1
AfrishopV3ApplicationTests.java
...a/com/example/afrishop_v3/AfrishopV3ApplicationTests.java
+11
-3
没有找到文件。
pom.xml
浏览文件 @
32dcd1eb
...
@@ -249,6 +249,26 @@
...
@@ -249,6 +249,26 @@
</exclusion>
</exclusion>
</exclusions>
</exclusions>
</dependency>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<scope>
test
</scope>
</dependency>
<!--发邮件 END-->
<!--发邮件 END-->
</dependencies>
</dependencies>
...
...
src/main/java/com/example/afrishop_v3/controllers/PaypalContoller.java
浏览文件 @
32dcd1eb
//package com.example.afrishop_v3.controllers;
package
com
.
example
.
afrishop_v3
.
controllers
;
//
//import com.diaoyun.zion.chinafrica.constant.KeyConstant;
//import com.diaoyun.zion.chinafrica.entity.TbCfOrderEntity;
import
com.example.afrishop_v3.base.Result
;
//import com.diaoyun.zion.chinafrica.enums.OrderStatusEnum;
import
com.example.afrishop_v3.config.PaypalPaymentIntent
;
//import com.diaoyun.zion.chinafrica.service.PayPalService;
import
com.example.afrishop_v3.config.PaypalPaymentMethod
;
//import com.diaoyun.zion.chinafrica.service.TbCfOrderService;
import
com.example.afrishop_v3.enums.DeliveryStatusEnum
;
//import com.diaoyun.zion.chinafrica.vo.TbCfOrderVo;
import
com.example.afrishop_v3.enums.OrderStatusEnum
;
//import com.diaoyun.zion.master.base.Result;
import
com.example.afrishop_v3.enums.ResultCodeEnum
;
//import com.diaoyun.zion.master.common.RedisCache;
import
com.example.afrishop_v3.models.TbCfFinance
;
//import com.diaoyun.zion.master.config.PaypalPaymentIntent;
import
com.example.afrishop_v3.models.TbCfOrder
;
//import com.diaoyun.zion.master.config.PaypalPaymentMethod;
import
com.example.afrishop_v3.repository.TbCfFinanceRepository
;
//import com.diaoyun.zion.master.enums.ResultCodeEnum;
import
com.example.afrishop_v3.repository.TbCfOrderRepository
;
//import com.example.afrishop_v3.base.Result;
import
com.example.afrishop_v3.util.IdUtil
;
//import com.example.afrishop_v3.bo.KeyConstant;
import
com.paypal.api.payments.*
;
//import com.example.afrishop_v3.config.PaypalPaymentIntent;
import
com.paypal.base.rest.APIContext
;
//import com.example.afrishop_v3.config.PaypalPaymentMethod;
import
org.apache.commons.lang3.StringUtils
;
//import com.example.afrishop_v3.enums.OrderStatusEnum;
import
org.slf4j.Logger
;
//import com.example.afrishop_v3.enums.ResultCodeEnum;
import
org.slf4j.LoggerFactory
;
//import com.example.afrishop_v3.models.TbCfOrder;
import
org.springframework.beans.factory.annotation.Value
;
//import com.example.afrishop_v3.repository.TbCfOrderRepository;
import
org.springframework.web.bind.annotation.*
;
//import com.paypal.api.payments.Links;
//import com.paypal.api.payments.Payment;
import
javax.servlet.http.HttpServletRequest
;
//import com.paypal.base.rest.APIContext;
import
javax.servlet.http.HttpServletResponse
;
//import io.swagger.annotations.Api;
import
java.io.IOException
;
//import io.swagger.annotations.ApiOperation;
import
java.math.BigDecimal
;
//import io.swagger.annotations.ApiParam;
import
java.text.SimpleDateFormat
;
//import org.apache.commons.lang3.StringUtils;
import
java.time.Instant
;
//import org.slf4j.Logger;
import
java.time.LocalDateTime
;
//import org.slf4j.LoggerFactory;
import
java.util.ArrayList
;
//import org.springframework.beans.factory.annotation.Autowired;
import
java.util.Date
;
//import org.springframework.beans.factory.annotation.Value;
import
java.util.List
;
//import org.springframework.web.bind.annotation.*;
import
java.util.Optional
;
//
//import javax.annotation.Resource;
/**
//import javax.servlet.http.HttpServletRequest;
* @Auther: wudepeng
//import javax.servlet.http.HttpServletResponse;
* @Date: 2020/11/27
//import java.math.BigDecimal;
* @Description:paypal支付
//import java.util.Date;
*/
//import java.util.Optional;
@RestController
//
@RequestMapping
(
value
=
"/paypal"
)
///**
public
class
PaypalContoller
{
// * @Auther: wudepeng
// * @Date: 2020/11/10
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
PaypalContoller
.
class
);
// * @Description:paypal支付
// */
@Value
(
"${paypal.success_url}"
)
//@RestController
private
String
PAYPAL_SUCCESS_URL
;
//@RequestMapping(value = "/paypal")
//public class PaypalContoller {
@Value
(
"${paypal.cancel_url}"
)
//
private
String
PAYPAL_CANCEL_URL
;
// private static Logger logger = LoggerFactory.getLogger(PaypalContoller.class);
//
@Value
(
"${paypal.success_page}"
)
// @Value("${paypal.success_url}")
private
String
PAYPAL_SUCCESS_PAGE
;
// private String PAYPAL_SUCCESS_URL;
//
@Value
(
"${paypal.failed_page}"
)
// @Value("${paypal.cancel_url}")
private
String
PAYPAL_FAILED_PAGE
;
// private String PAYPAL_CANCEL_URL;
//
// @Value("${paypal.success_page}")
private
final
APIContext
apiContext
;
// private String PAYPAL_SUCCESS_PAGE;
//
private
final
TbCfOrderRepository
orderRepository
;
// @Value("${paypal.failed_page}")
// private String PAYPAL_FAILED_PAGE;
private
final
TbCfFinanceRepository
financeRepository
;
//
// @Autowired
public
PaypalContoller
(
APIContext
apiContext
,
TbCfOrderRepository
orderRepository
,
TbCfFinanceRepository
financeRepository
)
{
// private APIContext apiContext;
this
.
apiContext
=
apiContext
;
//
this
.
orderRepository
=
orderRepository
;
// @Autowired
this
.
financeRepository
=
financeRepository
;
// private TbCfOrderRepository orderRepository;
}
//
// /**
// * 发起paypal支付
/**
// *
* 发起paypal支付
// * @param orderId 订单号
*
// * @return Result
* @param orderId 订单号
// */
* @return Result
// @PostMapping("/payment/{orderId}")
*/
// public Result payment(@PathVariable("orderId") String orderId) {
@PostMapping
(
"/payment/{orderId}"
)
// Result result = new Result();
public
Result
payment
(
@PathVariable
(
"orderId"
)
String
orderId
)
{
// //==========================支付信息校验==========================
Result
result
=
new
Result
();
//
//==========================支付信息校验==========================
// //订单号
// if (StringUtils.isBlank(orderId)) {
//订单号
// logger.error("paypal支付,订单Id不能为空");
if
(
StringUtils
.
isBlank
(
orderId
))
{
// return payErrorInfo(result);
logger
.
error
(
"paypal支付,订单Id不能为空"
);
// }
return
payErrorInfo
(
result
);
//
}
//// //从缓存中获取订单信息
//// TbCfOrderVo orderCache = (TbCfOrderVo) orderRedisCache.get(KeyConstant.ORDER_DET + orderId);
//订单信息
////
Optional
<
TbCfOrder
>
byId
=
orderRepository
.
findById
(
orderId
);
//// if (orderCache == null) {
if
(
byId
.
isPresent
())
{
//// //订单已过期或者不存在
TbCfOrder
order
=
byId
.
get
();
//// logger.error("paypal支付,订单不存在");
//// return payErrorInfo(result);
//订单不存在
//// }
if
(
order
==
null
)
{
//
logger
.
error
(
"订单不存在"
);
// //订单信息
return
payErrorInfo
(
result
);
// Optional<TbCfOrder> byId = orderRepository.findById(orderId);
}
// if (byId.isPresent()) {
// TbCfOrder order = byId.get();
//价格不存在或错误
// //价格不存在或错误
if
(
order
.
getRealityPay
()
==
null
||
order
.
getRealityPay
().
compareTo
(
BigDecimal
.
ZERO
)
<=
0
)
{
// if (order.getRealityPay() == null || order.getRealityPay().compareTo(BigDecimal.ZERO) <= 0) {
logger
.
error
(
"paypal支付,订单["
+
order
.
getOrderNo
()
+
"]价格错误"
);
// logger.error("paypal支付,订单[" + order.getOrderNo() + "]价格错误");
return
payErrorInfo
(
result
);
// return payErrorInfo(result);
}
// }
//
//订单已支付
//
if
(
OrderStatusEnum
.
PAID
.
getValue
().
equals
(
order
.
getPayStatus
().
toString
()))
{
// //订单已支付
logger
.
error
(
"paypal支付,订单["
+
order
.
getOrderNo
()
+
"]已支付"
);
// if (OrderStatusEnum.PAID.getValue().equals(order.getPayStatus().toString())) {
return
payErrorInfo
(
result
);
// logger.error("paypal支付,订单[" + order.getOrderNo() + "]已支付");
}
// return payErrorInfo(result);
// }
//==========================paypal支付业务开始==========================
//
//
logger
.
info
(
"paypal支付开始,时间:"
+
getTime
());
// //价格转double类型
String
orderInfo
=
order
.
getDeliveryName
()
+
"'s payment information"
;
// BigDecimal realityPay = order.getRealityPay();
logger
.
info
(
orderInfo
);
// String price = realityPay.toString();
try
{
// double orderPrice = Double.parseDouble(price);
//
Amount
amount
=
new
Amount
();
// //==========================paypal支付业务开始==========================
amount
.
setCurrency
(
"USD"
);
//
amount
.
setTotal
(
String
.
format
(
"%.2f"
,
order
.
getRealityPay
()));
// logger.info("paypal支付开始,时间:" + new Date());
// String orderInfo = order.getDeliveryName() + "'s payment information";
Transaction
transaction
=
new
Transaction
();
// logger.info(orderInfo);
transaction
.
setDescription
(
orderInfo
);
// String currency = "USD";
transaction
.
setAmount
(
amount
);
// try {
// Payment payment = payPalService.payment(
List
<
Transaction
>
transactions
=
new
ArrayList
<>();
// orderId,
transactions
.
add
(
transaction
);
// orderPrice,
// currency,
Payer
payer
=
new
Payer
();
// PaypalPaymentMethod.paypal,
payer
.
setPaymentMethod
(
PaypalPaymentMethod
.
paypal
.
toString
());
// PaypalPaymentIntent.sale,
Payment
payment
=
new
Payment
();
// orderInfo,
payment
.
setIntent
(
PaypalPaymentIntent
.
sale
.
toString
());
// PAYPAL_CANCEL_URL,
payment
.
setPayer
(
payer
);
// PAYPAL_SUCCESS_URL);
payment
.
setTransactions
(
transactions
);
//
RedirectUrls
redirectUrls
=
new
RedirectUrls
();
// String paymentId = payment.getId();
redirectUrls
.
setCancelUrl
(
PAYPAL_CANCEL_URL
);
// orderRedisCache.set(KeyConstant.ORDER_DET + "_" + paymentId, orderId);
redirectUrls
.
setReturnUrl
(
PAYPAL_SUCCESS_URL
+
"/"
+
orderId
);
// logger.info("paypal支付完成,时间:" + new Date());
payment
.
setRedirectUrls
(
redirectUrls
);
// for (Links links : payment.getLinks()) {
Payment
pay
=
payment
.
create
(
apiContext
);
// if (links.getRel().equals("approval_url")) {
// return result.setData(links.getHref());
logger
.
info
(
"paypal支付完成,时间:"
+
getTime
());
// }
for
(
Links
links
:
pay
.
getLinks
())
{
// }
if
(
links
.
getRel
().
equals
(
"approval_url"
))
{
// } catch (Exception e) {
return
result
.
setData
(
links
.
getHref
());
// logger.info("paypal支付发生异常,时间:" + new Date() + e.getMessage());
}
// return payErrorInfo(result);
}
// }
}
catch
(
Exception
e
)
{
// }
logger
.
info
(
"paypal支付发生异常,时间:"
+
getTime
()
+
e
.
getMessage
());
//
return
payErrorInfo
(
result
);
// return payErrorInfo(result);
}
// }
}
//
// /**
return
payErrorInfo
(
result
);
// * 支付失败提示
}
// *
// * @param result
/**
// */
* 用户取消支付
// public Result payErrorInfo(Result result) {
*/
// result.setCode(ResultCodeEnum.ORDER_PAY_ERROR.getCode());
@GetMapping
(
"/cancel"
)
// result.setMessage(ResultCodeEnum.ORDER_PAY_ERROR.getDesc());
public
void
paymentCancle
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
// return result;
response
.
sendRedirect
(
PAYPAL_FAILED_PAGE
);
// }
}
//}
/**
* 支付成功的回调方法
* <p>
* https://app.afrieshop.com/afrishop/paypal/success?
* paymentId=PAYID-L6XFFIA6TM43947C4571820W&
* token=EC-82206922L7926962C&
* PayerID=WBS8FAZYEQQXS
*/
@GetMapping
(
"/success/{orderId}"
)
public
void
paymentSuccess
(
@PathVariable
(
"orderId"
)
String
orderId
,
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
Exception
{
logger
.
info
(
"paypal支付校验开始,时间:"
+
getTime
());
String
payerId
=
request
.
getParameter
(
"PayerID"
);
//订单id
String
paymentId
=
request
.
getParameter
(
"paymentId"
);
String
token
=
request
.
getParameter
(
"token"
);
System
.
out
.
println
(
"payerId"
+
payerId
);
System
.
out
.
println
(
"paymentId"
+
paymentId
);
System
.
out
.
println
(
"token"
+
token
);
Payment
payment
=
new
Payment
();
payment
.
setId
(
paymentId
);
PaymentExecution
paymentExecute
=
new
PaymentExecution
();
paymentExecute
.
setPayerId
(
payerId
);
Payment
paypal
=
payment
.
execute
(
apiContext
,
paymentExecute
);
logger
.
info
(
"支付信息:"
+
paypal
);
logger
.
info
(
"orderId:"
+
orderId
);
Optional
<
TbCfOrder
>
byId
=
orderRepository
.
findById
(
orderId
);
String
payUrl
=
PAYPAL_SUCCESS_URL
+
"?paymentId="
+
payerId
+
"&token="
+
token
+
"&PayerID="
+
payerId
;
if
(
byId
.
isPresent
())
{
TbCfOrder
order
=
byId
.
get
();
//paypal支付成功
if
(
paypal
.
getState
().
equals
(
"approved"
))
{
logger
.
info
(
"paypal支付,订单["
+
order
.
getOrderNo
()
+
"]支付成功"
);
//1)、修改订单状态
TbCfOrder
tbCfOrder
=
changeOrderState
(
payerId
,
order
);
//2)、生成支付流水
createFinance
(
paymentId
,
payUrl
,
order
);
//数据库校验支付状态##PayStatus 20
if
(
OrderStatusEnum
.
PAID
.
getValue
().
equals
(
tbCfOrder
.
getPayStatus
()))
{
response
.
sendRedirect
(
PAYPAL_SUCCESS_PAGE
);
}
}
}
}
/**
* 支付失败提示
*
* @param result
*/
public
Result
payErrorInfo
(
Result
result
)
{
result
.
setCode
(
ResultCodeEnum
.
ORDER_PAY_ERROR
.
getCode
());
result
.
setMessage
(
ResultCodeEnum
.
ORDER_PAY_ERROR
.
getDesc
());
return
result
;
}
/**
* 修改订单状态
*/
private
TbCfOrder
changeOrderState
(
String
transToken
,
TbCfOrder
order
)
{
//更改订单状态
order
.
setUpdateTime
(
new
Date
());
order
.
setDealTime
(
new
Date
());
order
.
setPayId
(
transToken
);
order
.
setOrderStatus
(
OrderStatusEnum
.
PAID
.
getValue
());
order
.
setPayStatus
(
OrderStatusEnum
.
PAID
.
getValue
());
order
.
setDeliveryFlag
(
DeliveryStatusEnum
.
PROCESSING
.
getValue
());
return
orderRepository
.
save
(
order
);
}
private
TbCfFinance
createFinance
(
String
paymentId
,
String
url
,
TbCfOrder
tbCfOrderVo
)
{
TbCfFinance
tbCfFinance
=
new
TbCfFinance
();
tbCfFinance
.
setOrderId
(
tbCfOrderVo
.
getOrderId
());
tbCfFinance
.
setFinaceId
(
IdUtil
.
createIdbyUUID
());
tbCfFinance
.
setPayAccount
(
tbCfOrderVo
.
getRealityPay
());
tbCfFinance
.
setPayId
(
paymentId
);
tbCfFinance
.
setPayTime
(
new
Date
());
tbCfFinance
.
setReceiptUrl
(
url
);
tbCfFinance
.
setPayWayCode
(
"paypal"
);
tbCfFinance
.
setUserId
(
tbCfOrderVo
.
getUserId
());
financeRepository
.
save
(
tbCfFinance
);
return
tbCfFinance
;
}
public
String
getTime
()
{
SimpleDateFormat
format
=
new
SimpleDateFormat
(
"yyyy-MM-dd hh:mm:ss"
);
String
now
=
format
.
format
(
new
Date
());
return
now
;
}
}
src/main/java/com/example/afrishop_v3/security/WebSecurityConfig.java
浏览文件 @
32dcd1eb
...
@@ -66,7 +66,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
...
@@ -66,7 +66,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.
sessionManagement
().
sessionCreationPolicy
(
SessionCreationPolicy
.
STATELESS
).
and
()
.
sessionManagement
().
sessionCreationPolicy
(
SessionCreationPolicy
.
STATELESS
).
and
()
.
authorizeRequests
().
antMatchers
(
"/api/auth/**"
,
"/search/image/**"
,
"/itemStation/**"
,
"/startPage/**"
,
.
authorizeRequests
().
antMatchers
(
"/api/auth/**"
,
"/search/image/**"
,
"/itemStation/**"
,
"/startPage/**"
,
"/goodsType/**"
,
"/home/**"
,
"/spider/**"
,
"/store/**"
,
"/shopify/**"
,
"/community/**"
,
"/version/**"
,
"/goodsType/**"
,
"/home/**"
,
"/spider/**"
,
"/store/**"
,
"/shopify/**"
,
"/community/**"
,
"/version/**"
,
"/flutterwave/notify/**"
,
"/dpo/notify/**"
,
"/advertisement/**"
,
"/website/**"
).
permitAll
()
"/flutterwave/notify/**"
,
"/dpo/notify/**"
,
"/advertisement/**"
,
"/website/**"
,
"/paypal/**"
).
permitAll
()
.
antMatchers
(
"/api/test/**"
).
permitAll
()
.
antMatchers
(
"/api/test/**"
).
permitAll
()
.
anyRequest
().
authenticated
();
.
anyRequest
().
authenticated
();
...
...
src/test/java/com/example/afrishop_v3/AfrishopV3ApplicationTests.java
浏览文件 @
32dcd1eb
//package com.example.afrishop_v3;
//package com.example.afrishop_v3;
//
//
//import org.junit.jupiter.api.Test;
//import org.junit.jupiter.api.Test;
//import org.junit.runner.RunWith;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.context.junit4.SpringRunner;
//
//
//import java.text.SimpleDateFormat;
//import java.time.LocalDateTime;
//import java.util.Date;
//
//@RunWith(SpringRunner.class)
//@SpringBootTest
//@SpringBootTest
//class AfrishopV3ApplicationTests {
//
public
class AfrishopV3ApplicationTests {
//
//
// @Test
// @Test
// void contextLoads() {
// void contextLoads() {
//
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//
// String now = format.format(new Date());
// System.out.println(now);
// }
// }
//
//
//}
//}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论