Skip to content

Commit aa38095

Browse files
authored
Merge pull request thephpleague#158 from davidkoberan/master
added support for reference transaction in express checkout
2 parents 37e742f + a68f8d4 commit aa38095

File tree

5 files changed

+178
-2
lines changed

5 files changed

+178
-2
lines changed

src/Message/ExpressAuthorizeRequest.php

+31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Omnipay\Common\Exception\InvalidRequestException;
66
use Omnipay\PayPal\Support\InstantUpdateApi\ShippingOption;
7+
use Omnipay\PayPal\Support\InstantUpdateApi\BillingAgreement;
78

89
/**
910
* PayPal Express Authorize Request
@@ -49,6 +50,22 @@ public function getShippingOptions()
4950
return $this->getParameter('shippingOptions');
5051
}
5152

53+
/**
54+
* @param BillingAgreement $data
55+
*/
56+
public function setBillingAgreement($data)
57+
{
58+
$this->setParameter('billingAgreement', $data);
59+
}
60+
61+
/**
62+
* @return BillingAgreement
63+
*/
64+
public function getBillingAgreement()
65+
{
66+
return $this->getParameter('billingAgreement');
67+
}
68+
5269
protected function validateCallback()
5370
{
5471
$callback = $this->getCallback();
@@ -152,6 +169,20 @@ public function getData()
152169
$data['EMAIL'] = $card->getEmail();
153170
}
154171

172+
$billingAgreement = $this->getBillingAgreement();
173+
if ($billingAgreement) {
174+
$data['L_BILLINGTYPE0'] = $billingAgreement->getType();
175+
$data['L_BILLINGAGREEMENTDESCRIPTION0'] = $billingAgreement->getDescription();
176+
177+
if ($billingAgreement->hasPaymentType()) {
178+
$data['L_PAYMENTTYPE0'] = $billingAgreement->getPaymentType();
179+
}
180+
181+
if ($billingAgreement->hasCustomAnnotation()) {
182+
$data['L_BILLINGAGREEMENTCUSTOM0'] = $billingAgreement->getCustomAnnotation();
183+
}
184+
}
185+
155186
$data = array_merge($data, $this->getItemData());
156187

