A payment method represents transactable payment information such as credit card details or a customer's authorization to charge a PayPal or Venmo account. Payment methods belong to a customer, are securely stored in the Braintree Vault, and have a paymentMethodToken
attribute that you can store on your servers (with reduced PCI compliance burden) and later use to create transactions.
Create
Use PaymentMethod.create()
to create a payment method for an existing customer using a payment method nonce received from the client:
PaymentMethodRequest request = new PaymentMethodRequest()
.customerId("131866")
.paymentMethodNonce(nonceFromTheClient);
Result<? extends PaymentMethod> result = gateway.paymentMethod().create(request);
Alternatively, you can create a new customer with a payment method using Customer.create()
with the paymentMethodNonce()
parameter.
Once successfully created, you can use Transaction.sale()
with the paymentMethodToken()
parameter to create a transaction.
Update
Use PaymentMethod.update()
to update an existing payment method.
Make default
Use the makeDefault
option to set a payment method as the default for its customer:
PaymentMethodRequest updateRequest = new PaymentMethodRequest()
.options()
.makeDefault(true)
.done();
Result<? extends PaymentMethod> result = gateway.paymentMethod().update("the_token", updateRequest);
Billing address
Update the billing address:
PaymentMethodRequest updateRequest = new PaymentMethodRequest()
.billingAddress()
.streetAddress("100 Maple Lane")
.options()
.updateExisting(true)
.done();
Result<? extends PaymentMethod> result = gateway.paymentMethod().update("the_token", updateRequest);
If the payment method can't be found, it will throw a NotFoundException
.
You can also omit the updateExisting
option to create a new billing address for just this payment method.
See the reference and more examples of updating a payment method. If you want to update both payment method and customer information together, use Customer.update()
.
Find
Use PaymentMethod.find()
to find a payment method:
PaymentMethod paymentMethod = gateway.paymentMethod().find("token");
If the payment method can't be found, it will throw a NotFoundException
.
The return value of the PaymentMethod.find()
call will be a PaymentMethod
response object.
Delete
Use PaymentMethod.delete()
to delete a payment method:
Result<? extends PaymentMethod> result = gateway.paymentMethod().delete("the_token");
result.isSuccess();
// true
If the payment method can't be found, it will throw a NotFoundException
.