Open Journal Systems  3.3.0
vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php
1 <?php
2 
3 namespace Omnipay\PayPal\Message;
4 
10 use Omnipay\Tests\TestCase;
11 
12 class ExpressAuthorizeRequestTest extends TestCase
13 {
17  private $request;
18 
19  public function setUp()
20  {
21  parent::setUp();
22 
23  $this->request = new ExpressAuthorizeRequest($this->getHttpClient(), $this->getHttpRequest());
24  $this->request->initialize(
25  array(
26  'amount' => '10.00',
27  'returnUrl' => 'https://www.example.com/return',
28  'cancelUrl' => 'https://www.example.com/cancel',
29  )
30  );
31  }
32 
33  public function testGetDataWithoutCard()
34  {
35  $this->request->initialize(array(
36  'amount' => '10.00',
37  'currency' => 'AUD',
38  'transactionId' => '111',
39  'description' => 'Order Description',
40  'returnUrl' => 'https://www.example.com/return',
41  'cancelUrl' => 'https://www.example.com/cancel',
42  'subject' => 'demo@example.com',
43  'headerImageUrl' => 'https://www.example.com/header.jpg',
44  'noShipping' => 0,
45  'localeCode' => 'EN',
46  'allowNote' => 0,
47  'addressOverride' => 0,
48  'brandName' => 'Dunder Mifflin Paper Company, Inc.',
49  'customerServiceNumber' => '1-801-FLOWERS',
50  ));
51 
52  $data = $this->request->getData();
53 
54  $this->assertSame('10.00', $data['PAYMENTREQUEST_0_AMT']);
55  $this->assertSame('AUD', $data['PAYMENTREQUEST_0_CURRENCYCODE']);
56  $this->assertSame('111', $data['PAYMENTREQUEST_0_INVNUM']);
57  $this->assertSame('Order Description', $data['PAYMENTREQUEST_0_DESC']);
58  $this->assertSame('https://www.example.com/return', $data['RETURNURL']);
59  $this->assertSame('https://www.example.com/cancel', $data['CANCELURL']);
60  $this->assertSame('demo@example.com', $data['SUBJECT']);
61  $this->assertSame('https://www.example.com/header.jpg', $data['HDRIMG']);
62  $this->assertSame(0, $data['NOSHIPPING']);
63  $this->assertSame(0, $data['ALLOWNOTE']);
64  $this->assertSame('EN', $data['LOCALECODE']);
65  $this->assertSame(0, $data['ADDROVERRIDE']);
66  $this->assertSame('Dunder Mifflin Paper Company, Inc.', $data['BRANDNAME']);
67  $this->assertSame('1-801-FLOWERS', $data['CUSTOMERSERVICENUMBER']);
68  }
69 
70  public function testGetDataWithCard()
71  {
72  $this->request->initialize(array(
73  'amount' => '10.00',
74  'currency' => 'AUD',
75  'transactionId' => '111',
76  'description' => 'Order Description',
77  'returnUrl' => 'https://www.example.com/return',
78  'cancelUrl' => 'https://www.example.com/cancel',
79  'subject' => 'demo@example.com',
80  'headerImageUrl' => 'https://www.example.com/header.jpg',
81  'noShipping' => 2,
82  'allowNote' => 1,
83  'addressOverride' => 1,
84  'brandName' => 'Dunder Mifflin Paper Company, Inc.',
85  'maxAmount' => 123.45,
86  'logoImageUrl' => 'https://www.example.com/logo.jpg',
87  'borderColor' => 'CCCCCC',
88  'localeCode' => 'EN',
89  'customerServiceNumber' => '1-801-FLOWERS',
90  'sellerPaypalAccountId' => 'billing@example.com',
91  ));
92 
93  $card = new CreditCard(array(
94  'name' => 'John Doe',
95  'address1' => '123 NW Blvd',
96  'address2' => 'Lynx Lane',
97  'city' => 'Topeka',
98  'state' => 'KS',
99  'country' => 'USA',
100  'postcode' => '66605',
101  'phone' => '555-555-5555',
102  'email' => 'test@email.com',
103  ));
104  $this->request->setCard($card);
105 
106  $expected = array(
107  'METHOD' => 'SetExpressCheckout',
109  'USER' => null,
110  'PWD' => null,
111  'SIGNATURE' => null,
112  'PAYMENTREQUEST_0_PAYMENTACTION' => 'Authorization',
113  'SOLUTIONTYPE' => null,
114  'LANDINGPAGE' => null,
115  'NOSHIPPING' => 2,
116  'ALLOWNOTE' => 1,
117  'ADDROVERRIDE' => 1,
118  'PAYMENTREQUEST_0_AMT' => '10.00',
119  'PAYMENTREQUEST_0_CURRENCYCODE' => 'AUD',
120  'PAYMENTREQUEST_0_INVNUM' => '111',
121  'PAYMENTREQUEST_0_DESC' => 'Order Description',
122  'RETURNURL' => 'https://www.example.com/return',
123  'CANCELURL' => 'https://www.example.com/cancel',
124  'SUBJECT' => 'demo@example.com',
125  'HDRIMG' => 'https://www.example.com/header.jpg',
126  'PAYMENTREQUEST_0_SHIPTONAME' => 'John Doe',
127  'PAYMENTREQUEST_0_SHIPTOSTREET' => '123 NW Blvd',
128  'PAYMENTREQUEST_0_SHIPTOSTREET2' => 'Lynx Lane',
129  'PAYMENTREQUEST_0_SHIPTOCITY' => 'Topeka',
130  'PAYMENTREQUEST_0_SHIPTOSTATE' => 'KS',
131  'PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE' => 'USA',
132  'PAYMENTREQUEST_0_SHIPTOZIP' => '66605',
133  'PAYMENTREQUEST_0_SHIPTOPHONENUM' => '555-555-5555',
134  'EMAIL' => 'test@email.com',
135  'BRANDNAME' => 'Dunder Mifflin Paper Company, Inc.',
136  'MAXAMT' => 123.45,
137  'PAYMENTREQUEST_0_TAXAMT' => null,
138  'PAYMENTREQUEST_0_SHIPPINGAMT' => null,
139  'PAYMENTREQUEST_0_HANDLINGAMT' => null,
140  'PAYMENTREQUEST_0_SHIPDISCAMT' => null,
141  'PAYMENTREQUEST_0_INSURANCEAMT' => null,
142  'LOGOIMG' => 'https://www.example.com/logo.jpg',
143  'CARTBORDERCOLOR' => 'CCCCCC',
144  'LOCALECODE' => 'EN',
145  'CUSTOMERSERVICENUMBER' => '1-801-FLOWERS',
146  'PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID' => 'billing@example.com',
147  );
148 
149  $this->assertEquals($expected, $this->request->getData());
150  }
151 
152  public function testGetDataWithItems()
153  {
154  $this->request->setItems(array(
155  array('name' => 'Floppy Disk', 'description' => 'MS-DOS', 'quantity' => 2, 'price' => 10, 'code' => '123456'),
156  array('name' => 'CD-ROM', 'description' => 'Windows 95', 'quantity' => 1, 'price' => 40),
157  ));
158 
159  $data = $this->request->getData();
160  $this->assertSame('Floppy Disk', $data['L_PAYMENTREQUEST_0_NAME0']);
161  $this->assertSame('MS-DOS', $data['L_PAYMENTREQUEST_0_DESC0']);
162  $this->assertSame(2, $data['L_PAYMENTREQUEST_0_QTY0']);
163  $this->assertSame('10.00', $data['L_PAYMENTREQUEST_0_AMT0']);
164  $this->assertSame('123456', $data['L_PAYMENTREQUEST_0_NUMBER0']);
165 
166  $this->assertSame('CD-ROM', $data['L_PAYMENTREQUEST_0_NAME1']);
167  $this->assertSame('Windows 95', $data['L_PAYMENTREQUEST_0_DESC1']);
168  $this->assertSame(1, $data['L_PAYMENTREQUEST_0_QTY1']);
169  $this->assertSame('40.00', $data['L_PAYMENTREQUEST_0_AMT1']);
170 
171  $this->assertSame('60.00', $data['PAYMENTREQUEST_0_ITEMAMT']);
172  }
173 
174  public function testGetDataWithExtraOrderDetails()
175  {
176  $this->request->initialize(array(
177  'amount' => '10.00',
178  'currency' => 'AUD',
179  'transactionId' => '111',
180  'description' => 'Order Description',
181  'returnUrl' => 'https://www.example.com/return',
182  'cancelUrl' => 'https://www.example.com/cancel',
183  'subject' => 'demo@example.com',
184  'headerImageUrl' => 'https://www.example.com/header.jpg',
185  'noShipping' => 0,
186  'allowNote' => 0,
187  'addressOverride' => 0,
188  'brandName' => 'Dunder Mifflin Paper Company, Inc.',
189  'taxAmount' => '2.00',
190  'shippingAmount' => '5.00',
191  'handlingAmount' => '1.00',
192  'shippingDiscount' => '-1.00',
193  'insuranceAmount' => '3.00',
194  ));
195 
196  $data = $this->request->getData();
197  $this->assertSame('2.00', $data['PAYMENTREQUEST_0_TAXAMT']);
198  $this->assertSame('5.00', $data['PAYMENTREQUEST_0_SHIPPINGAMT']);
199  $this->assertSame('1.00', $data['PAYMENTREQUEST_0_HANDLINGAMT']);
200  $this->assertSame('-1.00', $data['PAYMENTREQUEST_0_SHIPDISCAMT']);
201  $this->assertSame('3.00', $data['PAYMENTREQUEST_0_INSURANCEAMT']);
202  }
203 
204  public function testHeaderImageUrl()
205  {
206  $this->assertSame($this->request, $this->request->setHeaderImageUrl('https://www.example.com/header.jpg'));
207  $this->assertSame('https://www.example.com/header.jpg', $this->request->getHeaderImageUrl());
208 
209  $data = $this->request->getData();
210  $this->assertEquals('https://www.example.com/header.jpg', $data['HDRIMG']);
211  }
212 
213  public function testMaxAmount()
214  {
215  $this->request->setMaxAmount(321.54);
216 
217  $this->assertSame(321.54, $this->request->getMaxAmount());
218 
219  $data = $this->request->getData();
220 
221  $this->assertSame(321.54, $data['MAXAMT']);
222  }
223 
224  public function testDataWithCallback()
225  {
226  $baseData = array(
227  'amount' => '10.00',
228  'currency' => 'AUD',
229  'transactionId' => '111',
230  'description' => 'Order Description',
231  'returnUrl' => 'https://www.example.com/return',
232  'cancelUrl' => 'https://www.example.com/cancel',
233  'subject' => 'demo@example.com',
234  'headerImageUrl' => 'https://www.example.com/header.jpg',
235  'allowNote' => 0,
236  'addressOverride' => 0,
237  'brandName' => 'Dunder Mifflin Paper Company, Incy.',
238  );
239 
240  $shippingOptions = array(
241  new ShippingOption('First Class', 1.20, true, '1-2 days'),
242  new ShippingOption('Second Class', 0.70, false, '3-5 days'),
243  new ShippingOption('International', 3.50),
244  );
245 
246  // with a default callback timeout
247  $this->request->initialize(array_merge($baseData, array(
248  'callback' => 'https://www.example.com/calculate-shipping',
249  'shippingOptions' => $shippingOptions,
250  )));
251 
252  $data = $this->request->getData();
253  $this->assertSame('https://www.example.com/calculate-shipping', $data['CALLBACK']);
254  $this->assertSame(ExpressAuthorizeRequest::DEFAULT_CALLBACK_TIMEOUT, $data['CALLBACKTIMEOUT']);
255 
256  $this->assertSame('First Class', $data['L_SHIPPINGOPTIONNAME0']);
257  $this->assertSame('1.20', $data['L_SHIPPINGOPTIONAMOUNT0']);
258  $this->assertSame('1', $data['L_SHIPPINGOPTIONISDEFAULT0']);
259  $this->assertSame('1-2 days', $data['L_SHIPPINGOPTIONLABEL0']);
260 
261  $this->assertSame('Second Class', $data['L_SHIPPINGOPTIONNAME1']);
262  $this->assertSame('0.70', $data['L_SHIPPINGOPTIONAMOUNT1']);
263  $this->assertSame('0', $data['L_SHIPPINGOPTIONISDEFAULT1']);
264  $this->assertSame('3-5 days', $data['L_SHIPPINGOPTIONLABEL1']);
265 
266  $this->assertSame('International', $data['L_SHIPPINGOPTIONNAME2']);
267  $this->assertSame('3.50', $data['L_SHIPPINGOPTIONAMOUNT2']);
268  $this->assertSame('0', $data['L_SHIPPINGOPTIONISDEFAULT2']);
269 
270  // with a defined callback timeout
271  $this->request->initialize(array_merge($baseData, array(
272  'callback' => 'https://www.example.com/calculate-shipping',
273  'callbackTimeout' => 10,
274  'shippingOptions' => $shippingOptions,
275  )));
276 
277  $data = $this->request->getData();
278  $this->assertSame('https://www.example.com/calculate-shipping', $data['CALLBACK']);
279  $this->assertSame(10, $data['CALLBACKTIMEOUT']);
280  }
281 
283  {
284  $baseData = array(
285  'amount' => '10.00',
286  'currency' => 'AUD',
287  'transactionId' => '111',
288  'description' => 'Order Description',
289  'returnUrl' => 'https://www.example.com/return',
290  'cancelUrl' => 'https://www.example.com/cancel',
291  'subject' => 'demo@example.com',
292  'headerImageUrl' => 'https://www.example.com/header.jpg',
293  'allowNote' => 0,
294  'addressOverride' => 0,
295  'brandName' => 'Dunder Mifflin Paper Company, Incy.',
296  );
297 
298  $shippingOptions = array(
299  new ShippingOption('First Class', 1.20, false, '1-2 days'),
300  new ShippingOption('Second Class', 0.70, false, '3-5 days'),
301  new ShippingOption('International', 3.50),
302  );
303 
304  // with a default callback timeout
305  $this->request->initialize(array_merge($baseData, array(
306  'callback' => 'https://www.example.com/calculate-shipping',
307  'shippingOptions' => $shippingOptions,
308  )));
309 
310  $this->expectException(InvalidRequestException::class);
311  $this->expectExceptionMessage('One of the supplied shipping options must be set as default');
312 
313  $this->request->getData();
314  }
315 
316  public function testNoAmount()
317  {
318  $baseData = array(// nothing here - should cause a certain exception
319  );
320 
321  $this->request->initialize($baseData);
322 
323  $this->expectException(InvalidRequestException::class);
324  $this->expectExceptionMessage('The amount parameter is required');
325 
326  $this->request->getData();
327  }
328 
329  public function testAmountButNoReturnUrl()
330  {
331  $baseData = array(
332  'amount' => 10.00,
333  );
334 
335  $this->request->initialize($baseData);
336 
337  $this->expectException(InvalidRequestException::class);
338  $this->expectExceptionMessage('The returnUrl parameter is required');
339 
340  $this->request->getData();
341  }
342 
343  public function testBadCallbackConfiguration()
344  {
345  $baseData = array(
346  'amount' => '10.00',
347  'currency' => 'AUD',
348  'transactionId' => '111',
349  'description' => 'Order Description',
350  'returnUrl' => 'https://www.example.com/return',
351  'cancelUrl' => 'https://www.example.com/cancel',
352  'subject' => 'demo@example.com',
353  'headerImageUrl' => 'https://www.example.com/header.jpg',
354  'allowNote' => 0,
355  'addressOverride' => 0,
356  'brandName' => 'Dunder Mifflin Paper Company, Incy.',
357  );
358 
359  $this->request->initialize(array_merge($baseData, array(
360  'callback' => 'https://www.example.com/calculate-shipping',
361  )));
362 
363  // from the docblock on this exception -
364  // Thrown when a request is invalid or missing required fields.
365  // callback has been set but no shipping options so expect one of these:
366  $this->expectException(InvalidRequestException::class);
367 
368  $this->request->getData();
369  }
370 
371  public function testGetDataWithSingleBillingAgreement()
372  {
373  $billingAgreement = new BillingAgreement(false, 'Some Stuff');
374  $this->request->setBillingAgreement($billingAgreement);
375 
376  $data = $this->request->getData();
377 
378  $this->assertSame('MerchantInitiatedBillingSingleAgreement', $data['L_BILLINGTYPE0']);
379  $this->assertSame('Some Stuff', $data['L_BILLINGAGREEMENTDESCRIPTION0']);
380  }
381 
383  {
384  $billingAgreement = new BillingAgreement(true, 'Some Stuff');
385  $this->request->setBillingAgreement($billingAgreement);
386 
387  $data = $this->request->getData();
388 
389  $this->assertSame('MerchantInitiatedBilling', $data['L_BILLINGTYPE0']);
390  $this->assertSame('Some Stuff', $data['L_BILLINGAGREEMENTDESCRIPTION0']);
391  }
392 
394  {
395  $billingAgreement = new BillingAgreement(true, 'Some Stuff', 'InstantOnly', 'Some custom annotation');
396  $this->request->setBillingAgreement($billingAgreement);
397 
398  $data = $this->request->getData();
399 
400  $this->assertSame('MerchantInitiatedBilling', $data['L_BILLINGTYPE0']);
401  $this->assertSame('Some Stuff', $data['L_BILLINGAGREEMENTDESCRIPTION0']);
402  $this->assertSame('InstantOnly', $data['L_PAYMENTTYPE0']);
403  $this->assertSame('Some custom annotation', $data['L_BILLINGAGREEMENTCUSTOM0']);
404  }
405 
410  {
411  $this->expectException(InvalidRequestException::class);
412  $this->expectExceptionMessage("The 'paymentType' parameter can be only 'Any' or 'InstantOnly'");
413 
414  $billingAgreement = new BillingAgreement(false, 'Some Stuff', 'BadType', 'Some custom annotation');
415  }
416 }
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testGetDataWithBillingAgreementOptionalParameters
testGetDataWithBillingAgreementOptionalParameters()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:396
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testNoAmount
testNoAmount()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:319
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testGetDataWithExtraOrderDetails
testGetDataWithExtraOrderDetails()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:177
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testHeaderImageUrl
testHeaderImageUrl()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:207
Omnipay\PayPal\Message\AbstractRequest\API_VERSION
const API_VERSION
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRequest.php:37
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testDataWithCallback
testDataWithCallback()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:227
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testBadCallbackConfiguration
testBadCallbackConfiguration()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:346
Omnipay\PayPal\Support\InstantUpdateApi\BillingAgreement
Definition: lib/vendor/omnipay/paypal/src/Support/InstantUpdateApi/BillingAgreement.php:7
Omnipay\Common\Exception\InvalidRequestException
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/Exception/InvalidRequestException.php:10
Omnipay\PayPal\Message\ExpressAuthorizeRequest\DEFAULT_CALLBACK_TIMEOUT
const DEFAULT_CALLBACK_TIMEOUT
Definition: lib/vendor/omnipay/paypal/src/Message/ExpressAuthorizeRequest.php:15
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testMaxAmount
testMaxAmount()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:216
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testGetDataWithBillingAgreementWrongPaymentType
testGetDataWithBillingAgreementWrongPaymentType()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:412
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testGetDataWithRecurringBillingAgreement
testGetDataWithRecurringBillingAgreement()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:385
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testGetDataWithSingleBillingAgreement
testGetDataWithSingleBillingAgreement()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:374
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\setUp
setUp()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:22
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testDataWithCallbackAndNoDefaultShippingOption
testDataWithCallbackAndNoDefaultShippingOption()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:285
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testGetDataWithCard
testGetDataWithCard()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:73
Omnipay\PayPal\Support\InstantUpdateApi\ShippingOption
Definition: lib/vendor/omnipay/paypal/src/Support/InstantUpdateApi/ShippingOption.php:5
Omnipay\Common\CreditCard
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/CreditCard.php:94
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testGetDataWithItems
testGetDataWithItems()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:155
Omnipay\PayPal\Message
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRequest.php:6
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testAmountButNoReturnUrl
testAmountButNoReturnUrl()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:332
Omnipay\PayPal\Message\ExpressAuthorizeRequestTest\testGetDataWithoutCard
testGetDataWithoutCard()
Definition: vendor/omnipay/paypal/tests/Message/ExpressAuthorizeRequestTest.php:36
Omnipay\PayPal\Message\ExpressAuthorizeRequest
Definition: lib/vendor/omnipay/paypal/src/Message/ExpressAuthorizeRequest.php:12