关于 Braintree 支付网关与 Java 的集成

Posted

技术标签:

【中文标题】关于 Braintree 支付网关与 Java 的集成【英文标题】:Regarding Integration of Braintree payment Gateway and Java 【发布时间】:2017-02-04 14:44:05 【问题描述】:

我是支付网关集成的新手。我需要将Braintree Payment Gateway 与 JSF 应用程序集成。有人可以向我展示 java 类和示例支付页面的基本示例吗?我看到了一些客户端令牌如何生成它。我写了一个示例类和页面,如下所示,但我无法制作,因为我不明白。

页面

<form id="checkout" method="post" action="/checkout">
    <div id="payment-form"></div>
    <input type="submit" value="Pay $15">
</form>

<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
    // We generated a client token for you so you can test out this code
    // immediately. In a production-ready integration, you will need to
    // generate a client token on your server (see section below).
    var clientToken = "sZWQiOmZhbHNlLCJtZXJjaGFudElkIjoiMzQ4cGs5Y2dmM2JneXcyYiIsInZlbm1vIjoib2ZmIn0=";

    braintree.setup(clientToken, "dropin", 
        container : "payment-form"
    );

</script>

import spark.Request;
import spark.Response;
import spark.Route;

import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.Environment;

public class BrainTreeController 
    private static BraintreeGateway gateway = new BraintreeGateway(
              Environment.SANDBOX,
              "your_merchant_id",
              "your_public_key",
              "your_private_key"
            );
        post(new Route("/client_token") 
          @Override
          public Object handle(Request request, Response response) 
            return gateway.clientToken().generate();
          
        );

我在帖子中遇到错误

Syntax error on token "post", @ expected before this token

【问题讨论】:

【参考方案1】:

查看Braintree Java SDK Implementation 中的以下示例。根据要求将其集成到您的项目中:

public class BrainTreeImplementation 

private static Logger logger = Logger.getLogger(BrainTreeImplementation.class.getName());

// Below are the Braintree sandbox credentials
private static BraintreeGateway gateway = null;
private static String publicKey = "YOUR_PUBLIC_KEY";
private static String privateKey = "YOUR_PRIVATE_KEY";
private static String merchantId= "YOUR_MERCHANT_ID";   

public static void main(String[] args) 
    // Initialize Braintree Connection
    gateway = connectBraintreeGateway();
    braintreeProcessing();


public static void braintreeProcessing() 

    System.out.println(" ----- BrainTree Implementation Starts --- ");

    // Generate client Token
    String clientToken = generateClientToken();
    System.out.println(" Client Token : " +clientToken);

    // Receive payment method nonce
    String nonceFromTheClient = receivePaymentMethodNonce();

    // Do payment transactions
    BigDecimal amount = new BigDecimal("5.10");
    doPaymentTransaction(nonceFromTheClient, amount);


// Connect to Braintree Gateway.
public static BraintreeGateway connectBraintreeGateway() 
    BraintreeGateway braintreeGateway = new BraintreeGateway(
            Environment.SANDBOX, merchantId, publicKey, privateKey);
    return braintreeGateway;


// Make an endpoint which return client token.
public static String generateClientToken() 
    // client token will be generated at server side and return to client
    String clientToken = gateway.clientToken().generate();
    return clientToken;


// Make an endpoint which receive payment method nonce from client and do payment.
public static String receivePaymentMethodNonce() 
     String nonceFromTheClient =  "fake-valid-mastercard-nonce";
     return nonceFromTheClient;


// Make payment 
public void String doPaymentTransaction(String paymentMethodNonce, BigDecimal amount) 

    TransactionRequest request = new TransactionRequest();
    request.amount(amount);
    request.paymentMethodNonce(paymentMethodNonce);

    CustomerRequest customerRequest = request.customer();
    customerRequest.email("cpatel@gmail.com");
    customerRequest.firstName("Chirag");
    customerRequest.lastName("Patel");

    TransactionOptionsRequest options = request.options();
    options.submitForSettlement(true);

    // Done the transaction request
    options.done();

    // Create transaction ...
    Result<Transaction> result = gateway.transaction().sale(request);
    boolean isSuccess = result.isSuccess();

    if (isSuccess) 
        Transaction transaction = result.getTarget();
        displayTransactionInfo(transaction);
     else 
        ValidationErrors errors = result.getErrors();
        validationError(errors);
    


private static void displayTransactionInfo(Transaction transaction) 
    System.out.println(" ------ Transaction Info ------ ");
    System.out.println(" Transaction Id  : " +transaction.getId());
    System.out.println(" Processor Response Text : " +transaction.getProcessorResponseText());


private static void validationError(ValidationErrors errors) 
    List<ValidationError> error = errors.getAllDeepValidationErrors();
    for (ValidationError er : error) 
        System.out.println(" error code : " + er.getCode());
        System.out.println(" error message  : " + er.getMessage());
    


【讨论】:

我如何订阅@ckpatel 我尝试了 PaymentMethod paymentmethod = generatePaymentMethod("31558878");订阅请求订阅请求 = 新订阅请求(); .paymentMethodToken(paymentmethod.getToken()) .id("Subsc31558878").planId(plan.getId()) .paymentMethodNonce(receivePaymentMethodNonce()) .neverExpires(false).billingDayOfMonth(30) .firstBillingDate(date).price( plan.getPrice()).addOns() .done();Result subscresult = gateway.subscription() .create(subscritionrequest);

以上是关于关于 Braintree 支付网关与 Java 的集成的主要内容,如果未能解决你的问题,请参考以下文章

Braintree 支付网关 - 与 Classic ASP 集成

Braintree 支付网关 - 获取客户的订阅详情

Braintree 支付网关 - 获取客户信息

如何在flutter中获得braintree支付网关的nonce?

与 ActiveMerchant 一起使用的最佳支付网关是啥?

为 Java、Python、Ruby、Node.JS 和 PHP 开放支付网关库