我正在开发一个基于Spring的Java应用程序,可以在其中成功集成Paypal。因此,我们有一个基于课程注册的网站,供用户注册。现在,我对付款方式做了一些修改,因此从前端,我还将收到哪个学生试图为哪个课程付款。在数据库中检查课程费用,我可以设置费用并执行付款。
问题是,哪个用户付费,这是我所不知道的。有没有办法在请求,贝宝的响应中添加自定义Integer字段,所以我可以提取该信息并执行下一步以确保学生在我的后端也被标记为“ PAID”。请让我知道...
贝宝代码:
public Payment createPayment(HttpServletRequest req, HttpServletResponse resp, String abc) {
System.out.println("String abc is "+abc);
InputStream is = TestPayment.class
.getResourceAsStream("/sdk_config.properties");
try {
PayPalResource.initConfig(is);
} catch (PayPalRESTException e) {
LOGGER.fatal(e.getMessage());
}
Payment createdPayment = null;
APIContext apiContext = null;
String accessToken = null;
try {
accessToken = GenerateAccessToken.getAccessToken();
apiContext = new APIContext(accessToken);
} catch (PayPalRESTException e) {
req.setAttribute("error", e.getMessage());
}
if (req.getParameter("PayerID") != null) {
Payment payment = new Payment();
if (req.getParameter("guid") != null) {
payment.setId(map.get(req.getParameter("guid")));
}
PaymentExecution paymentExecution = new PaymentExecution();
paymentExecution.setPayerId(req.getParameter("PayerID"));
try {
createdPayment = payment.execute(apiContext, paymentExecution);
//ResultPrinter.addResult(req, resp, "Executed The Payment", Payment.getLastRequest(), Payment.getLastResponse(), null);
} catch (PayPalRESTException e) {
// ResultPrinter.addResult(req, resp, "Executed The Payment", Payment.getLastRequest(), null, e.getMessage());
}
} else {
Details details = new Details();
details.setShipping("1");
details.setSubtotal("5");
details.setTax("1");
Amount amount = new Amount();
amount.setCurrency("EUR");
// Total must be equal to sum of shipping, tax and subtotal.
amount.setTotal("7");
amount.setDetails(details);
// ###Transaction
// A transaction defines the contract of a
// payment - what is the payment for and who
// is fulfilling it. Transaction is created with
// a `Payee` and `Amount` types
Transaction transaction = new Transaction();
transaction.setAmount(amount);
transaction
.setDescription("This is the payment transaction description.");
// ### Items
Item item = new Item();
item.setName("Online Market").setQuantity("1").setCurrency("EUR").setPrice("5");
ItemList itemList = new ItemList();
List<Item> items = new ArrayList<Item>();
items.add(item);
itemList.setItems(items);
transaction.setItemList(itemList);
// The Payment creation API requires a list of
// Transaction; add the created `Transaction`
// to a List
List<Transaction> transactions = new ArrayList<Transaction>();
transactions.add(transaction);
// ###Payer
// A resource representing a Payer that funds a payment
// Payment Method
// as 'paypal'
Payer payer = new Payer();
payer.setPaymentMethod("paypal");
// ###Payment
// A Payment Resource; create one using
// the above types and intent as 'sale'
Payment payment = new Payment();
payment.setIntent("sale");
payment.setPayer(payer);
payment.setTransactions(transactions);
// ###Redirect URLs
RedirectUrls redirectUrls = new RedirectUrls();
String guid = UUID.randomUUID().toString().replaceAll("-", "");
redirectUrls.setCancelUrl(req.getScheme() + "://"
+ req.getServerName() + ":" + req.getServerPort()
+ req.getContextPath() + "/paymentcancelled");
redirectUrls.setReturnUrl(req.getScheme() + "://"
+ req.getServerName() + ":" + req.getServerPort()
+ req.getContextPath() + "/successpayment");
payment.setRedirectUrls(redirectUrls);
// Create a payment by posting to the APIService
// using a valid AccessToken
// The return object contains the status;
try {
createdPayment = payment.create(apiContext);
/* LOGGER.info("Created payment with id = "
+ createdPayment.getId() + " and status = "
+ createdPayment.getState());*/
// ###Payment Approval Url
Iterator<Links> links = createdPayment.getLinks().iterator();
while (links.hasNext()) {
Links link = links.next();
if (link.getRel().equalsIgnoreCase("approval_url")) {
req.setAttribute("redirectURL", link.getHref());
}
}
// ResultPrinter.addResult(req, resp, "Payment with PayPal", Payment.getLastRequest(), Payment.getLastResponse(), null);
map.put(guid, createdPayment.getId());
} catch (PayPalRESTException e) {
// ResultPrinter.addResult(req, resp, "Payment with PayPal", Payment.getLastRequest(), null, e.getMessage());
}
}
return createdPayment;
}
请让我知道。非常感谢。
最佳答案
在交易对象中设置“自定义”字段
https://developer.paypal.com/webapps/developer/docs/release-notes/#updates-for-5-august-2014
关于java - Paypal:查看哪个用户付款了?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30458528/