-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Strikewood/feature/token-billing
Implement tokenized billing
- Loading branch information
Showing
9 changed files
with
463 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Omnipay\FirstAtlanticCommerce; | ||
|
||
use Omnipay\Common\CreditCard as BaseCreditCard; | ||
use Omnipay\Common\Exception\InvalidCreditCardException; | ||
|
||
class CreditCard extends BaseCreditCard | ||
{ | ||
/** | ||
* Validate this credit card. If the card is invalid, InvalidCreditCardException is thrown. | ||
* | ||
* This method is called internally by gateways to avoid wasting time with an API call | ||
* when the credit card is clearly invalid. | ||
* | ||
* Falls back to validating number, cvv, expiryMonth, expiryYear if no parameters are present. | ||
* | ||
* @param string ... Optional variable length list of required parameters | ||
* @throws InvalidCreditCardException | ||
*/ | ||
public function validate() | ||
{ | ||
$parameters = func_get_args(); | ||
|
||
if ( count($parameters) == 0 ) | ||
{ | ||
$parameters = ['number', 'cvv', 'expiryMonth', 'expiryYear']; | ||
} | ||
|
||
foreach ($parameters as $key) | ||
{ | ||
$value = $this->parameters->get($key); | ||
|
||
if ( empty($value) ) | ||
{ | ||
throw new InvalidCreditCardException("The $key parameter is required"); | ||
} | ||
} | ||
|
||
if ( isset($parameters['expiryMonth']) && isset($parameters['expiryYear']) ) | ||
{ | ||
if ( $this->getExpiryDate('Ym') < gmdate('Ym') ) | ||
{ | ||
throw new InvalidCreditCardException('Card has expired'); | ||
} | ||
} | ||
|
||
if ( isset($parameters['number']) ) | ||
{ | ||
if ( !Helper::validateLuhn( $this->getNumber() ) ) | ||
{ | ||
throw new InvalidCreditCardException('Card number is invalid'); | ||
} | ||
|
||
if ( !is_null( $this->getNumber() ) && !preg_match( '/^\d{12,19}$/i', $this->getNumber() ) ) | ||
{ | ||
throw new InvalidCreditCardException('Card number should have 12 to 19 digits'); | ||
} | ||
} | ||
|
||
if ( isset($parameters['cvv']) ) | ||
{ | ||
if ( !is_null( $this->getCvv() ) && !preg_match( '/^\d{3,4}$/i', $this->getCvv() ) ) | ||
{ | ||
throw new InvalidCreditCardException('Card CVV should have 3 to 4 digits'); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace Omnipay\FirstAtlanticCommerce\Message; | ||
|
||
use Omnipay\FirstAtlanticCommerce\Message\AbstractRequest; | ||
|
||
/** | ||
* FACPG2 Tokenize Request | ||
*/ | ||
class CreateCardRequest extends AbstractRequest | ||
{ | ||
/** | ||
* @var string; | ||
*/ | ||
protected $requestName = 'TokenizeRequest'; | ||
|
||
/** | ||
* Returns the signature for the request. | ||
* | ||
* @return string base64 encoded sha1 hash of the merchantPassword, merchantId, | ||
* and acquirerId. | ||
*/ | ||
protected function generateSignature() | ||
{ | ||
$signature = $this->getMerchantPassword(); | ||
$signature .= $this->getMerchantId(); | ||
$signature .= $this->getAcquirerId(); | ||
|
||
return base64_encode( sha1($signature, true) ); | ||
} | ||
|
||
/** | ||
* Validate and construct the data for the request | ||
* | ||
* @return array | ||
*/ | ||
public function getData() | ||
{ | ||
$this->validate('merchantId', 'merchantPassword', 'acquirerId', 'customerReference', 'card'); | ||
$this->getCard()->validate(); | ||
|
||
$data = [ | ||
'CardNumber' => $this->getCard()->getNumber(), | ||
'CustomerReference' => $this->getCustomerReference(), | ||
'ExpiryDate' => $this->getCard()->getExpiryDate('my'), | ||
'MerchantNumber' => $this->getMerchantId(), | ||
'Signature' => $this->generateSignature() | ||
]; | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Get the customer reference. | ||
* | ||
* @return string | ||
*/ | ||
public function getCustomerReference() | ||
{ | ||
return $this->getParameter('customerReference'); | ||
} | ||
|
||
/** | ||
* Set the customer reference. | ||
* | ||
* @param string $value | ||
*/ | ||
public function setCustomerReference($value) | ||
{ | ||
return $this->setParameter('customerReference', $value); | ||
} | ||
|
||
/** | ||
* Returns endpoint for tokenize requests | ||
* | ||
* @return string Endpoint URL | ||
*/ | ||
protected function getEndpoint() | ||
{ | ||
return parent::getEndpoint() . 'Tokenize'; | ||
} | ||
|
||
/** | ||
* Return the tokenize response object | ||
* | ||
* @param \SimpleXMLElement $xml Response xml object | ||
* | ||
* @return CreateCardResponse | ||
*/ | ||
protected function newResponse($xml) | ||
{ | ||
return new CreateCardResponse($this, $xml); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Omnipay\FirstAtlanticCommerce\Message; | ||
|
||
use Omnipay\Common\Exception\InvalidResponseException; | ||
use Omnipay\Common\Message\RequestInterface; | ||
use Omnipay\FirstAtlanticCommerce\Message\AbstractResponse; | ||
|
||
/** | ||
* FACPG2 XML Tokenize Response | ||
*/ | ||
class CreateCardResponse extends AbstractResponse | ||
{ | ||
/** | ||
* Constructor | ||
* | ||
* @param RequestInterface $request | ||
* @param string $data | ||
*/ | ||
public function __construct(RequestInterface $request, $data) | ||
{ | ||
if ( empty($data) ) | ||
{ | ||
throw new InvalidResponseException(); | ||
} | ||
|
||
$this->request = $request; | ||
$this->data = $this->xmlDeserialize($data); | ||
} | ||
|
||
/** | ||
* Return whether or not the response was successful | ||
* | ||
* @return boolean | ||
*/ | ||
public function isSuccessful() | ||
{ | ||
return isset($this->data['Success']) && 'true' === $this->data['Success']; | ||
} | ||
|
||
/** | ||
* Return the response's reason message | ||
* | ||
* @return string | ||
*/ | ||
public function getMessage() | ||
{ | ||
return isset($this->data['ErrorMsg']) && !empty($this->data['ErrorMsg']) ? $this->data['ErrorMsg'] : null; | ||
} | ||
|
||
/** | ||
* Return card reference | ||
* | ||
* @return string | ||
*/ | ||
public function getCardReference() | ||
{ | ||
return isset($this->data['Token']) ? $this->data['Token'] : null; | ||
} | ||
|
||
} |
Oops, something went wrong.