Это способ оплаты через API Braintree:
@Test
public void payout() {
// credit card details used here are only valid for test purpose
String userId = BraintreePayments.USER_ID;
String cardNumber = "4111111111111111";
String ccv = "100";
String expirationDate = "05/2010";
BigDecimal amount = new BigDecimal("2.02");
// make a credit card payout
Result<Transaction> transactionResult = createTransaction(userId, cardNumber, ccv, expirationDate, amount);
BraintreePayments.showResult(transactionResult);
Assert.assertTrue(transactionResult.isSuccess());
}
/**
* Makes a payout via Braintree Payment API.
*
* @param userId User identifier number.
* @param cardNumber Credit card number.
* @param ccv Credit card's CCV code.
* @param expirationDate Expiration date of the credit card.
* @param amount Amount to deposit.
* @return A tran
*/
private Result<Transaction> createTransaction(final String userId,
final String cardNumber,
final String ccv,
final String expirationDate,
final BigDecimal amount) {
String customerId = BraintreePayments.customerIdGenerator(userId);
String orderId = UUID.randomUUID().toString();
System.out.println(String.format("Order ID: %s", orderId));
createCustomerIfNotExist(userId, customerId);
// make a credit card payout
TransactionRequest transactionRequest = new TransactionRequest()
.customerId(customerId)
.orderId(orderId)
.creditCard().number(cardNumber).cvv(ccv).expirationDate(expirationDate).cardholderName(userId).done()
.amount(amount)
.options().submitForSettlement(true)
.done();
Result<Transaction> transactionResult = GATEWAY.transaction().sale(transactionRequest);
// save credit card if the transaction was success
if (transactionResult.isSuccess()) {
CreditCardRequest creditCardRequest = new CreditCardRequest().
customerId(customerId).
number(cardNumber).
cvv(ccv).
expirationDate(expirationDate).
cardholderName(userId);
GATEWAY.creditCard().create(creditCardRequest);
}
return transactionResult;
}
/**
* Create customer if it does not exist.
*
* @param userId User Identification number.
* @param customerId Braintree customer ID.
*/
private void createCustomerIfNotExist(String userId, String customerId) {
try {
BraintreePayments.findCustomer(customerId);
} catch (com.braintreegateway.exceptions.NotFoundException e ) {
System.out.println(String.format("User %s does not exist. Let's create it.", userId));
CustomerRequest request = BraintreePayments.createCustomerRequest(userId);
BraintreeGateway gateway = BraintreePayments.initApi();
Result<Customer> result = gateway.customer().create(request);
BraintreePayments.showResult(result);
}
}
Приведенный выше код отлично работает для меня в качестве тестового кода.
Надеюсь, это поможет вам.