Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept Customer Object #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Common/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@
*/
class CreditCard
{
use ParametersTrait;
use ParametersTrait {
validate as traitValidate;
}

const BRAND_VISA = 'visa';
const BRAND_MASTERCARD = 'mastercard';
Expand Down Expand Up @@ -230,11 +232,21 @@ protected function setYearParameter($key, $value)
* Generally if you want to validate the credit card yourself with custom error
* messages, you should use your framework's validation library, not this method.
*
* @param array $args
* @return void
* @throws Exception\InvalidRequestException
* @throws InvalidCreditCardException
*/
public function validate()
public function validate(...$args)
{
if (!empty($args)) {
$this->traitValidate(...$args);
} else {
$this->validateCreditcard();
}
}

private function validateCreditcard()
{
$requiredParameters = array(
'number' => 'credit card number',
Expand Down
25 changes: 25 additions & 0 deletions src/Common/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,31 @@ public function setCard($value)
return $this->setParameter('card', $value);
}

/**
* Get the customer object.
*
* @return CreditCard
*/
public function getCustomer()
{
return $this->getParameter('card');
}

/**
* Sets the customer object.
*
* @param CreditCard $value
* @return $this
*/
public function setCustomer($value)
{
if ($value && !$value instanceof CreditCard) {
$value = new CreditCard($value);
}

return $this->setParameter('card', $value);
}

/**
* Get the card token.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/Omnipay/Common/CreditCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ public function testCustomBrandAddedTwiceReturnsFalse()
$this->assertFalse($this->card->addSupportedBrand('omniexpress', '/^9\d{12}(\d{3})?$/'));
}

/**
* @expectedException \Omnipay\Common\Exception\InvalidRequestException
* @expectedExceptionMessage The title parameter is required
*/
public function testValidateTitle()
{
$this->card->validate('title');
}

/**
* @expectedException \Omnipay\Common\Exception\InvalidRequestException
*/
public function testValidateMultiple()
{
$this->card->validate('title', 'gender');
}

public function testTitle()
{
$this->card->setTitle('Mr.');
Expand Down
19 changes: 19 additions & 0 deletions tests/Omnipay/Common/Message/AbstractRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ public function testSetCardWithArray()
$this->assertSame('1234', $card->getNumber());
}

public function testCustomer()
{
// no type checking on card parameter
$card = new CreditCard;
$this->assertSame($this->request, $this->request->setCustomer($card));
$this->assertSame($card, $this->request->getCustomer());
}

public function testSetCustomerWithArray()
{
// passing array should create CreditCard object
$this->assertSame($this->request, $this->request->setCustomer(array('first_name' => 'Barry')));

$customer = $this->request->getCustomer();
$customer->validate('billingFirstName');
$this->assertInstanceOf('\Omnipay\Common\CreditCard', $customer);
$this->assertSame('Barry', $customer->getName());
}

public function testToken()
{
$this->assertSame($this->request, $this->request->setToken('12345'));
Expand Down