Skip to content

Commit

Permalink
Merge pull request #158 from davidkoberan/master
Browse files Browse the repository at this point in the history
 added support for reference transaction in express checkout
  • Loading branch information
delatbabel authored Feb 22, 2017
2 parents 37e742f + a68f8d4 commit aa38095
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/Message/ExpressAuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\PayPal\Support\InstantUpdateApi\ShippingOption;
use Omnipay\PayPal\Support\InstantUpdateApi\BillingAgreement;

/**
* PayPal Express Authorize Request
Expand Down Expand Up @@ -49,6 +50,22 @@ public function getShippingOptions()
return $this->getParameter('shippingOptions');
}

/**
* @param BillingAgreement $data
*/
public function setBillingAgreement($data)
{
$this->setParameter('billingAgreement', $data);
}

/**
* @return BillingAgreement
*/
public function getBillingAgreement()
{
return $this->getParameter('billingAgreement');
}

protected function validateCallback()
{
$callback = $this->getCallback();
Expand Down Expand Up @@ -152,6 +169,20 @@ public function getData()
$data['EMAIL'] = $card->getEmail();
}

$billingAgreement = $this->getBillingAgreement();
if ($billingAgreement) {
$data['L_BILLINGTYPE0'] = $billingAgreement->getType();
$data['L_BILLINGAGREEMENTDESCRIPTION0'] = $billingAgreement->getDescription();

if ($billingAgreement->hasPaymentType()) {
$data['L_PAYMENTTYPE0'] = $billingAgreement->getPaymentType();
}

if ($billingAgreement->hasCustomAnnotation()) {
$data['L_BILLINGAGREEMENTCUSTOM0'] = $billingAgreement->getCustomAnnotation();
}
}

$data = array_merge($data, $this->getItemData());

return $data;
Expand Down
97 changes: 97 additions & 0 deletions src/Support/InstantUpdateApi/BillingAgreement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Omnipay\PayPal\Support\InstantUpdateApi;

use Omnipay\Common\Exception\InvalidRequestException;

class BillingAgreement
{
/**
* Billing agreement types for single or recurring payment
*
* @var array
*/
protected $types = array(
'single' => 'MerchantInitiatedBillingSingleAgreement',
'recurring' => 'MerchantInitiatedBilling',
);

/** @var string */
private $type;

/** @var string */
private $description;

/** @var string */
private $paymentType;

/** @var string */
private $customAnnotation;

/**
* @param bool $recurring L_BILLINGTYPE0
* @param string $description L_BILLINGAGREEMENTDESCRIPTION0
* @param null|string $paymentType L_PAYMENTTYPE0
* @param null|string $customAnnotation L_BILLINGAGREEMENTCUSTOM0
* @throws \Exception
*/
public function __construct($recurring, $description, $paymentType = null, $customAnnotation = null)
{
if (!$recurring && !is_null($paymentType) && !in_array($paymentType, array('Any', 'InstantOnly'))) {
throw new InvalidRequestException("The 'paymentType' parameter can be only 'Any' or 'InstantOnly'");
}

$this->type = $recurring ? $this->types['recurring'] : $this->types['single'];
$this->description = $description;
$this->customAnnotation = $customAnnotation;
$this->paymentType = $paymentType;
}

/**
* @return string
*/
public function getType()
{
return $this->type;
}

/**
* @return string
*/
public function getDescription()
{
return $this->description;
}

/**
* @return bool
*/
public function hasPaymentType()
{
return !is_null($this->paymentType);
}

/**
* @return string
*/
public function getPaymentType()
{
return $this->paymentType;
}

/**
* @return bool
*/
public function hasCustomAnnotation()
{
return !is_null($this->customAnnotation);
}

/**
* @return string
*/
public function getCustomAnnotation()
{
return $this->customAnnotation;
}
}
48 changes: 48 additions & 0 deletions tests/Message/ExpressAuthorizeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Omnipay\Common\CreditCard;
use Omnipay\PayPal\Message\ExpressAuthorizeRequest;
use Omnipay\PayPal\Support\InstantUpdateApi\BillingAgreement;
use Omnipay\PayPal\Support\InstantUpdateApi\ShippingOption;
use Omnipay\Tests\TestCase;

Expand Down Expand Up @@ -372,4 +373,51 @@ public function testBadCallbackConfiguration()
$this->request->getData();
}

public function testGetDataWithSingleBillingAgreement()
{
$billingAgreement = new BillingAgreement(false, 'Some Stuff');
$this->request->setBillingAgreement($billingAgreement);

$data = $this->request->getData();

$this->assertSame('MerchantInitiatedBillingSingleAgreement', $data['L_BILLINGTYPE0']);
$this->assertSame('Some Stuff', $data['L_BILLINGAGREEMENTDESCRIPTION0']);
}

public function testGetDataWithRecurringBillingAgreement()
{
$billingAgreement = new BillingAgreement(true, 'Some Stuff');
$this->request->setBillingAgreement($billingAgreement);

$data = $this->request->getData();

$this->assertSame('MerchantInitiatedBilling', $data['L_BILLINGTYPE0']);
$this->assertSame('Some Stuff', $data['L_BILLINGAGREEMENTDESCRIPTION0']);
}

public function testGetDataWithBillingAgreementOptionalParameters()
{
$billingAgreement = new BillingAgreement(true, 'Some Stuff', 'InstantOnly', 'Some custom annotation');
$this->request->setBillingAgreement($billingAgreement);

$data = $this->request->getData();

$this->assertSame('MerchantInitiatedBilling', $data['L_BILLINGTYPE0']);
$this->assertSame('Some Stuff', $data['L_BILLINGAGREEMENTDESCRIPTION0']);
$this->assertSame('InstantOnly', $data['L_PAYMENTTYPE0']);
$this->assertSame('Some custom annotation', $data['L_BILLINGAGREEMENTCUSTOM0']);
}

/**
*
*/
public function testGetDataWithBillingAgreementWrongPaymentType()
{
$this->setExpectedException(
'\Omnipay\Common\Exception\InvalidRequestException',
"The 'paymentType' parameter can be only 'Any' or 'InstantOnly'"
);

$billingAgreement = new BillingAgreement(false, 'Some Stuff', 'BadType', 'Some custom annotation');
}
}
2 changes: 1 addition & 1 deletion tests/ProGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setUp()
'lastName' => 'User',
'number' => '4111111111111111',
'expiryMonth' => '12',
'expiryYear' => '2016',
'expiryYear' => '2017',
'cvv' => '123',
)),
);
Expand Down
2 changes: 1 addition & 1 deletion tests/RestGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function setUp()
'lastName' => 'User',
'number' => '4111111111111111',
'expiryMonth' => '12',
'expiryYear' => '2016',
'expiryYear' => '2017',
'cvv' => '123',
)),
);
Expand Down

0 comments on commit aa38095

Please sign in to comment.