Return URL not set in payment success

deepakk1
Contributor
Contributor

hello,

i am not able to open after susses payment to new URL. actually i am passing return URL as Executed payment. and i am  set the return url at the time of execution of the payment. to opening new page on same page but it will not set and open it . please help

 

 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.paypal.api.payments.Links;
import com.paypal.api.payments.Payment;
import com.paypal.api.payments.RelatedResources;
import com.paypal.api.payments.Sale;
import com.paypal.api.payments.Transaction;
import com.paypal.base.rest.PayPalRESTException;
import com.paypal.orders.OrdersGetRequest;
import com.virgo.supportadmin.config.PaypalIntentMethod;
import com.virgo.supportadmin.config.PaypalPaymentIntent;
import com.virgo.supportadmin.service.PaypalServiceIF;
import com.virogo.supportadmin.util.URLUtils;

@CrossOrigin(origins = "*")
@RestController
public class PaypalController {

@Autowired
PaypalServiceIF paymentServiceIF;

private final Logger log = LoggerFactory.getLogger(this.getClass().getSimpleName());
public static final String PAYPAL_SUCCESS_URL = "pay/success";
public static final String PAYPAL_CANCEL_URL = "pay/cancel";

@RequestMapping(method = RequestMethod.POST, value = "pay")
public Map<String, Object> pay(@RequestBody Map<String,Object> paymentOrderFrom,HttpServletRequest request) {
log.info("Pay Create Approval Link");
String cancelUrl = URLUtils.getBaseURl(request) + "/" + PAYPAL_CANCEL_URL;
String successUrl = URLUtils.getBaseURl(request) + "/" + PAYPAL_SUCCESS_URL;


String value=paymentOrderFrom.get("value").toString();
String currency_code=paymentOrderFrom.get("currency_code").toString();
String description=paymentOrderFrom.get("description").toString();


Map<String, Object> linkMap= new HashMap<>();
try {
Payment payment = paymentServiceIF.createPayment(
value,
currency_code,
PaypalIntentMethod.paypal,
PaypalPaymentIntent.sale,
description,
cancelUrl,
successUrl);
//System.out.println(payment);
for(Links links : payment.getLinks()){
if(links.getRel().equals("approval_url")){
//return "redirect:" + links.getHref();
linkMap.put("Link", links.getHref());
}
}
} catch (PayPalRESTException e) {
log.error(e.getMessage());
}
return linkMap;

}

@RequestMapping(method = RequestMethod.GET, value = PAYPAL_CANCEL_URL)
public String cancelPay(){
String html="

Payment Cancel

";
return html;
}

 

@RequestMapping(method = RequestMethod.GET, value = PAYPAL_SUCCESS_URL)
public String successPay(@RequestParam("paymentId") String paymentId, @RequestParam("PayerID") String payerId) throws PayPalRESTException{
log.info("Payment Execute and get responce");
Map<String, Object> excutePayment= new HashMap<>();
String returnUrl="wwww.google.com";


Payment payment = paymentServiceIF.executePayment(paymentId, payerId,returnUrl);
//System.out.println(payment);
if(payment.getState().equals("approved")){
//return "success";
String transationId = null;
String amount=null;
for(Transaction transaction : payment.getTransactions()){
List<RelatedResources> transactionrelatedResource = transaction.getRelatedResources();
for (RelatedResources relatedResources : transactionrelatedResource) {
Sale transationsaleid=relatedResources.getSale();
transationId=transationsaleid.getId();
amount=transationsaleid.getAmount().toString();
System.out.println(transationId);
}

}


String excetuePayment=payment.toString();
excutePayment=getJsonStringToMap(excetuePayment);
List transactionList= new ArrayList<>();
transactionList.add(payment.getTransactions());

//System.out.println(payment.getTransactions());


String html="

Your payment has been successfully completedTransaction Id "+transationId+"

";
return html;
}
return "redirect :";
//return excutePayment;
}

//Convert to String to Map
public static Map<String, Object> getJsonStringToMap(String stringJson) {
Map<String, Object> map = new HashMap<String, Object>();
try {
ObjectMapper mapper = new ObjectMapper();
map = mapper.readValue(stringJson, new TypeReference<Map<String, Object>>() {
});
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}

 

 

Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.