问题
I am using adaptivepayments api.The code to get paypal key is,
PayRequest payRequest = new PayRequest(requestEnvelope, "PAY_PRIMARY",
getProperties().getProperty("paypal_failed_url").trim() + bookingId, "SGD", receiverList,
getProperties().getProperty("paypal_success_url").trim() + bookingId);
payRequest.setFeesPayer("PRIMARYRECEIVER");
payRequest.setTrackingId(transactionKey);
payRequest.setReceiverList(receiverList);
AdaptivePaymentsService service = null;
Map<String, String> sdkConfig = new HashMap<String, String>();
sdkConfig.put("mode", "sandbox");
sdkConfig.put("acct1.UserName", "SG_Owner_api1.owner.com");
sdkConfig.put("acct1.Password", "WY8HAW6WGUNM3R72");
sdkConfig.put("acct1.Signature", "AFcWxV21C7fd0v3bYYYRCpSSRl31ATxmPR-EfTtPngimumXW80wNhGsQ");
sdkConfig.put("acct1.AppId", "APP-80W284485P519543T");
service = new AdaptivePaymentsService(sdkConfig);
PayResponse payResponse = null;
// ## Making API call
// Invoke the appropriate method corresponding to API in service
// wrapper object
payResponse = service.pay(payRequest);
logger.debug("-->" + payResponse.getPayKey());
logger.debug("-->" + payResponse.getResponseEnvelope().getAck());
The paykey is returned successfully, after that for the payment the paypal page should be shown for user's authorization and the sandbox link for that is , (https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey=paykey)
Foe this I am doing ,
httpResponse.addHeader("Access-Control-Allow-Origin", "*");
httpResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
httpResponse.addHeader("Access-Control-Allow-Headers",
"access-control-allow-origin,content-type, accept");
httpResponse.sendRedirect(url + payKey);
return Response.status(successResponse.getCode()).entity(successResponse).build();
The paypal palpage is not shown and I can see error in browser console,"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey=paykey"
geeting error, No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:8080' is therefore not allowed access.
Please let me know what needs to be done to get redirected to paypal page.
Thanks, Gayithri
回答1:
Well I think you're doing this with AJAX call on onClick action of the button. There is another option that you can use instead of using ajax hit place the button inside form tag and in the form action add your link & set method as get(or the one that's allowed)
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey=paykey" method="get">
<button type="submit" value="Proceed to Payment"/>
</form>
if you could provide some code for the people to help that would be great as it's just a shot in air as we don't know what you're actually trying to do.
回答2:
AFAIK this might Chrome does not support localhost to go through the Access-Control-Allow-Origin
To send Access-Control-Allow-Origin in the header, just try your localhost in your hostfile (C:\Windows\System32\drivers\etc\hosts
on Windows, /etc/hosts
on most Unixes) to some other domain, like:
127.0.0.1 localhost mydomain.com
Then if you'd access your script using mydomain.com instead of localhost, the call should succeed.
来源:https://stackoverflow.com/questions/39283885/paypal-cors-error