Hi, I created a very simple program with the help of paypal's api docs. The code is fairly simple. Program works correctly as it should, it even shows the success response when payment is executed through return url. But the amount is not being added from seller account to the merchant account. Any help would be appreciated. Thanks! Here's my code. @Configuration
public class PaypalConfig {
@Value("${paypal.client.app}")
private String clientId;
@Value("${paypal.client.secret}")
private String clientSecret;
@Value("${paypal.mode}")
private String mode;
@Bean
public Map<String, String> paypalSdkConfig(){
Map<String, String> sdkConfig = new HashMap<>();
sdkConfig.put("mode", mode);
return sdkConfig;
}
@Bean
public OAuthTokenCredential authTokenCredential() {
return new OAuthTokenCredential(clientId,clientSecret,paypalSdkConfig());
}
@Bean
public APIContext apiContext() throws PayPalRESTException{
@SuppressWarnings("deprecation")
APIContext apiContext = new APIContext(authTokenCredential().getAccessToken());
apiContext.setConfigurationMap(paypalSdkConfig());
return apiContext;
}
}
@Autowired
private APIContext apiContext;
@GetMapping()
public String paypalPay(HttpServletRequest req) {
// Set payment details
Details details = new Details();
details.setShipping("1");
details.setSubtotal("5");
details.setTax("1");
// Payment amount
Amount amount = new Amount();
amount.setCurrency("USD");
// Total must be equal to sum of shipping, tax and subtotal.
amount.setTotal("7");
amount.setDetails(details);
// Transaction information
Transaction transaction = new Transaction();
transaction.setAmount(amount);
transaction.setDescription("This is the payment transaction description.");
// ### Items
Item item = new Item();
item.setName("Ground Coffee 40 oz").setQuantity("1").setCurrency("USD").setPrice("5");
ItemList itemList = new ItemList();
List items = new ArrayList();
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 transactions = new ArrayList();
transactions.add(transaction);
// ###Payer
// A resource representing a Payer that funds a payment
// Payment Method
// as 'paypal'
Payer payer = new Payer();
payer.setPaymentMethod("paypal");
// Add payment details
Payment payment = new Payment();
payment.setIntent("sale");
payment.setPayer(payer);
// payment.setRedirectUrls(redirectUrls);
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() + "/paypal/payment/cancel?guid=" + guid);
redirectUrls.setReturnUrl(req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort()
+ req.getContextPath() + "/paypal/payment/success?guid=" + guid);
payment.setRedirectUrls(redirectUrls);
// Create payment
try {
Payment createdPayment = payment.create(apiContext);
// ###Payment Approval Url
Iterator links = createdPayment.getLinks().iterator();
while (links.hasNext()) {
Links link = links.next();
if (link.getRel().equalsIgnoreCase("approval_url")) {
// System.out.println(link.getHref());
// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
// req.setAttribute("redirectURL", link.getHref());
return "redirect:" + link.getHref();
}
}
} catch (PayPalRESTException e) {
System.err.println(e.getDetails());
return "redirect:/paypal/error";
}
// System.out.println("Success url: " + redirectUrls.getReturnUrl());
// System.out.println("Payment id"+ createdPayment.getId());
// return createdPayment;
return "redirect:/paypal/error";
}
@GetMapping("/payment/success")
@ResponseBody
public String executePayment(HttpServletRequest req) {
APIContext apiContext = new APIContext(clientID, clientSecret, mode);
Payment payment = new Payment();
payment.setId(req.getParameter("paymentId"));
PaymentExecution paymentExecution = new PaymentExecution();
paymentExecution.setPayerId(req.getParameter("PayerID"));
try {
Payment createdPayment = payment.execute(apiContext, paymentExecution);
System.out.println(createdPayment);
return "Success";
} catch (PayPalRESTException e) {
System.err.println(e.getDetails());
return "Failed";
}
}
... View more