157188
return $data;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Omnipay\PayPal\Support\InstantUpdateApi;
4+
5+
use Omnipay\Common\Exception\InvalidRequestException;
6+
7+
class BillingAgreement
8+
{
9+
/**
10+
* Billing agreement types for single or recurring payment
11+
*
12+
* @var array
13+
*/
14+
protected $types = array(
15+
'single' => 'MerchantInitiatedBillingSingleAgreement',
16+
'recurring' => 'MerchantInitiatedBilling',
17+
);
18+
19+
/** @var string */
20+
private $type;
21+
22+
/** @var string */
23+
private $description;
24+
25+
/** @var string */
26+
private $paymentType;
27+
28+
/** @var string */
29+
private $customAnnotation;
30+
31+
/**
32+
* @param bool $recurring L_BILLINGTYPE0
33+
* @param string $description L_BILLINGAGREEMENTDESCRIPTION0
34+
* @param null|string $paymentType L_PAYMENTTYPE0
35+
* @param null|string $customAnnotation L_BILLINGAGREEMENTCUSTOM0
36+
* @throws \Exception
37+
*/
38+
public function __construct($recurring, $description, $paymentType = null, $customAnnotation = null)
39+
{
40+
if (!$recurring && !is_null($paymentType) && !in_array($paymentType, array('Any', 'InstantOnly'))) {
41+
throw new InvalidRequestException("The 'paymentType' parameter can be only 'Any' or 'InstantOnly'");
42+
}
43+
44+
$this->type = $recurring ? $this->types['recurring'] : $this->types['single'];
45+
$this->description = $description;
46+
$this->customAnnotation = $customAnnotation;
47+
$this->paymentType = $paymentType;
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function getType()
54+
{
55+
return $this->type;
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
public function getDescription()
62+
{
63+
return $this->description;
64+
}
65+
66+
/**
67+
* @return bool
68+
*/
69+
public function hasPaymentType()
70+
{
71+
return !is_null($this->paymentType);
72+
}
73+
74+
/**
75+
* @return string
76+
*/
77+
public function getPaymentType()
78+
{
79+
return $this->paymentType;
80+
}
81+
82+
/**
83+
* @return bool
84+
*/
85+
public function hasCustomAnnotation()
86+
{
87+
return !is_null($this->customAnnotation);
88+
}
89+
90+
/**
91+
* @return string
92+
*/
93+
public function getCustomAnnotation()
94+
{
95+
return $this->customAnnotation;
96+
}
97+
}

tests/Message/ExpressAuthorizeRequestTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Omnipay\Common\CreditCard;
66
use Omnipay\PayPal\Message\ExpressAuthorizeRequest;
7+
use Omnipay\PayPal\Support\InstantUpdateApi\BillingAgreement;
78
use Omnipay\PayPal\Support\InstantUpdateApi\ShippingOption;
89
use Omnipay\Tests\TestCase;
910

@@ -372,4 +373,51 @@ public function testBadCallbackConfiguration()
372373
$this->request->getData();
373374
}
374375

376+
public function testGetDataWithSingleBillingAgreement()
377+
{
378+
$billingAgreement = new BillingAgreement(false, 'Some Stuff');
379+
$this->request->setBillingAgreement($billingAgreement);
380+
381+
$data = $this->request->getData();
382+
383+
$this->assertSame('MerchantInitiatedBillingSingleAgreement', $data['L_BILLINGTYPE0']);
384+
$this->assertSame('Some Stuff', $data['L_BILLINGAGREEMENTDESCRIPTION0']);
385+
}
386+
387+
public function testGetDataWithRecurringBillingAgreement()
388+
{
389+
$billingAgreement = new BillingAgreement(true, 'Some Stuff');
390+
$this->request->setBillingAgreement($billingAgreement);
391+
392+
$data = $this->request->getData();
393+
394+
$this->assertSame('MerchantInitiatedBilling', $data['L_BILLINGTYPE0']);
395+
$this->assertSame('Some Stuff', $data['L_BILLINGAGREEMENTDESCRIPTION0']);
396+
}
397+
398+
public function testGetDataWithBillingAgreementOptionalParameters()
399+
{
400+
$billingAgreement = new BillingAgreement(true, 'Some Stuff', 'InstantOnly', 'Some custom annotation');
401+
$this->request->setBillingAgreement($billingAgreement);
402+
403+
$data = $this->request->getData();
404+
405+
$this->assertSame('MerchantInitiatedBilling', $data['L_BILLINGTYPE0']);
406+
$this->assertSame('Some Stuff', $data['L_BILLINGAGREEMENTDESCRIPTION0']);
407+
$this->assertSame('InstantOnly', $data['L_PAYMENTTYPE0']);
408+
$this->assertSame('Some custom annotation', $data['L_BILLINGAGREEMENTCUSTOM0']);
409+
}
410+
411+
/**
412+
*
413+
*/
414+
public function testGetDataWithBillingAgreementWrongPaymentType()
415+
{
416+
$this->setExpectedException(
417+
'\Omnipay\Common\Exception\InvalidRequestException',
418+
"The 'paymentType' parameter can be only 'Any' or 'InstantOnly'"
419+
);
420+
421+
$billingAgreement = new BillingAgreement(false, 'Some Stuff', 'BadType', 'Some custom annotation');
422+
}
375423
}

tests/ProGatewayTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function setUp()
2020
'lastName' => 'User',
2121
'number' => '4111111111111111',
2222
'expiryMonth' => '12',
23-
'expiryYear' => '2016',
23+
'expiryYear' => '2017',
2424
'cvv' => '123',
2525
)),
2626
);

tests/RestGatewayTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function setUp()
3131
'lastName' => 'User',
3232
'number' => '4111111111111111',
3333
'expiryMonth' => '12',
34-
'expiryYear' => '2016',
34+
'expiryYear' => '2017',
3535
'cvv' => '123',
3636
)),
3737
);

0 commit comments

Comments
 (0)