Skip to content

Commit

Permalink
Merge pull request #91 from vimeo/PYMT-1355
Browse files Browse the repository at this point in the history
Add support for ECP payment methods
  • Loading branch information
fredespo authored Aug 1, 2023
2 parents 71787b3 + 031f1a0 commit 47f7074
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 6 deletions.
99 changes: 99 additions & 0 deletions src/EcpAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

use Omnipay\Common\Helper;
use Symfony\Component\HttpFoundation\ParameterBag;

class EcpAccount
{
/**
* Internal storage of all of the account parameters.
*
* @var \Symfony\Component\HttpFoundation\ParameterBag
*/
protected $parameters;

public function __construct($parameters = null)
{
$this->initialize($parameters);
}

/**
* Initialize the object with parameters.
* If any unknown parameters passed, they will be ignored.
*/
public function initialize(array $parameters = null): self
{
$this->parameters = new ParameterBag;

Helper::initialize($this, $parameters);

return $this;
}

public function getParameters(): array
{
return $this->parameters->all();
}

protected function getParameter($key): mixed
{
return $this->parameters->get($key);
}

protected function setParameter($key, $value): self
{
$this->parameters->set($key, $value);

return $this;
}

public function getMaskedAccountNumber(): self
{
return $this->getParameter('maskedAccountNumber');
}

public function setMaskedAccountNumber(string $value): self
{
return $this->setParameter('maskedAccountNumber', $value);
}

public function getRoutingNumber(): self
{
return $this->getParameter('routingNumber');
}

public function setRoutingNumber(string $value): self
{
return $this->setParameter('routingNumber', $value);
}

public function getAccountType(): self
{
return $this->getParameter('accountType');
}

public function setAccountType(string $value): self
{
return $this->setParameter('accountType', $value);
}

public function getBillingPostcode(): self
{
return $this->getParameter('billingPostcode');
}

public function setBillingPostcode(string $value): self
{
return $this->setParameter('billingPostcode', $value);
}

public function getBillingCountry(): self
{
return $this->getParameter('billingCountry');
}

public function setBillingCountry(string $value): self
{
return $this->setParameter('billingCountry', $value);
}
}
20 changes: 18 additions & 2 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
const PAYMENT_METHOD_PAYPAL = 'PayPal';
const PAYMENT_METHOD_APPLE_PAY = 'ApplePay';
const PAYMENT_METHOD_CREDIT_CARD = 'CreditCard';
const PAYMENT_METHOD_ECP = 'ECP';

/**
* If chargeback probability from risk scoring is greater than this,
Expand Down Expand Up @@ -337,6 +338,21 @@ public function setPaymentMethodReference($value)
return $this->setParameter('paymentMethodReference', $value);
}

/**
* @return null|string
*/
public function getPaymentMethodType()
{
return $this->getParameter('paymentMethodType');
}

/**
* @return static
*/
public function setPaymentMethodType($value) {
return $this->setParameter('paymentMethodType', $value);
}

/**
* @return null|string
*/
Expand Down Expand Up @@ -1135,7 +1151,7 @@ protected function buildPaymentMethod($paymentMethodType, $addAttributes = false

$card = $this->getCard();

if ($paymentMethodType === self::PAYMENT_METHOD_CREDIT_CARD
if (in_array($paymentMethodType, [self::PAYMENT_METHOD_CREDIT_CARD, self::PAYMENT_METHOD_ECP])
|| ($paymentMethodType === null && $card)
) {
if ($card) {
Expand All @@ -1153,7 +1169,7 @@ protected function buildPaymentMethod($paymentMethodType, $addAttributes = false

// never change the type on an update
if (!$this->isUpdate()) {
$paymentMethod->type = self::PAYMENT_METHOD_CREDIT_CARD;
$paymentMethod->type = $paymentMethodType;
}
} elseif ($paymentMethodType === self::PAYMENT_METHOD_PAYPAL
|| ($paymentMethodType === null && $this->getReturnUrl())
Expand Down
25 changes: 25 additions & 0 deletions src/Message/CompleteHOAResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,37 @@ public function getPaymentMethod()
return null;
}

if ($vindiciaPaymentMethod->type === AbstractRequest::PAYMENT_METHOD_ECP) {
$vindiciaPaymentMethod->ecp->account = $this->getMaskedEcpAccountNumber();
}

$this->paymentMethod = $this->objectHelper->buildPaymentMethod($vindiciaPaymentMethod);
}

return isset($this->paymentMethod) ? $this->paymentMethod : null;
}

private function getMaskedEcpAccountNumber(): ?string
{
$account_length = null;
$last_digits = null;
foreach ($this->data->session->postValues as $post_value) {
switch($post_value->name) {
case 'vin_PaymentMethod_ecp_lastDigits':
$last_digits = $post_value->value;
break;
case 'vin_PaymentMethod_ecp_accountLength':
$account_length = $post_value->value;
break;
}
}

if ($account_length !== null && $last_digits !== null) {
return str_repeat('X', $account_length - strlen($last_digits)) . $last_digits;
}
return null;
}

/**
* @return null|\Omnipay\Vindicia\Subscription
*/
Expand Down
26 changes: 22 additions & 4 deletions src/ObjectHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,21 @@ public function buildPaymentMethod(stdClass $object, stdClass $salesTaxAddress =
}
}

return new PaymentMethod(array(
$payment_method_args = [
'paymentMethodId' => isset($object->merchantPaymentMethodId) ? $object->merchantPaymentMethodId : null,
'paymentMethodReference' => isset($object->VID) ? $object->VID : null,
'type' => isset($object->type) ? $object->type : null,
// NonStrippingCreditCard won't remove the X's that Vindicia masks with
'card' => $this->buildCreditCard($object, $salesTaxAddress, $cvv),
'attributes' => isset($nameValues) ? $this->buildAttributes($nameValues) : null,
'payPalEmail' => $payPalEmail
));
];
if ($object->type === AbstractRequest::PAYMENT_METHOD_ECP) {
$payment_method_args['ecpAccount'] = $this->buildEcpAccount($object);
} else {
// NonStrippingCreditCard won't remove the X's that Vindicia masks with
$payment_method_args['card'] = $this->buildCreditCard($object, $salesTaxAddress, $cvv);
}

return new PaymentMethod($payment_method_args);
}

