Open Monograph Press  3.3.0
vendor/omnipay/paypal/tests/ExpressGatewayTest.php
1 <?php
2 
3 namespace Omnipay\PayPal;
4 
5 use Omnipay\Tests\GatewayTestCase;
6 
7 class ExpressGatewayTest extends GatewayTestCase
8 {
12  protected $gateway;
13 
17  protected $options;
18 
22  protected $voidOptions;
23 
24  public function setUp()
25  {
26  parent::setUp();
27 
28  $this->gateway = new ExpressGateway($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\ExpressAuthorizeResponse', $response);
47  $this->assertFalse($response->isPending());
48  $this->assertFalse($response->isSuccessful());
49  $this->assertTrue($response->isRedirect());
50  $this->assertEquals('https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=EC-42721413K79637829', $response->getRedirectUrl());
51  }
52 
53  public function testAuthorizeFailure()
54  {
55  $this->setMockHttpResponse('ExpressPurchaseFailure.txt');
56 
57  $response = $this->gateway->authorize($this->options)->send();
58 
59  $this->assertFalse($response->isPending());
60  $this->assertFalse($response->isSuccessful());
61  $this->assertFalse($response->isRedirect());
62  $this->assertNull($response->getTransactionReference());
63  $this->assertSame('This transaction cannot be processed. The amount to be charged is zero.', $response->getMessage());
64  }
65 
66  public function testPurchaseSuccess()
67  {
68  $this->setMockHttpResponse('ExpressPurchaseSuccess.txt');
69 
70  $response = $this->gateway->purchase($this->options)->send();
71 
72  $this->assertInstanceOf('\Omnipay\PayPal\Message\ExpressAuthorizeResponse', $response);
73  $this->assertFalse($response->isPending());
74  $this->assertFalse($response->isSuccessful());
75  $this->assertTrue($response->isRedirect());
76  $this->assertEquals('https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=EC-42721413K79637829', $response->getRedirectUrl());
77  }
78 
79  public function testPurchaseFailure()
80  {
81  $this->setMockHttpResponse('ExpressPurchaseFailure.txt');
82 
83  $response = $this->gateway->purchase($this->options)->send();
84 
85  $this->assertFalse($response->isPending());
86  $this->assertFalse($response->isSuccessful());
87  $this->assertFalse($response->isRedirect());
88  $this->assertNull($response->getTransactionReference());
89  $this->assertSame('This transaction cannot be processed. The amount to be charged is zero.', $response->getMessage());
90  }
91 
92  public function testOrderSuccess()
93  {
94  $this->setMockHttpResponse('ExpressOrderSuccess.txt');
95 
96  $response = $this->gateway->order($this->options)->send();
97 
98  $this->assertInstanceOf('\Omnipay\PayPal\Message\ExpressAuthorizeResponse', $response);
99  $this->assertFalse($response->isPending());
100  $this->assertFalse($response->isSuccessful());
101  $this->assertTrue($response->isRedirect());
102  $this->assertEquals('https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=EC-42721413K79637829', $response->getRedirectUrl());
103  }
104 
105  public function testOrderFailure()
106  {
107  $this->setMockHttpResponse('ExpressOrderFailure.txt');
108 
109  $response = $this->gateway->order($this->options)->send();
110 
111  $this->assertFalse($response->isPending());
112  $this->assertFalse($response->isSuccessful());
113  $this->assertFalse($response->isRedirect());
114  $this->assertNull($response->getTransactionReference());
115  $this->assertSame('This transaction cannot be processed. The amount to be charged is zero.', $response->getMessage());
116  }
117 
118  public function testVoidSuccess()
119  {
120  $this->setMockHttpResponse('ExpressVoidSuccess.txt');
121 
122  $response = $this->gateway->void($this->voidOptions)->send();
123 
124  $this->assertInstanceOf('\Omnipay\PayPal\Message\Response', $response);
125  $this->assertTrue($response->isSuccessful());
126  $this->assertFalse($response->isRedirect());
127  $this->assertEquals('ASDFASDFASDF', $response->getTransactionReference());
128  }
129 
130  public function testVoidFailure()
131  {
132  $this->setMockHttpResponse('ExpressVoidFailure.txt');
133 
134  $response = $this->gateway->void($this->voidOptions)->send();
135 
136  $this->assertInstanceOf('\Omnipay\PayPal\Message\Response', $response);
137  $this->assertFalse($response->isSuccessful());
138  }
139 
140  public function testFetchCheckout()
141  {
142  $options = array('token' => 'abc123');
143  $request = $this->gateway->fetchCheckout($options);
144 
145  $this->assertInstanceOf('\Omnipay\PayPal\Message\ExpressFetchCheckoutRequest', $request);
146  $this->assertSame('abc123', $request->getToken());
147  }
148 
150  {
151  $this->setMockHttpResponse('ExpressCompletePurchaseFailureRedirect.txt');
152 
153  $response = $this->gateway->completePurchase($this->options)->send();
154 
155  $this->assertFalse($response->isPending());
156  $this->assertFalse($response->isSuccessful());
157  $this->assertTrue($response->isRedirect());
158  $this->assertEquals('ASDFASDFASDF', $response->getTransactionReference());
159  $this->assertSame('This transaction couldn\'t be completed. Please redirect your customer to PayPal.', $response->getMessage());
160  }
161 
162  public function testCompletePurchaseHttpOptions()
163  {
164  $this->setMockHttpResponse('ExpressPurchaseSuccess.txt');
165 
166  $this->getHttpRequest()->query->replace(array(
167  'token' => 'GET_TOKEN',
168  'PayerID' => 'GET_PAYERID',
169  ));
170 
171  $response = $this->gateway->completePurchase(array(
172  'amount' => '10.00',
173  'currency' => 'EUR',
174  ))->send();
175 
176  $httpRequests = $this->getMockedRequests();
177  $httpRequest = $httpRequests[0];
178  parse_str((string)$httpRequest->getBody(), $postData);
179  $this->assertSame('GET_TOKEN', $postData['TOKEN']);
180  $this->assertSame('GET_PAYERID', $postData['PAYERID']);
181  }
182 
183  public function testCompletePurchaseCustomOptions()
184  {
185  $this->setMockHttpResponse('ExpressPurchaseSuccess.txt');
186 
187  // Those values should not be used if custom token or payerid are passed
188  $this->getHttpRequest()->query->replace(array(
189  'token' => 'GET_TOKEN',
190  'PayerID' => 'GET_PAYERID',
191  ));
192 
193  $response = $this->gateway->completePurchase(array(
194  'amount' => '10.00',
195  'currency' => 'EUR',
196  'token' => 'CUSTOM_TOKEN',
197  'payerid' => 'CUSTOM_PAYERID',
198  ))->send();
199 
200  $httpRequests = $this->getMockedRequests();
201  $httpRequest = $httpRequests[0];
202  parse_str((string)$httpRequest->getBody(), $postData);
203  $this->assertSame('CUSTOM_TOKEN', $postData['TOKEN']);
204  $this->assertSame('CUSTOM_PAYERID', $postData['PAYERID']);
205  }
206 
207  public function testTransactionSearch()
208  {
209  $transactionSearch = $this->gateway->transactionSearch(array(
210  'startDate' => '2015-01-01',
211  'endDate' => '2015-12-31',
212  ));
213 
214  $this->assertInstanceOf('\Omnipay\PayPal\Message\ExpressTransactionSearchRequest', $transactionSearch);
215  $this->assertInstanceOf('\DateTime', $transactionSearch->getStartDate());
216  $this->assertInstanceOf('\DateTime', $transactionSearch->getEndDate());
217  }
218 }
Omnipay\PayPal\ExpressGatewayTest\testOrderSuccess
testOrderSuccess()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:101
Omnipay\PayPal\ExpressGatewayTest\testTransactionSearch
testTransactionSearch()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:216
Omnipay\PayPal
Definition: lib/vendor/omnipay/paypal/src/ExpressGateway.php:3
Omnipay\PayPal\ExpressGatewayTest\testCompletePurchaseFailureRedirect
testCompletePurchaseFailureRedirect()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:158
Omnipay\PayPal\ExpressGatewayTest\$gateway
$gateway
Definition: lib/vendor/omnipay/paypal/tests/ExpressGatewayTest.php:15
Omnipay\PayPal\ExpressGatewayTest\setUp
setUp()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:33
Omnipay\PayPal\ExpressGatewayTest\testAuthorizeSuccess
testAuthorizeSuccess()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:49
Omnipay\PayPal\ExpressGatewayTest\testOrderFailure
testOrderFailure()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:114
Omnipay\PayPal\ExpressGateway
Definition: lib/vendor/omnipay/paypal/src/ExpressGateway.php:8
Omnipay\PayPal\ExpressGatewayTest\testAuthorizeFailure
testAuthorizeFailure()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:62
Omnipay\PayPal\ExpressGatewayTest\testFetchCheckout
testFetchCheckout()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:149
Omnipay\PayPal\ExpressGatewayTest\testPurchaseFailure
testPurchaseFailure()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:88
Omnipay\PayPal\ExpressGatewayTest\$options
$options
Definition: lib/vendor/omnipay/paypal/tests/ExpressGatewayTest.php:23
Omnipay\PayPal\ExpressGatewayTest\testVoidFailure
testVoidFailure()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:139
Omnipay\PayPal\ExpressGatewayTest\testCompletePurchaseCustomOptions
testCompletePurchaseCustomOptions()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:192
Omnipay\PayPal\ExpressGatewayTest\testPurchaseSuccess
testPurchaseSuccess()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:75
Omnipay\PayPal\ExpressGatewayTest\testCompletePurchaseHttpOptions
testCompletePurchaseHttpOptions()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:171
Omnipay\PayPal\ExpressGatewayTest\testVoidSuccess
testVoidSuccess()
Definition: vendor/omnipay/paypal/tests/ExpressGatewayTest.php:127
Omnipay\PayPal\ExpressGatewayTest\$voidOptions
$voidOptions
Definition: lib/vendor/omnipay/paypal/tests/ExpressGatewayTest.php:31