Skip to content

Commit

Permalink
Merge pull request #142 from flo-sch/in-context-checkout
Browse files Browse the repository at this point in the history
[In-Context] Create a new PayPal Express In-Context gateway extending…
  • Loading branch information
delatbabel authored Sep 6, 2016
2 parents bc16ae6 + e5be45b commit 592f1cc
Show file tree
Hide file tree
Showing 10 changed files with 584 additions and 7 deletions.
24 changes: 24 additions & 0 deletions src/ExpressInContextGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Omnipay\PayPal;

/**
* PayPal Express In-Context Class
*/
class ExpressInContextGateway extends ExpressGateway
{
public function getName()
{
return 'PayPal Express In-Context';
}

public function authorize(array $parameters = array())
{
return $this->createRequest('\Omnipay\PayPal\Message\ExpressInContextAuthorizeRequest', $parameters);
}

public function order(array $parameters = array())
{
return $this->createRequest('\Omnipay\PayPal\Message\ExpressInContextOrderRequest', $parameters);
}
}
17 changes: 10 additions & 7 deletions src/Message/ExpressAuthorizeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ public function isRedirect()

public function getRedirectUrl()
{
$query = array(
'cmd' => '_express-checkout',
'useraction' => 'commit',
'token' => $this->getTransactionReference(),
);

return $this->getCheckoutEndpoint().'?'.http_build_query($query, '', '&');
return $this->getCheckoutEndpoint().'?'.http_build_query($this->getRedirectQueryParameters(), '', '&');
}

public function getTransactionReference()
Expand All @@ -48,6 +42,15 @@ public function getRedirectData()
return null;
}

protected function getRedirectQueryParameters()
{
return array(
'cmd' => '_express-checkout',
'useraction' => 'commit',
'token' => $this->getTransactionReference(),
);
}

protected function getCheckoutEndpoint()
{
return $this->getRequest()->getTestMode() ? $this->testCheckoutEndpoint : $this->liveCheckoutEndpoint;
Expand Down
14 changes: 14 additions & 0 deletions src/Message/ExpressInContextAuthorizeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal Express In-Context Authorize Request
*/
class ExpressInContextAuthorizeRequest extends ExpressAuthorizeRequest
{
protected function createResponse($data)
{
return $this->response = new ExpressInContextAuthorizeResponse($this, $data);
}
}
20 changes: 20 additions & 0 deletions src/Message/ExpressInContextAuthorizeResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal Express In-Context Authorize Response
*/
class ExpressInContextAuthorizeResponse extends ExpressAuthorizeResponse
{
protected $liveCheckoutEndpoint = 'https://www.paypal.com/checkoutnow';
protected $testCheckoutEndpoint = 'https://www.sandbox.paypal.com/checkoutnow';

protected function getRedirectQueryParameters()
{
return array(
'useraction' => 'commit',
'token' => $this->getTransactionReference(),
);
}
}
17 changes: 17 additions & 0 deletions src/Message/ExpressInContextOrderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal Express In-Context Order Request
*/
class ExpressInContextOrderRequest extends ExpressInContextAuthorizeRequest
{
public function getData()
{
$data = parent::getData();
$data['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Order';

return $data;
}
}
78 changes: 78 additions & 0 deletions tests/ExpressInContextGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Omnipay\PayPal;

use Omnipay\Tests\GatewayTestCase;

class ExpressInContextGatewayTest extends GatewayTestCase
{
/**
* @var \Omnipay\PayPal\ExpressInContextGateway
*/
protected $gateway;

/**
* @var array
*/
protected $options;

/**
* @var array
*/
protected $voidOptions;

public function setUp()
{
parent::setUp();

$this->gateway = new ExpressInContextGateway($this->getHttpClient(), $this->getHttpRequest());

$this->options = array(
'amount' => '10.00',
'returnUrl' => 'https://www.example.com/return',
'cancelUrl' => 'https://www.example.com/cancel',
);
$this->voidOptions = array(
'transactionReference' => 'ASDFASDFASDF',
);
}

public function testAuthorizeSuccess()
{
$this->setMockHttpResponse('ExpressPurchaseSuccess.txt');

$response = $this->gateway->authorize($this->options)->send();

$this->assertInstanceOf('\Omnipay\PayPal\Message\ExpressInContextAuthorizeResponse', $response);
$this->assertFalse($response->isPending());
$this->assertFalse($response->isSuccessful());
$this->assertTrue($response->isRedirect());
$this->assertEquals('https://www.paypal.com/checkoutnow?useraction=commit&token=EC-42721413K79637829', $response->getRedirectUrl());
}

public function testPurchaseSuccess()
{
$this->setMockHttpResponse('ExpressPurchaseSuccess.txt');

$response = $this->gateway->purchase($this->options)->send();

$this->assertInstanceOf('\Omnipay\PayPal\Message\ExpressInContextAuthorizeResponse', $response);
$this->assertFalse($response->isPending());
$this->assertFalse($response->isSuccessful());
$this->assertTrue($response->isRedirect());
$this->assertEquals('https://www.paypal.com/checkoutnow?useraction=commit&token=EC-42721413K79637829', $response->getRedirectUrl());
}

public function testOrderSuccess()
{
$this->setMockHttpResponse('ExpressOrderSuccess.txt');

$response = $this->gateway->order($this->options)->send();

$this->assertInstanceOf('\Omnipay\PayPal\Message\ExpressInContextAuthorizeResponse', $response);
$this->assertFalse($response->isPending());
$this->assertFalse($response->isSuccessful());
$this->assertTrue($response->isRedirect());
$this->assertEquals('https://www.paypal.com/checkoutnow?useraction=commit&token=EC-42721413K79637829', $response->getRedirectUrl());
}
}
1 change: 1 addition & 0 deletions tests/Message/ExpressAuthorizeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Omnipay\PayPal\Message;

use Omnipay\Common\CreditCard;
use Omnipay\PayPal\Message\ExpressAuthorizeRequest;
use Omnipay\PayPal\Support\InstantUpdateApi\ShippingOption;
use Omnipay\Tests\TestCase;

Expand Down
1 change: 1 addition & 0 deletions tests/Message/ExpressAuthorizeResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Omnipay\PayPal\Message;

use Omnipay\Tests\TestCase;
use Omnipay\PayPal\Message\ExpressAuthorizeResponse;

class ExpressAuthorizeResponseTest extends TestCase
{
Expand Down
Loading

0 comments on commit 592f1cc

Please sign in to comment.