问题
I am new to payment gateway integration.I need to integrate Braintree Payment Gateway
with JSF Application. Can someone show my a basic example of the java class and sample payment Page. I saw some client Token how to generate it. I wrote a sample class and page like below,But i coudnt make as i dont get the idea.
page
<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>
Class
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();
}
});
}
I get a error in post
Syntax error on token "post", @ expected before this token
回答1:
Checkout the below sample from the Braintree Java SDK Implementation. Integrate this in your project according to requirements:
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());
}
}
}
来源:https://stackoverflow.com/questions/39716674/regarding-integration-of-braintree-payment-gateway-and-java