Skip to content

Commit 592f1cc

Browse files
authored
Merge pull request #142 from flo-sch/in-context-checkout
[In-Context] Create a new PayPal Express In-Context gateway extending…
2 parents bc16ae6 + e5be45b commit 592f1cc

10 files changed

+584
-7
lines changed

src/ExpressInContextGateway.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Omnipay\PayPal;
4+
5+
/**
6+
* PayPal Express In-Context Class
7+
*/
8+
class ExpressInContextGateway extends ExpressGateway
9+
{
10+
public function getName()
11+
{
12+
return 'PayPal Express In-Context';
13+
}
14+
15+
public function authorize(array $parameters = array())
16+
{
17+
return $this->createRequest('\Omnipay\PayPal\Message\ExpressInContextAuthorizeRequest', $parameters);
18+
}
19+
20+
public function order(array $parameters = array())
21+
{
22+
return $this->createRequest('\Omnipay\PayPal\Message\ExpressInContextOrderRequest', $parameters);
23+
}
24+
}

src/Message/ExpressAuthorizeResponse.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ public function isRedirect()
2424

2525
public function getRedirectUrl()
2626
{
27-
$query = array(
28-
'cmd' => '_express-checkout',
29-
'useraction' => 'commit',
30-
'token' => $this->getTransactionReference(),
31-
);
32-
33-
return $this->getCheckoutEndpoint().'?'.http_build_query($query, '', '&');
27+
return $this->getCheckoutEndpoint().'?'.http_build_query($this->getRedirectQueryParameters(), '', '&');
3428
}
3529

3630
public function getTransactionReference()
@@ -48,6 +42,15 @@ public function getRedirectData()
4842
return null;
4943
}
5044

45+
protected function getRedirectQueryParameters()
46+
{
47+
return array(
48+
'cmd' => '_express-checkout',
49+
'useraction' => 'commit',
50+
'token' => $this->getTransactionReference(),
51+
);
52+
}
53+
5154
protected function getCheckoutEndpoint()
5255
{
5356
return $this->getRequest()->getTestMode() ? $this->testCheckoutEndpoint : $this->liveCheckoutEndpoint;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Omnipay\PayPal\Message;
4+
5+
/**
6+
* PayPal Express In-Context Authorize Request
7+
*/
8+
class ExpressInContextAuthorizeRequest extends ExpressAuthorizeRequest
9+
{
10+
protected function createResponse($data)
11+
{
12+
return $this->response = new ExpressInContextAuthorizeResponse($this, $data);
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Omnipay\PayPal\Message;
4+
5+
/**
6+
* PayPal Express In-Context Authorize Response
7+
*/
8+
class ExpressInContextAuthorizeResponse extends ExpressAuthorizeResponse
9+
{
10+
protected $liveCheckoutEndpoint = 'https://www.paypal.com/checkoutnow';
11+
protected $testCheckoutEndpoint = 'https://www.sandbox.paypal.com/checkoutnow';
12+
13+
protected function getRedirectQueryParameters()
14+
{
15+
return array(
16+
'useraction' => 'commit',
17+
'token' => $this->getTransactionReference(),
18+
);
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Omnipay\PayPal\Message;
4+
5+
/**
6+
* PayPal Express In-Context Order Request
7+
*/
8+
class ExpressInContextOrderRequest extends ExpressInContextAuthorizeRequest
9+
{
10+
public function getData()
11+
{
12+
$data = parent::getData();
13+
$data['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Order';
14+
15+
return $data;
16+
}
17+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Omnipay\PayPal;
4+
5+
use Omnipay\Tests\GatewayTestCase;
6+
7+
class ExpressInContextGatewayTest extends GatewayTestCase
8+
{
9+
/**
10+
* @var \Omnipay\PayPal\ExpressInContextGateway
11+
*/
12+
protected $gateway;
13+
14+
/**
15+
* @var array
16+
*/
17+
protected $options;
18+
19+
/**
20+
* @var array
21+
*/
22+
protected $voidOptions;
23+
24+
public function setUp()
25+
{
26+
parent::setUp();
27+
28+
$this->gateway = new ExpressInContextGateway($this->getHttpClient(), $this->getHttpRequest());
29+
30+
$this->options = array(
31+
'amount' => '10.00',
32+
'returnUrl' => 'https://www.example.com/return',
33+
'cancelUrl' => 'https://www.example.com/cancel',
34+
);
35+
$this->voidOptions = array(
36+
'transactionReference' => 'ASDFASDFASDF',
37+
);
38+
}
39+
40+
public function testAuthorizeSuccess()
41+
{
42+
$this->setMockHttpResponse('ExpressPurchaseSuccess.txt');
43+
44+
$response = $this->gateway->authorize($this->options)->send();
45+
46+
$this->assertInstanceOf('\Omnipay\PayPal\Message\ExpressInContextAuthorizeResponse', $response);
47+
$this->assertFalse($response->isPending());
48+
$this->assertFalse($response->isSuccessful());
49+
$this->assertTrue($response->isRedirect());
50+
$this->assertEquals('https://www.paypal.com/checkoutnow?useraction=commit&token=EC-42721413K79637829', $response->getRedirectUrl());
51+
}
52+
53+
public function testPurchaseSuccess()
54+
{
55+
$this->setMockHttpResponse('ExpressPurchaseSuccess.txt');
56+
57+
$response = $this->gateway->purchase($this->options)->send();
58+
59+
$this->assertInstanceOf('\Omnipay\PayPal\Message\ExpressInContextAuthorizeResponse', $response);
60+
$this->assertFalse($response->isPending());
61+
$this->assertFalse($response->isSuccessful());
62+
$this->assertTrue($response->isRedirect());
63+
$this->assertEquals('https://www.paypal.com/checkoutnow?useraction=commit&token=EC-42721413K79637829', $response->getRedirectUrl());
64+
}
65+
66+
public function testOrderSuccess()
67+
{
68+
$this->setMockHttpResponse('ExpressOrderSuccess.txt');
69+
70+
$response = $this->gateway->order($this->options)->send();
71+
72+
$this->assertInstanceOf('\Omnipay\PayPal\Message\ExpressInContextAuthorizeResponse', $response);
73+
$this->assertFalse($response->isPending());
74+
$this->assertFalse($response->isSuccessful());
75+
$this->assertTrue($response->isRedirect());
76+
$this->assertEquals('https://www.paypal.com/checkoutnow?useraction=commit&token=EC-42721413K79637829', $response->getRedirectUrl());
77+
}
78+
}

tests/Message/ExpressAuthorizeRequestTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Omnipay\PayPal\Message;
44

55
use Omnipay\Common\CreditCard;
6+
use Omnipay\PayPal\Message\ExpressAuthorizeRequest;
67
use Omnipay\PayPal\Support\InstantUpdateApi\ShippingOption;
78
use Omnipay\Tests\TestCase;
89

tests/Message/ExpressAuthorizeResponseTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Omnipay\PayPal\Message;
44

55
use Omnipay\Tests\TestCase;
6+
use Omnipay\PayPal\Message\ExpressAuthorizeResponse;
67

78
class ExpressAuthorizeResponseTest extends TestCase
89
{

0 commit comments

Comments
 (0)