在我的项目中,我使用调度程序cron job进行每月订阅,试用期为2个月。因此,如果有任何服务在当前日期到期,将向管理员支付特定金额,从而启用每月订阅流程。话虽如此,我已经存储了信用卡参考ID从贝宝保险库在我的数据库。现在,我想用这个参考ID通过Braintree付款。是否有办法从Paypal保险库获得详细信息或使用卡参考ID直接进行支付。请帮助!
我尝试了下面的代码。但不确定如何从Paypal保险库或从数据库中获取信用卡详细信息并添加到这里。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());
}
}
}
-
使用PayPal Vault或PayPal Checkout with Vault获取nonce并将其发送到服务器。贝宝拱顶
-
服务器使用nonce创建用户。创建客户
-
服务器使用用户id或支付令牌创建事务。销售
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="paypal-button" style="padding-top: 150px"></div>
</body>
<!-- Load the client component. -->
<script src="https://js.braintreegateway.com/web/3.88.2/js/client.min.js"></script>
<!-- Load the PayPal Checkout component. -->
<script src="https://js.braintreegateway.com/web/3.88.2/js/paypal-checkout.min.js"></script>
<script>
// Create a PayPal Checkout component
// Create a client.
braintree.client.create({
authorization: '' //tokenizationKey
}, function (clientErr, clientInstance) {
// Stop if there was a problem creating the client.
// This could happen if there is a network error or if the authorization
// is invalid.
if (clientErr) {
console.error('Error creating client:', clientErr);
return;
}
// Create a PayPal Checkout component.
braintree.paypalCheckout.create({
client: clientInstance
}, function (paypalCheckoutErr, paypalCheckoutInstance) {
paypalCheckoutInstance.loadPayPalSDK({
vault: true
}, function () {
paypal.Buttons({
fundingSource: paypal.FUNDING.PAYPAL,
createBillingAgreement: function () {
return paypalCheckoutInstance.createPayment({
flow: 'vault', // Required
// The following are optional params
billingAgreementDescription: 'Your agreement description',
enableShippingAddress: false,
shippingAddressEditable: false,
});
},
onApprove: function (data, actions) {
return paypalCheckoutInstance.tokenizePayment(data, function (err, payload) {
// Submit `payload.nonce` to your server
console.log('nonce:',payload.nonce)
});
},
onCancel: function (data) {
console.log('PayPal payment canceled', JSON.stringify(data, 0, 2));
},
onError: function (err) {
console.error('PayPal error', err);
}
}).render('#paypal-button').then(function () {
// The PayPal button will be rendered in an html element with the ID
// `paypal-button`. This function will be called when the PayPal button
// is set up and ready to be used
});
});
});
});
</script>
</html>
public class BraintreePayDemo {
private static BraintreeGateway gateway = new BraintreeGateway(
Environment.SANDBOX,
"merchantId",
"publicKey",
"privateKey"
);
public static void customerCreate(){
CustomerRequest request = new CustomerRequest()
.id("test123")
.firstName("Hu")
.lastName("Z")
.paymentMethodNonce("3614a3fe-7503-0d1a-cfea-cad702094cc2");
Result<Customer> result = gateway.customer().create(request);
if(result.isSuccess()){
Customer customer = result.getTarget();
System.out.println(customer.getId());
System.out.println(customer.getPaymentMethods().get(0).getToken());
}else{
System.out.println(result.getMessage());
}
}
public static void saleByCustomerId(){
TransactionRequest request = new TransactionRequest()
.customerId("204871267")
.amount(new BigDecimal("1.00"))
.options()
.submitForSettlement(true)
.done();;
Result<Transaction> result = gateway.transaction().sale(request);
if (result.isSuccess()) {
Transaction transaction = result.getTarget();
System.out.println("Success ID: " + transaction.getId());
} else {
System.out.println("Message: " + result.getMessage());
}
}
public static void saleByPaymentMethodToken(){
TransactionRequest request = new TransactionRequest()
.paymentMethodToken("16mv5t7k")
.amount(new BigDecimal("10.00"))
.options()
.submitForSettlement(true)
.done();
Result<Transaction> result = gateway.transaction().sale(request);
if (result.isSuccess()) {
Transaction transaction = result.getTarget();
System.out.println("Success ID: " + transaction.getId());
} else {
System.out.println("Message: " + result.getMessage());
}
}
}