Open Monograph Press  3.3.0
vendor/omnipay/paypal/tests/RestGatewayTest.php
1 <?php
2 
3 namespace Omnipay\PayPal;
4 
5 use Omnipay\Tests\GatewayTestCase;
7 
8 class RestGatewayTest extends GatewayTestCase
9 {
11  public $gateway;
12 
14  public $options;
15 
17  public $subscription_options;
18 
19  public function setUp()
20  {
21  parent::setUp();
22 
23  $this->gateway = new RestGateway($this->getHttpClient(), $this->getHttpRequest());
24  $this->gateway->setToken('TEST-TOKEN-123');
25  $this->gateway->setTokenExpires(time() + 600);
26 
27  $this->options = array(
28  'amount' => '10.00',
29  'card' => new CreditCard(array(
30  'firstName' => 'Example',
31  'lastName' => 'User',
32  'number' => '4111111111111111',
33  'expiryMonth' => '12',
34  'expiryYear' => date('Y'),
35  'cvv' => '123',
36  )),
37  );
38 
39  $this->subscription_options = array(
40  'transactionReference' => 'ABC-1234',
41  'description' => 'Description goes here',
42  );
43  }
44 
45  public function testBearerToken()
46  {
47  $this->gateway->setToken('');
48  $this->setMockHttpResponse('RestTokenSuccess.txt');
49 
50  $this->assertFalse($this->gateway->hasToken());
51  $this->assertEquals('A015GQlKQ6uCRzLHSGRliANi59BHw6egNVKEWRnxvTwvLr0', $this->gateway->getToken()); // triggers request
52  $this->assertEquals(time() + 28800, $this->gateway->getTokenExpires());
53  $this->assertTrue($this->gateway->hasToken());
54  }
55 
56  public function testBearerTokenReused()
57  {
58  $this->setMockHttpResponse('RestTokenSuccess.txt');
59  $this->gateway->setToken('MYTOKEN');
60  $this->gateway->setTokenExpires(time() + 60);
61 
62  $this->assertTrue($this->gateway->hasToken());
63  $this->assertEquals('MYTOKEN', $this->gateway->getToken());
64  }
65 
66  public function testBearerTokenExpires()
67  {
68  $this->setMockHttpResponse('RestTokenSuccess.txt');
69  $this->gateway->setToken('MYTOKEN');
70  $this->gateway->setTokenExpires(time() - 60);
71 
72  $this->assertFalse($this->gateway->hasToken());
73  $this->assertEquals('A015GQlKQ6uCRzLHSGRliANi59BHw6egNVKEWRnxvTwvLr0', $this->gateway->getToken());
74  }
75 
76  public function testAuthorize()
77  {
78  $this->setMockHttpResponse('RestPurchaseSuccess.txt');
79 
80  $response = $this->gateway->authorize($this->options)->send();
81 
82  $this->assertTrue($response->isSuccessful());
83  $this->assertEquals('44E89981F8714392Y', $response->getTransactionReference());
84  $this->assertNull($response->getMessage());
85  }
86 
87  public function testPurchase()
88  {
89  $this->setMockHttpResponse('RestPurchaseSuccess.txt');
90 
91  $response = $this->gateway->purchase($this->options)->send();
92 
93  $this->assertTrue($response->isSuccessful());
94  $this->assertEquals('44E89981F8714392Y', $response->getTransactionReference());
95  $this->assertNull($response->getMessage());
96  }
97 
98  public function testCapture()
99  {
100  $request = $this->gateway->capture(array(
101  'transactionReference' => 'abc123',
102  'amount' => 10.00,
103  'currency' => 'AUD',
104  ));
105 
106  $this->assertInstanceOf('\Omnipay\PayPal\Message\RestCaptureRequest', $request);
107  $this->assertSame('abc123', $request->getTransactionReference());
108  $endPoint = $request->getEndpoint();
109  $this->assertSame('https://api.paypal.com/v1/payments/authorization/abc123/capture', $endPoint);
110  $data = $request->getData();
111  $this->assertNotEmpty($data);
112  }
113 
114  public function testRefund()
115  {
116  $request = $this->gateway->refund(array(
117  'transactionReference' => 'abc123',
118  'amount' => 10.00,
119  'currency' => 'AUD',
120  ));
121 
122  $this->assertInstanceOf('\Omnipay\PayPal\Message\RestRefundRequest', $request);
123  $this->assertSame('abc123', $request->getTransactionReference());
124  $endPoint = $request->getEndpoint();
125  $this->assertSame('https://api.paypal.com/v1/payments/sale/abc123/refund', $endPoint);
126  $data = $request->getData();
127  $this->assertNotEmpty($data);
128  }
129 
130  public function testFullRefund()
131  {
132  $request = $this->gateway->refund(array(
133  'transactionReference' => 'abc123',
134  ));
135 
136  $this->assertInstanceOf('\Omnipay\PayPal\Message\RestRefundRequest', $request);
137  $this->assertSame('abc123', $request->getTransactionReference());
138  $endPoint = $request->getEndpoint();
139  $this->assertSame('https://api.paypal.com/v1/payments/sale/abc123/refund', $endPoint);
140  $data = $request->getData();
141 
142  // we're expecting an empty object here
143  $json = json_encode($data);
144  $this->assertEquals('{}', $json);
145  }
146 
147  public function testFetchTransaction()
148  {
149  $request = $this->gateway->fetchTransaction(array('transactionReference' => 'abc123'));
150 
151  $this->assertInstanceOf('\Omnipay\PayPal\Message\RestFetchTransactionRequest', $request);
152  $this->assertSame('abc123', $request->getTransactionReference());
153  $data = $request->getData();
154  $this->assertEmpty($data);
155  }
156 
157  public function testListPlan()
158  {
159  $request = $this->gateway->listPlan(array(
160  'page' => 0,
161  'status' => 'ACTIVE',
162  'pageSize' => 10, //number of plans in a single page
163  'totalRequired' => 'yes'
164  ));
165 
166  $this->assertInstanceOf('\Omnipay\PayPal\Message\RestListPlanRequest', $request);
167  $this->assertSame(0, $request->getPage());
168  $this->assertSame('ACTIVE', $request->getStatus());
169  $this->assertSame(10, $request->getPageSize());
170  $this->assertSame('yes', $request->getTotalRequired());
171 
172  $endPoint = $request->getEndpoint();
173  $this->assertSame('https://api.paypal.com/v1/payments/billing-plans', $endPoint);
174  $data = $request->getData();
175  $this->assertNotEmpty($data);
176  }
177 
178  public function testFetchPurchase()
179  {
180  $request = $this->gateway->fetchPurchase(array('transactionReference' => 'abc123'));
181 
182  $this->assertInstanceOf('\Omnipay\PayPal\Message\RestFetchPurchaseRequest', $request);
183  $this->assertSame('abc123', $request->getTransactionReference());
184  $data = $request->getData();
185  $this->assertEmpty($data);
186  }
187 
188  public function testListPurchase()
189  {
190  $request = $this->gateway->listPurchase(array(
191  'count' => 15,
192  'startId' => 'PAY123',
193  'startIndex' => 1,
194  'startTime' => '2015-09-07T00:00:00Z',
195  'endTime' => '2015-09-08T00:00:00Z',
196  ));
197 
198  $this->assertInstanceOf('\Omnipay\PayPal\Message\RestListPurchaseRequest', $request);
199  $this->assertSame(15, $request->getCount());
200  $this->assertSame('PAY123', $request->getStartId());
201  $this->assertSame(1, $request->getStartIndex());
202  $this->assertSame('2015-09-07T00:00:00Z', $request->getStartTime());
203  $this->assertSame('2015-09-08T00:00:00Z', $request->getEndTime());
204  $endPoint = $request->getEndpoint();
205  $this->assertSame('https://api.paypal.com/v1/payments/payment', $endPoint);
206  $data = $request->getData();
207  $this->assertNotEmpty($data);
208  }
209 
210  public function testCreateCard()
211  {
212  $this->setMockHttpResponse('RestCreateCardSuccess.txt');
213 
214  $response = $this->gateway->createCard($this->options)->send();
215 
216  $this->assertTrue($response->isSuccessful());
217  $this->assertEquals('CARD-70E78145XN686604FKO3L6OQ', $response->getCardReference());
218  $this->assertNull($response->getMessage());
219  }
220 
221  public function testPayWithSavedCard()
222  {
223  $this->setMockHttpResponse('RestCreateCardSuccess.txt');
224  $response = $this->gateway->createCard($this->options)->send();
225  $cardRef = $response->getCardReference();
226 
227  $this->setMockHttpResponse('RestPurchaseSuccess.txt');
228  $response = $this->gateway->purchase(array('amount'=>'10.00', 'cardReference'=>$cardRef))->send();
229  $this->assertTrue($response->isSuccessful());
230  $this->assertEquals('44E89981F8714392Y', $response->getTransactionReference());
231  $this->assertNull($response->getMessage());
232  }
233 
234  // Incomplete generic tests for subscription payments
235 
236  public function testCompleteSubscription()
237  {
238  $this->setMockHttpResponse('RestExecuteSubscriptionSuccess.txt');
239  $response = $this->gateway->completeSubscription($this->subscription_options)->send();
240  $this->assertTrue($response->isSuccessful());
241  $this->assertNull($response->getMessage());
242 
243  $this->assertEquals('I-0LN988D3JACS', $response->getTransactionReference());
244  }
245 
246  public function testCancelSubscription()
247  {
248  $this->setMockHttpResponse('RestGenericSubscriptionSuccess.txt');
249  $response = $this->gateway->cancelSubscription($this->subscription_options)->send();
250  $this->assertTrue($response->isSuccessful());
251  $this->assertNull($response->getMessage());
252  }
253 
254  public function testSuspendSubscription()
255  {
256  $this->setMockHttpResponse('RestGenericSubscriptionSuccess.txt');
257  $response = $this->gateway->suspendSubscription($this->subscription_options)->send();
258  $this->assertTrue($response->isSuccessful());
259  $this->assertNull($response->getMessage());
260  }
261 
262  public function testReactivateSubscription()
263  {
264  $this->setMockHttpResponse('RestGenericSubscriptionSuccess.txt');
265  $response = $this->gateway->reactivateSubscription($this->subscription_options)->send();
266  $this->assertTrue($response->isSuccessful());
267  $this->assertNull($response->getMessage());
268  }
269 
270  public function testRefundCapture()
271  {
272  $request = $this->gateway->refundCapture(array(
273  'transactionReference' => 'abc123'
274  ));
275 
276  $this->assertInstanceOf('\Omnipay\PayPal\Message\RestRefundCaptureRequest', $request);
277  $this->assertSame('abc123', $request->getTransactionReference());
278  $endPoint = $request->getEndpoint();
279  $this->assertSame('https://api.paypal.com/v1/payments/capture/abc123/refund', $endPoint);
280 
281  $request->setAmount('15.99');
282  $request->setCurrency('BRL');
283  $request->setDescription('Test Description');
284  $data = $request->getData();
285  // we're expecting an empty object here
286  $json = json_encode($data);
287  $this->assertEquals('{"amount":{"currency":"BRL","total":"15.99"},"description":"Test Description"}', $json);
288  }
289 
290  public function testVoid()
291  {
292  $request = $this->gateway->void(array(
293  'transactionReference' => 'abc123'
294  ));
295 
296  $this->assertInstanceOf('\Omnipay\PayPal\Message\RestVoidRequest', $request);
297  $this->assertSame('abc123', $request->getTransactionReference());
298  $endPoint = $request->getEndpoint();
299  $this->assertSame('https://api.paypal.com/v1/payments/authorization/abc123/void', $endPoint);
300  $data = $request->getData();
301  $this->assertEmpty($data);
302  }
303 }
Omnipay\PayPal\RestGatewayTest\$options
$options
Definition: lib/vendor/omnipay/paypal/tests/RestGatewayTest.php:20
Omnipay\PayPal\RestGatewayTest\testBearerTokenReused
testBearerTokenReused()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:65
Omnipay\PayPal\RestGatewayTest\testFetchPurchase
testFetchPurchase()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:187
Omnipay\PayPal\RestGatewayTest\testPurchase
testPurchase()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:96
Omnipay\PayPal\RestGatewayTest\testReactivateSubscription
testReactivateSubscription()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:271
Omnipay\PayPal\RestGatewayTest\testAuthorize
testAuthorize()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:85
Omnipay\PayPal
Definition: lib/vendor/omnipay/paypal/src/ExpressGateway.php:3
Omnipay\PayPal\RestGatewayTest\testCapture
testCapture()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:107
Omnipay\PayPal\RestGatewayTest\testPayWithSavedCard
testPayWithSavedCard()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:230
Omnipay\PayPal\RestGatewayTest\testBearerToken
testBearerToken()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:54
Omnipay\PayPal\RestGatewayTest\testCompleteSubscription
testCompleteSubscription()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:245
Omnipay\PayPal\RestGatewayTest\testFullRefund
testFullRefund()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:139
Omnipay\PayPal\RestGatewayTest\testVoid
testVoid()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:299
Omnipay\PayPal\RestGateway
Definition: lib/vendor/omnipay/paypal/src/RestGateway.php:152
Omnipay\PayPal\RestGatewayTest\testCancelSubscription
testCancelSubscription()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:255
Omnipay\PayPal\RestGatewayTest\$gateway
$gateway
Definition: lib/vendor/omnipay/paypal/tests/RestGatewayTest.php:14
Omnipay\PayPal\RestGatewayTest\testListPlan
testListPlan()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:166
Omnipay\PayPal\RestGatewayTest\setUp
setUp()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:28
Omnipay\PayPal\RestGatewayTest\testRefundCapture
testRefundCapture()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:279
Omnipay\PayPal\RestGatewayTest\testSuspendSubscription
testSuspendSubscription()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:263
Omnipay\PayPal\RestGatewayTest\testFetchTransaction
testFetchTransaction()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:156
Omnipay\PayPal\RestGatewayTest\testCreateCard
testCreateCard()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:219
Omnipay\PayPal\RestGatewayTest\$subscription_options
$subscription_options
Definition: lib/vendor/omnipay/paypal/tests/RestGatewayTest.php:26
Omnipay\Common\CreditCard
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/CreditCard.php:94
Omnipay\PayPal\RestGatewayTest\testListPurchase
testListPurchase()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:197
Omnipay\PayPal\RestGatewayTest\testBearerTokenExpires
testBearerTokenExpires()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:75
Omnipay\PayPal\RestGatewayTest\testRefund
testRefund()
Definition: vendor/omnipay/paypal/tests/RestGatewayTest.php:123