0

I don't know if this is possible or if it's a complete madness but I'm trying to execute a PHP method from AJAX call using OctoberCMS Ajax Framework(I assume that this uses jQuery behind it) and is not working because I never get redirect to PayPal site. The PHP code I'm trying to get working is this one:

protected function onExecutePurchaseMethod()
{
    Omnipay::gateway('PayPal_Express');

    $params = [
        'username'  => $this->username,
        'password'  => $this->password,
        'signature' => $this->signature,
        'testMode'  => $this->sandboxMode,
        'amount'    => Session::get('amountToReload'),
        'cancelUrl' => url( 'payment/step4', "", $secure = null ),
        'returnUrl' => url( 'payment/step2', "", $secure = null ),
        'currency'  => 'USD'
    ];

    $response = Omnipay::purchase($params)->send();

    if ($response->isSuccessful()) {
        var_dump($response);
    } else {
        var_dump($response->getMessage());
    }
}

What is happening since none redirect to PayPal is executed and page is getting stuck many times forcing me to close the browser and reopen again, no method is executed and no visible errors. It's possible to do what I'm trying to do? Is not a madness? If it's possible where is my error?

As extra info I'm using Barryvdh Laravel-omnipay package for handle Omnipay from within Laravel.

ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • I haven used Omnipay, but Express Checkout would require a redirect out to paypal, so how are you handling that in your AJAX? XMLHTTPRequest will not follow a HTTP redirect. – prodigitalson Nov 13 '14 at 02:28
  • @prodigitalson and that's exactly what is happening "no redirect to PayPal" so how I should handle this? How do I fit this code into OctoberCMS page lifecycle? That's what I'm going through Ajax instead of typically way – ReynierPM Nov 13 '14 at 02:32
  • I don't know anything about OctoberCMS but normally you would not do the express request over ajax. Is ajax required because of the CMS or can you handle an express payment method with a straight POST? – prodigitalson Nov 13 '14 at 02:41
  • @prodigitalson that's the main issue, they have a weird flow that's not the typically as I said before, I wish not to use the Ajax framework but I don't know how to call a method inside a component – ReynierPM Nov 13 '14 at 02:46
  • Ok, so it sounds like your real question needs to be `How do call a method inside a component in OctoberCMS?` Cause I think thats whats going to have to happen here :-) – prodigitalson Nov 13 '14 at 03:13

1 Answers1

0

After looking briefly through the documentation, my best guess is that you're missing a required field for the purchase() method. I believe you need a card parameter (even if it's an invalid one) to get it to process.

cchapman
  • 3,269
  • 10
  • 50
  • 68
  • [This](http://stackoverflow.com/a/20779741/719427) answer says is not neccesary where did you read that one? Why if I will pay through PayPal I should use a CC even if it's a faker one? – ReynierPM Nov 13 '14 at 01:41
  • Ahh, you're right. `card` isn't required, sorry. Here is the required fields I did find [here](https://github.com/thephpleague/omnipay) under Gateway Methods: `token, amount, currency, description, transactionId, clientIp, returnUrl, cancelUrl`. Apologies. – cchapman Nov 13 '14 at 01:44
  • Hmmm I don't think that's the problem since not all those parameters are required. Take a look [here](https://github.com/thephpleague/omnipay-paypal/blob/master/src/ProGateway.php#L17-L25) you'll see a few only and also there is this comment `All gateway methods take an $options array as an argument. Each gateway differs in which parameters are required, and the gateway will throw InvalidRequestException if you omit any required parameters.` at docs and I don't get any `InvalidRequestException` – ReynierPM Nov 13 '14 at 01:48
  • I see now. There goes to not reading the whole documentation :/ – cchapman Nov 13 '14 at 01:51
  • Have you tried setting the `gateway` (from the beginning of [your link](http://stackoverflow.com/questions/20756067/omnipay-paypal-integration-with-laravel-4/20779741#20779741)) and then calling the purchase method on that? – cchapman Nov 13 '14 at 01:53
  • Hmm, I don't think that is quite what I see in the documentation. With what you have I don't think it's saving any state. Try $gateway = Omnipay::create('PayPal_Express'); and actually call $response = $gateway->purchase($params). Otherwise I'm sorry if I wasn't any help. – cchapman Nov 13 '14 at 02:11
  • Nop, that doesn't help either I tried and no redirect to PayPal site happen, maybe is for the Ajax call I'm not sure this is allowed – ReynierPM Nov 13 '14 at 02:13