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