I'm following the PayPal REST tutorial here: https://code.tutsplus.com/articles/paypal-integration-part-2-paypal-rest-api--cms-22917
I have this in my controller method (below),I believe it's the redirect line causing the issue, the previous line reaches out to paypal to get an id without issue. payment.GetApprovalUrl() retrieves the url listed in the error message:
public ActionResult CreatePayment()
{
var userId = _userRepository.LoggedInUserId();
var cart = _cartRepository.GetCart(userId);
var payment = PayPalRepository.CreatePayment(GetBaseUrl(),
"sale",cart);
return Redirect(payment.GetApprovalUrl());
}
This action method is called from the paypal button javascript:
payment: function(resolve, reject) {
// Set up the payment here, when the buyer clicks on the button
var env = this.props.env;
var client = this.props.client;
var CREATE_PAYMENT_URL = 'http://localhost:3259/Payment/CreatePayment';
return paypal.request.post(CREATE_PAYMENT_URL)
.then(function (data) {
console.log('data: ' + data);
resolve(data.paymentID);
})
.catch(function (err) { reject(err); })
The console.log statement never executes, and the browser console shows this error: XMLHttpRequest cannot load https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-7EL20553GB692634H. No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have the controller tagged with an AllowCrossSiteJson attribute (found on stackoverflow, will update with reference to author) as follows:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var ctx = filterContext.RequestContext.HttpContext;
var origin = ctx.Request.Headers["Origin"];
var allowOrigin = !string.IsNullOrWhiteSpace(origin) ? origin : "*";
ctx.Response.AddHeader("Access-Control-Allow-Origin", allowOrigin);
ctx.Response.AddHeader("Access-Control-Allow-Headers", "*");
ctx.Response.AddHeader("Access-Control-Allow-Credentials", "true");
base.OnActionExecuting(filterContext);
}
What am I doing wrong, or not doing, in order to get by this error?