/**
Expand Down Expand Up @@ -279,6 +285,18 @@ public function buildCreditCard(stdClass $object, stdClass $salesTaxAddress = nu
return new NonStrippingCreditCard($card_info);
}

public function buildEcpAccount(stdClass $object): EcpAccount
{
$ecp_info = [
'maskedAccountNumber' => $object->ecp->account,
'routingNumber' => $object->ecp->routingNumber,
'accountType' => $object->ecp->accountType,
'billingpostcode' => $object->billingAddress->postalCode,
'billingcountry' => $object->billingAddress->country,
];
return new EcpAccount($ecp_info);
}

/**
* @return \Omnipay\Vindicia\Plan
*/
Expand Down
11 changes: 11 additions & 0 deletions src/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Omnipay\Common\Helper;
use Symfony\Component\HttpFoundation\ParameterBag;
use Omnipay\Common\CreditCard;
use Omnipay\Vindicia\EcpAccount;

/**
* Generic representation of a payment method object returned by a gateway.
Expand Down Expand Up @@ -177,6 +178,16 @@ public function setCard($value)
return $this->setParameter('card', $value);
}

public function getEcpAccount(): ?EcpAccount
{
return $this->getParameter('ecpAccount');
}

public function setEcpAccount(EcpAccount $value): self
{
return $this->setParameter('ecpAccount', $value);
}

/**
* Get the payment method type, either PayPal or CreditCard
*
Expand Down
51 changes: 51 additions & 0 deletions tests/EcpAccountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Omnipay\Vindicia;

use EcpAccount;
use Omnipay\Tests\TestCase;
use Omnipay\Vindicia\TestFramework\DataFaker;

class EcpAccountTest extends TestCase
{
public function setUp(): void
{
$this->ecp = new EcpAccount();
$this->faker = new DataFaker();
}

public function testMaskedAccountNumber(): void
{
$value = 'XXXXX1234';
$this->assertSame($this->ecp, $this->ecp->setMaskedAccountNumber($value));
$this->assertSame($value, $this->ecp->getMaskedAccountNumber());
}

public function testRoutingNumber(): void
{
$value = '123456789';
$this->assertSame($this->ecp, $this->ecp->setRoutingNumber($value));
$this->assertSame($value, $this->ecp->getRoutingNumber());
}

public function testAccountType(): void
{
$value = 'ConsumerChecking';
$this->assertSame($this->ecp, $this->ecp->setAccountType($value));
$this->assertSame($value, $this->ecp->getAccountType());
}

public function testPostalCode(): void
{
$value = $this->faker->postcode();
$this->assertSame($this->ecp, $this->ecp->setBillingPostcode($value));
$this->assertSame($value, $this->ecp->getBillingPostcode());
}

public function testCountry(): void
{
$value = $this->faker->region();
$this->assertSame($this->ecp, $this->ecp->setBillingCountry($value));
$this->assertSame($value, $this->ecp->getBillingCountry());
}
}
7 changes: 7 additions & 0 deletions tests/Message/AbstractRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,4 +507,11 @@ public function testSendData()
TestableSoapClient::getLastArguments()
);
}

public function testPaymentMethodType(): void
{
$paymentMethodType = AbstractRequest::PAYMENT_METHOD_ECP;
$this->assertSame($this->request, $this->request->setPaymentMethodType($paymentMethodType));
$this->assertSame($paymentMethodType, $this->request->getPaymentMethodType());
}
}
8 changes: 8 additions & 0 deletions tests/PaymentMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Omnipay\Vindicia\TestFramework\DataFaker;
use Omnipay\Tests\TestCase;
use Omnipay\Common\CreditCard;
use Omnipay\Vindicia\EcpAccount;

class PaymentMethodTest extends TestCase
{
Expand Down Expand Up @@ -121,4 +122,11 @@ public function testAttributes()
$this->assertSame($this->paymentMethod, $this->paymentMethod->setAttributes($attributes));
$this->assertSame($attributes, $this->paymentMethod->getAttributes());
}

public function testEcpAccount(): void
{
$ecp = new EcpAccount();
$this->assertSame($this->paymentMethod, $this->paymentMethod->setEcpAccount($ecp));
$this->assertSame($ecp, $this->paymentMethod->getEcpAccount());
}
}

0 comments on commit 47f7074

Please sign in to comment.