简体   繁体   中英

Braintree Gateway: Using a customerId how can I charge a vaulted credit card a one-time fee using PaymentMethodRequest or TransactionRequest?

I am using the Braintree Sping Example found here: https://github.com/braintree/braintree_spring_example .

The controller class has a method that charges a new credit card/customer a particular amount. The controller gets that amount from the POST data.

Instead of using a new card/customer, I'd like to use a vaulted credit card.

It seems that the way to do this is by creating a new PaymentMethodRequest as shown here: https://developers.braintreepayments.com/reference/request/payment-method/create/java

But when I look at the API, I do not see how to set an amount to charge with PaymentMethodRequest. Unlike the TransactionRequest class, PaymentMethodRequest does not allow the setting of an amount.

So, given a customerid, how do I charge a vaulted card a one-time fee?

Thanks for the help.

This is the method that processes the post information


    public String postForm(@RequestParam("amount") String amount, @RequestParam("payment_method_nonce") String nonce, Model model, final RedirectAttributes redirectAttributes) {
 // ... validate the amount ... 

        TransactionRequest request = new TransactionRequest()
            .amount(decimalAmount)
            .paymentMethodNonce(nonce)
            .options()
                .submitForSettlement(true)
                .done();

        Result<Transaction> result = gateway.transaction().sale(request);

    // ... process result....
    }

It seems like I should be able to do

PaymentMethodRequest request = new PaymentMethodRequest()
  .amount(decimalAmount) // this isn't actually allowed by the class
  .customerId(customer.getId())
  .token("the_token")
  .paymentMethodNonce(nonceFromTheClient);

But PaymentMethodRequest does not have that functionality.

It turns out that I was making changes in the wrong method.

To accomplish my goal, I made no changes to postForm(). Instead, I changed checkout(). I added the customerId and ClientTokenRequest lines to the code. Here I hard coded the customerId. This is just for demo purposes.

   @RequestMapping(value = "/checkouts", method = RequestMethod.GET)
    public String checkout(Model model) {
        // Two new lines for the new way  - retrieve customer specific token from the vault
          String customerId = "555555555"; // Hard coded just to make it work.
        ClientTokenRequest clientTokenRequest = new ClientTokenRequest() .customerId(customerId);

        // One modified line for the new way - gets vaulted payment methods: now use clientTokenRequest to generate clientToken
        String clientToken = gateway.clientToken().generate(clientTokenRequest);

        // old way - one time charge with all new data. generate() takes no arguments
        /* String clientToken = gateway.clientToken().generate(); */

        model.addAttribute("clientToken", clientToken);

        return "checkouts/new";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM