提交 e06c27d7 authored 作者: 吴德鹏's avatar 吴德鹏

Braintree支付

上级 f062e533
......@@ -32,6 +32,12 @@
<dependencies>
<dependency>
<groupId>com.braintreepayments.gateway</groupId>
<artifactId>braintree-java</artifactId>
<version>2.87.0</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
......
package com.example.afrishop_v3.controllers;
import com.braintreegateway.*;
import com.example.afrishop_v3.models.TbCfOrder;
import com.example.afrishop_v3.repository.TbCfOrderRepository;
import com.example.afrishop_v3.util.GatewayFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Optional;
/**
* @Auther: wudepeng
* @Date: 2021/02/27
* @Description:Braintree支付
*/
@RestController
public class BraintreeController {
private static BraintreeGateway gateway = GatewayFactory.getlisiGateway();
private final TbCfOrderRepository orderRepository;
@Value("${paypal.success_page}")
private String PAYPAL_SUCCESS_PAGE;
@Value("${paypal.failed_page}")
private String PAYPAL_FAILED_PAGE;
public BraintreeController(TbCfOrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
@PostMapping("/getToken")
public String getToken() {
//官方写new ClientTokenRequest().customerId("customerId")可以不要。
ClientTokenRequest clientTokenRequest = new ClientTokenRequest();
String clientToken = gateway.clientToken().generate(clientTokenRequest);
return clientToken;
}
@PostMapping("/pay")
public void pay(@RequestParam String orderId,
@RequestParam String nonce,
HttpServletRequest req,
HttpServletResponse resp) throws IOException {
Optional<TbCfOrder> byId = orderRepository.findById(orderId);
if (StringUtils.isBlank(orderId) || StringUtils.isBlank(nonce) || !byId.isPresent()) {
resp.sendRedirect(PAYPAL_FAILED_PAGE);
}
TbCfOrder order = byId.get();
BigDecimal orderPrice = order.getRealityPay();
String orderInfo = order.getDeliveryName() + "'s payment information";
TransactionRequest request = new TransactionRequest()
.amount(orderPrice)
.paymentMethodNonce(nonce)
.deviceData(orderInfo)
.options()
.submitForSettlement(true)
.done();
Result<Transaction> result = gateway.transaction().sale(request);
if (result.isSuccess()) {
resp.sendRedirect(PAYPAL_SUCCESS_PAGE);
} else {
resp.sendRedirect(PAYPAL_FAILED_PAGE);
}
}
}
......@@ -373,7 +373,10 @@ public class TbCfOrder {
* 使用类型 1:全场 2:分类商品 3:特定商品(use_type)
*/
boolean fullAct = activityRepository.existsByUseType(1);
if (this.open && fullAct) {
/**
* 之前app没有接活动,所以要和mobile端区分,才设置一个开关(open)
*/
if (/*this.open && */fullAct) {
Activity act = null;
List<Activity> alist = activityRepository.findAllByUseType(1);
......
package com.example.afrishop_v3.util;
import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.Environment;
/**
* @Auther: wudepeng
* @Date: 2021/02/27
* @Description:
*/
public class GatewayFactory {
public static BraintreeGateway getlisiGateway() {
BraintreeGateway gateway = new BraintreeGateway(
Environment.SANDBOX,
"g24p4hdqckyqfx37",
"4c2sxyqntc4k5967",
"cda52df9ab396a52ed0302fdeda081cb"
);
return gateway;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论