Laravel和Omnipay的Access-Control-Allow-Origin

时间:2013-10-06 10:54:16

标签: backbone.js laravel omnipay

我正在尝试从Backbone发出一个Ajax发布请求,调用Laravel路由“付费”,但我总是从控制台日志得到这个答案:

XMLHttpRequest cannot load https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&useraction=commit&token=EC-5S932878HU8059629. Origin http://localhost is not allowed by Access-Control-Allow-Origin. 

我尝试修改我的MAMP httpd.conf以接受跨域脚本,正如您所看到的,我已经在routes.php中添加了一条头指令。 这是我的JS代码:

Backbone.ajax({
        url:'index.php/pay',
        type:'POST',
        dataType:"json",
        data: converteditems,
        crossDomain: true,
        success:function (data) {
            if(data.error) {  // If there is an error, show the error messages
                $('.alert-error').text(data.error.text).show();
           }         
        }
    });

这是Laravel中的routes.php:

<?php
header('Access-Control-Allow-Origin: *');
Route::get('/', function()
{
return View::make('home');
});

Route::resource('products','ProductsController');

Route::resource('login', 'AuthenticationController');

Route::post('pay','PaypalController@doPay');

doPay方法以这种方式使用Omnipay包(用于c的测试目的):

public function doPay()

{          

$gateway = GatewayFactory::create('PayPal_Express');
$gateway->setUsername('blablablabla-facilitator_api1.gmail.com'); 
$gateway->setPassword('137787773');
$gateway->setSignature('AhFvPK5rU.kfQOKIwZcYO1yItmtHASGDFDFGDbY9.w');
$gateway->setTestMode('true');
$args['amount']='2.00';
$args['description']='Your purchase';
    $args['returnUrl']='http://localhost/shoppingcart/index.php/return';
    $args['cancelUrl']='http://localhost/shoppingcart/index.php/cancel';

try {

$response = $gateway->purchase($args)->send();

if ($response->isSuccessful()) {
$responsereturn=$response->getData();         
} elseif ($response->isRedirect()) {    
    $response->redirect();
} else {   
    exit($response->getMessage());
}
} catch (\Exception $e) { 
exit('internal error, log exception and display a generic message to the customer');
 }
}

我控制台标题中的更多信息:

Request URL:http://localhost/shoppingcart/public/index.php/pay
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Origin:http://localhost
Referer:http://localhost/shoppingcart/public/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36

Request URL:https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&useraction=commit&token=EC-6J290181UP558705C
Request Headersview source
Origin:http://localhost
Referer:http://localhost/shoppingcart/public/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36
Query String Parametersview sourceview URL encoded
cmd:_express-checkout
useraction:commit
token:EC-6J290181UP558705C

1 个答案:

答案 0 :(得分:0)

如果您想从Ajax发出请求,那么您不希望omnipay立即将该请求重定向到paypal。您想将客户的浏览器重定向到paypal。

因此,您需要在控制器操作中添加一些逻辑,因此如果是XHR请求,则只需返回URL以将客户转发给。

E.g。在omnipay响应中使用$ response-&gt; getRedirectUrl()而不是$ response-&gt; redirect()。然后将其放入JSON响应或其他内容中,并使用JavaScript将客户浏览器发送到该URL。