5 use Omnipay\Tests\GatewayTestCase;
19 public function setUp()
23 $this->gateway =
new RestGateway($this->getHttpClient(), $this->getHttpRequest());
24 $this->gateway->setToken(
'TEST-TOKEN-123');
25 $this->gateway->setTokenExpires(time() + 600);
27 $this->options = array(
30 'firstName' =>
'Example',
32 'number' =>
'4111111111111111',
33 'expiryMonth' =>
'12',
34 'expiryYear' =>
'2017',
39 $this->subscription_options = array(
40 'transactionReference' =>
'ABC-1234',
41 'description' =>
'Description goes here',
47 $this->gateway->setToken(
'');
48 $this->setMockHttpResponse(
'RestTokenSuccess.txt');
50 $this->assertFalse($this->gateway->hasToken());
51 $this->assertEquals(
'A015GQlKQ6uCRzLHSGRliANi59BHw6egNVKEWRnxvTwvLr0', $this->gateway->getToken());
52 $this->assertEquals(time() + 28800, $this->gateway->getTokenExpires());
53 $this->assertTrue($this->gateway->hasToken());
58 $this->setMockHttpResponse(
'RestTokenSuccess.txt');
59 $this->gateway->setToken(
'MYTOKEN');
60 $this->gateway->setTokenExpires(time() + 60);
62 $this->assertTrue($this->gateway->hasToken());
63 $this->assertEquals(
'MYTOKEN', $this->gateway->getToken());
68 $this->setMockHttpResponse(
'RestTokenSuccess.txt');
69 $this->gateway->setToken(
'MYTOKEN');
70 $this->gateway->setTokenExpires(time() - 60);
72 $this->assertFalse($this->gateway->hasToken());
73 $this->assertEquals(
'A015GQlKQ6uCRzLHSGRliANi59BHw6egNVKEWRnxvTwvLr0', $this->gateway->getToken());
78 $this->setMockHttpResponse(
'RestPurchaseSuccess.txt');
80 $response = $this->gateway->authorize($this->options)->send();
82 $this->assertTrue($response->isSuccessful());
83 $this->assertEquals(
'44E89981F8714392Y', $response->getTransactionReference());
84 $this->assertNull($response->getMessage());
89 $this->setMockHttpResponse(
'RestPurchaseSuccess.txt');
91 $response = $this->gateway->purchase($this->options)->send();
93 $this->assertTrue($response->isSuccessful());
94 $this->assertEquals(
'44E89981F8714392Y', $response->getTransactionReference());
95 $this->assertNull($response->getMessage());
100 $request = $this->gateway->capture(array(
101 'transactionReference' =>
'abc123',
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);
116 $request = $this->gateway->refund(array(
117 'transactionReference' =>
'abc123',
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);
132 $request = $this->gateway->refund(array(
133 'transactionReference' =>
'abc123',
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();
143 $json = json_encode($data);
144 $this->assertEquals(
'{}', $json);
149 $request = $this->gateway->fetchTransaction(array(
'transactionReference' =>
'abc123'));
151 $this->assertInstanceOf(
'\Omnipay\PayPal\Message\RestFetchTransactionRequest', $request);
152 $this->assertSame(
'abc123', $request->getTransactionReference());
153 $data = $request->getData();
154 $this->assertEmpty($data);
159 $request = $this->gateway->listPlan(array(
161 'status' =>
'ACTIVE',
163 'totalRequired' =>
'yes'
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());
172 $endPoint = $request->getEndpoint();
173 $this->assertSame(
'https://api.paypal.com/v1/payments/billing-plans', $endPoint);
174 $data = $request->getData();
175 $this->assertNotEmpty($data);
180 $request = $this->gateway->fetchPurchase(array(
'transactionReference' =>
'abc123'));
182 $this->assertInstanceOf(
'\Omnipay\PayPal\Message\RestFetchPurchaseRequest', $request);
183 $this->assertSame(
'abc123', $request->getTransactionReference());
184 $data = $request->getData();
185 $this->assertEmpty($data);
190 $request = $this->gateway->listPurchase(array(
192 'startId' =>
'PAY123',
194 'startTime' =>
'2015-09-07T00:00:00Z',
195 'endTime' =>
'2015-09-08T00:00:00Z',
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);
212 $this->setMockHttpResponse(
'RestCreateCardSuccess.txt');
214 $response = $this->gateway->createCard($this->options)->send();
216 $this->assertTrue($response->isSuccessful());
217 $this->assertEquals(
'CARD-70E78145XN686604FKO3L6OQ', $response->getCardReference());
218 $this->assertNull($response->getMessage());
223 $this->setMockHttpResponse(
'RestCreateCardSuccess.txt');
224 $response = $this->gateway->createCard($this->options)->send();
225 $cardRef = $response->getCardReference();
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());
238 $this->setMockHttpResponse(
'RestExecuteSubscriptionSuccess.txt');
239 $response = $this->gateway->completeSubscription($this->subscription_options)->send();
240 $this->assertTrue($response->isSuccessful());
241 $this->assertNull($response->getMessage());
243 $this->assertEquals(
'I-0LN988D3JACS', $response->getTransactionReference());
248 $this->setMockHttpResponse(
'RestGenericSubscriptionSuccess.txt');
249 $response = $this->gateway->cancelSubscription($this->subscription_options)->send();
250 $this->assertTrue($response->isSuccessful());
251 $this->assertNull($response->getMessage());
256 $this->setMockHttpResponse(
'RestGenericSubscriptionSuccess.txt');
257 $response = $this->gateway->suspendSubscription($this->subscription_options)->send();
258 $this->assertTrue($response->isSuccessful());
259 $this->assertNull($response->getMessage());
264 $this->setMockHttpResponse(
'RestGenericSubscriptionSuccess.txt');
265 $response = $this->gateway->reactivateSubscription($this->subscription_options)->send();
266 $this->assertTrue($response->isSuccessful());
267 $this->assertNull($response->getMessage());
272 $request = $this->gateway->refundCapture(array(
273 'transactionReference' =>
'abc123'
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);
281 $request->setAmount(
'15.99');
282 $request->setCurrency(
'BRL');
283 $request->setDescription(
'Test Description');
284 $data = $request->getData();
286 $json = json_encode($data);
287 $this->assertEquals(
'{"amount":{"currency":"BRL","total":"15.99"},"description":"Test Description"}', $json);
292 $request = $this->gateway->void(array(
293 'transactionReference' =>
'abc123'
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);