我是支付网关集成的新手。我需要将Braintree Payment Gateway
与JSF应用程序集成。有人可以展示我的java类和示例付款页面的基本示例。我看到一些客户端令牌如何生成它。我写了一个样本类和页面,如下所示,但我可能因为我不明白这个想法。
页
<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>
班级
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();
}
});
}
我在帖子中收到错误
Syntax error on token "post", @ expected before this token
答案 0 :(得分:3)
从Braintree Java SDK Implementation查看以下示例。根据要求将其集成到您的项目中:
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());
}
}
}