Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
A
Afrishop refactored project
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Whispa
Afrishop refactored project
Commits
6541ea56
提交
6541ea56
authored
1月 05, 2021
作者:
吴德鹏
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
stripe 支付
上级
101303cb
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
197 行增加
和
47 行删除
+197
-47
StripeController.java
...com/example/afrishop_v3/controllers/StripeController.java
+196
-46
StripePay.java
src/main/java/com/example/afrishop_v3/util/StripePay.java
+1
-1
没有找到文件。
src/main/java/com/example/afrishop_v3/controllers/StripeController.java
浏览文件 @
6541ea56
package
com
.
example
.
afrishop_v3
.
controllers
;
package
com
.
example
.
afrishop_v3
.
controllers
;
import
com.example.afrishop_v3.base.Result
;
import
com.example.afrishop_v3.base.Result
;
import
com.google.gson.Gson
;
import
com.example.afrishop_v3.enums.DeliveryStatusEnum
;
import
com.stripe.Stripe
;
import
com.example.afrishop_v3.enums.OrderStatusEnum
;
import
com.example.afrishop_v3.models.*
;
import
com.example.afrishop_v3.repository.*
;
import
com.example.afrishop_v3.util.IdUtil
;
import
com.example.afrishop_v3.util.StripePay
;
import
com.stripe.exception.StripeException
;
import
com.stripe.exception.StripeException
;
import
com.stripe.model.Customer
;
import
com.stripe.model.Charge
;
import
com.stripe.model.PaymentIntent
;
import
org.apache.commons.lang3.StringUtils
;
import
com.stripe.model.PaymentMethod
;
import
com.stripe.model.checkout.Session
;
import
com.stripe.param.PaymentIntentCreateParams
;
import
com.stripe.param.checkout.SessionCreateParams
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.ArrayList
;
import
java.math.BigDecimal
;
import
java.util.HashMap
;
import
java.math.RoundingMode
;
import
java.util.Map
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
/**
/**
* @Auther: wudepeng
* @Auther: wudepeng
* @Date: 2021/01/04
* @Date: 2021/01/04
* @Description:Stripe支付
* @Description:Stripe支付
*/
*/
@Transactional
@RestController
@RestController
@RequestMapping
(
"/stripe"
)
@RequestMapping
(
"/stripe"
)
public
class
StripeController
{
public
class
StripeController
extends
Controller
{
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
StripeController
.
class
);
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
StripeController
.
class
);
...
@@ -42,45 +45,62 @@ public class StripeController {
...
@@ -42,45 +45,62 @@ public class StripeController {
@Value
(
"${stripe.cancel_url}"
)
@Value
(
"${stripe.cancel_url}"
)
private
String
CANCEL_URL
;
private
String
CANCEL_URL
;
@PostMapping
(
"/payment"
)
public
Result
payment
(
@RequestParam
Map
<
String
,
Object
>
paramMap
)
{
return
new
Result
();
private
final
TbCfOrderRepository
orderRepository
;
private
final
TbCfFinanceRepository
financeRepository
;
private
final
NetworkRepository
networkRepository
;
private
final
BonusRepository
bonusRepository
;
private
final
PostRepository
postRepository
;
private
final
UserRepository
userRepository
;
public
StripeController
(
TbCfOrderRepository
orderRepository
,
TbCfFinanceRepository
financeRepository
,
NetworkRepository
networkRepository
,
BonusRepository
bonusRepository
,
PostRepository
postRepository
,
UserRepository
userRepository
)
{
this
.
orderRepository
=
orderRepository
;
this
.
financeRepository
=
financeRepository
;
this
.
networkRepository
=
networkRepository
;
this
.
bonusRepository
=
bonusRepository
;
this
.
postRepository
=
postRepository
;
this
.
userRepository
=
userRepository
;
}
}
/* @PostMapping("/payment")
@PostMapping
(
"/payment"
)
public Result payment(@RequestParam Map<String, Object> paramMap) {
public
Result
payment
(
@RequestParam
Map
<
String
,
Object
>
params
)
throws
StripeException
{
Result
result
=
new
Result
();
Map<String, String> resultMap = new HashMap<>();
String
orderId
=
(
String
)
params
.
get
(
"orderId"
);
try {
String
token
=
(
String
)
params
.
get
(
"token"
);
Stripe.apiKey = SECRET_KEY;
if
(
orderId
==
""
||
orderId
==
null
)
{
Map<String, Object> params = new HashMap<String, Object>();
logger
.
info
(
"订单号不能为空"
);
ArrayList<String> paymentMethodTypes = new ArrayList<>();
return
result
;
paymentMethodTypes.add("card");
params.put("payment_method_types", paymentMethodTypes);
ArrayList<HashMap<String, Object>> lineItems = new ArrayList<>();
HashMap<String, Object> lineItem = new HashMap<String, Object>();
lineItem.put("name", "胡鹏飞测试商品");
lineItem.put("description", "这是一个测试单描述");
lineItem.put("amount", 500); // 支付金额
lineItem.put("currency", "usd"); //支付币种
lineItem.put("quantity", 1);
lineItems.add(lineItem);
params.put("line_items", lineItems);
//TODO 必须使用https 返回的回调地址
params.put("client_reference_id", paramMap.get("orderId"));//业务系统唯一标识 即订单唯一编号
params.put("success_url", SUCCESS_URL);// 支付成功跳转页面
params.put("cancel_url", CANCEL_URL);// 支付失败跳转页面
Session session = Session.create(params);
String sessionId = session.getId();
logger.info("sessionId :{}", session.getId());
resultMap.put("sessionId", sessionId);
} catch (StripeException e) {
e.printStackTrace();
}
}
return new Result(resultMap);
if
(
token
==
""
||
token
==
null
)
{
logger
.
info
(
"token不能为空"
);
return
result
;
}
Optional
<
TbCfOrder
>
byId
=
orderRepository
.
findById
(
orderId
);
if
(
byId
.
isPresent
())
{
TbCfOrder
order
=
byId
.
get
();
BigDecimal
orderPrice
=
order
.
getRealityPay
();
Charge
charge
=
StripePay
.
createCharge
(
orderPrice
.
longValue
(),
SECRET_KEY
,
token
);
if
(
"succeeded"
.
equals
(
charge
.
getStatus
()))
{
//支付成功
//1)、修改订单状态
changeOrderState
(
token
,
order
);
//2)、生成支付流水
createFinance
(
charge
.
getId
(),
charge
.
getReceiptUrl
(),
order
);
//3)、生成佣金
Bonus
bonus
=
new
Bonus
();
TbCfUserInfo
userInfo
=
new
TbCfUserInfo
();
userInfo
.
setUserId
(
order
.
getUserId
());
bonus
.
setOrderId
(
orderId
);
bonus
.
setAmount
(
order
.
getItemsPrice
());
System
.
out
.
println
(
"佣金-----》》》订单号:"
+
orderId
+
"=user="
+
order
.
getUserId
()
+
"=price="
+
order
.
getItemsPrice
());
saveNetworkMarketing
(
bonus
,
order
.
getUserId
());
}
}
return
new
Result
();
}
}*/
@GetMapping
(
"/getPublicKey"
)
@GetMapping
(
"/getPublicKey"
)
public
Result
getPublicKey
()
{
public
Result
getPublicKey
()
{
...
@@ -88,5 +108,135 @@ public class StripeController {
...
@@ -88,5 +108,135 @@ public class StripeController {
return
new
Result
(
PUBLIC_KEY
);
return
new
Result
(
PUBLIC_KEY
);
}
}
/**
* 修改订单状态
*/
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
;
}
public
void
saveNetworkMarketing
(
Bonus
bonus
,
String
userId
)
{
String
orderId
=
bonus
.
getOrderId
();
if
(
orderId
==
null
)
logger
.
info
(
"佣金:orderId为空"
);
Optional
<
TbCfOrder
>
orderOptional
=
orderRepository
.
findById
(
orderId
);
if
(!
orderOptional
.
isPresent
())
logger
.
info
(
"佣金:订单不存在"
);
TbCfOrder
order
=
orderOptional
.
get
();
if
(!
OrderStatusEnum
.
PAID
.
getValue
().
equals
(
order
.
getPayStatus
()))
{
logger
.
info
(
"佣金:订单未支付"
);
}
boolean
condition
=
orderId
!=
null
&&
orderOptional
.
isPresent
()
&&
OrderStatusEnum
.
PAID
.
getValue
().
equals
(
order
.
getPayStatus
())
&&
!
StringUtils
.
isBlank
(
userId
);
// if (bonusRepository.existsByOrderId(orderId)) {
// return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(), "Transaction already done !!!");
// }
BigDecimal
amount
=
bonus
.
getAmount
();
if
(
condition
)
{
Optional
<
TbCfUserInfo
>
optionalUser
=
userRepository
.
findById
(
userId
);
if
(
optionalUser
.
isPresent
())
{
TbCfUserInfo
user
=
optionalUser
.
get
();
Post
post
=
bonus
.
getPost
();
Optional
<
Post
>
postOptional
=
post
==
null
?
Optional
.
empty
()
:
postRepository
.
findById
(
post
.
getId
());
String
productSharer
=
bonus
.
getProductSharer
();
Optional
<
TbCfUserInfo
>
sharer
=
Optional
.
empty
();
if
(
productSharer
!=
null
)
{
sharer
=
userRepository
.
findByCode
(
productSharer
);
}
synchronized
(
this
)
{
boolean
exists
=
bonusRepository
.
existsByOrderId
(
orderId
);
if
(!
exists
&&
user
.
invited
())
{
BigDecimal
v
=
amount
.
multiply
(
BigDecimal
.
valueOf
(
10
)).
divide
(
BigDecimal
.
valueOf
(
100
),
RoundingMode
.
CEILING
);
bonus
.
setAmount
(
v
);
bonus
.
setUserInfo
(
user
);
bonus
.
setPercentage
(
10
);
bonus
=
bonusRepository
.
save
(
bonus
);
if
(
user
.
hasFcm
())
{
sendNotification
(
user
.
getFcm
(),
"Bonus alert !!"
,
user
.
display
()
+
", You received bonus of $"
+
formatter
.
format
(
v
)
+
" in your account"
);
}
}
TbCfUserInfo
bonusInc
=
runBonusInc
(
user
,
amount
,
5
,
false
,
orderId
);
runBonusInc
(
sharer
.
orElseGet
(()
->
postOptional
.
isPresent
()
?
postOptional
.
get
().
getRealUser
()
:
bonusInc
),
amount
,
5
,
postOptional
.
isPresent
()
||
sharer
.
isPresent
(),
orderId
);
//runBonusInc(bonusInc, amount, 0);
}
}
}
}
private
TbCfUserInfo
runBonusInc
(
TbCfUserInfo
user
,
BigDecimal
amount
,
int
percent
,
boolean
direct
,
String
orderId
)
{
if
(
user
==
null
)
return
null
;
Optional
<
Network
>
userCode
=
networkRepository
.
findFirstByNetworkInfoCode
(
user
.
getCode
());
if
(
userCode
.
isPresent
()
||
direct
)
{
TbCfUserInfo
userInfo
=
direct
?
user
:
userCode
.
get
().
getUserInfo
();
Bonus
bonus
=
new
Bonus
();
bonus
.
setUserInfo
(
userInfo
);
BigDecimal
v
=
amount
.
multiply
(
BigDecimal
.
valueOf
(
percent
));
v
=
v
.
divide
(
BigDecimal
.
valueOf
(
100
),
RoundingMode
.
CEILING
);
bonus
.
setAmount
(
v
);
bonus
.
setPercentage
(
percent
);
bonus
.
setOrderId
(
orderId
);
if
(
userInfo
!=
null
&&
userInfo
.
invited
()
&&
!
"000000"
.
equals
(
userInfo
.
getCode
()))
{
bonusRepository
.
save
(
bonus
);
// bonus = repository.save(bonus);
if
(
userInfo
.
hasFcm
())
{
sendNotification
(
userInfo
.
getFcm
(),
"Bonus alert !!"
,
userInfo
.
display
()
+
", You received bonus of $"
+
formatter
.
format
(
v
)
+
" in your account"
);
}
}
return
userInfo
;
}
return
null
;
}
}
}
src/main/java/com/example/afrishop_v3/util/StripePay.java
浏览文件 @
6541ea56
...
@@ -19,7 +19,7 @@ public class StripePay {
...
@@ -19,7 +19,7 @@ public class StripePay {
* @param token
* @param token
* @return
* @return
*/
*/
public
static
Charge
createCharge
(
Integer
amount
,
String
sk
,
String
token
)
throws
StripeException
{
public
static
Charge
createCharge
(
Long
amount
,
String
sk
,
String
token
)
throws
StripeException
{
Stripe
.
apiKey
=
sk
;
Stripe
.
apiKey
=
sk
;
Map
<
String
,
Object
>
chargeParams
=
new
HashMap
<>(
16
);
Map
<
String
,
Object
>
chargeParams
=
new
HashMap
<>(
16
);
chargeParams
.
put
(
"amount"
,
amount
);
chargeParams
.
put
(
"amount"
,
amount
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论