diff --git a/src/BankAccount.php b/src/BankAccount.php new file mode 100644 index 00000000..2109f73f --- /dev/null +++ b/src/BankAccount.php @@ -0,0 +1,601 @@ +initialize($parameters); + } + + /** + * Initialize the object with parameters. + * + * If any unknown parameters passed, they will be ignored. + * + * @param array $parameters An associative array of parameters + * + * @return $this + */ + public function initialize($parameters = null) + { + $this->parameters = new ParameterBag; + + Helper::initialize($this, $parameters); + + return $this; + } + + public function getParameters() + { + return $this->parameters->all(); + } + + protected function getParameter($key) + { + return $this->parameters->get($key); + } + + protected function setParameter($key, $value) + { + $this->parameters->set($key, $value); + + return $this; + } + + /** + * All known/supported bank account types, and a regular expression to match them. + * + * + * @return array + */ + public function getSupportedAccountType() + { + return array( + static::ACCOUNT_TYPE_CHECKING, + static::ACCOUNT_TYPE_BUSINESS_CHECKING, + static::ACCOUNT_TYPE_SAVINGS + ); + } + + /** + * Validate this bank account. If the bank account is invalid, InvalidArgumentException is thrown. + * + */ + public function validate() + { + if (!in_array($this->getBankAccountType(), $this->getSupportedAccountType())) { + throw new \InvalidArgumentException('The bank account type is not in the supported list.'); + } + } + + + public function getAccountNumber() + { + return $this->getParameter('accountNumber'); + } + + public function setAccountNumber($value) + { + // strip non-numeric characters + return $this->setParameter('accountNumber', preg_replace('/\D/', '', $value)); + } + + public function getNumberLastFour() + { + return substr($this->getAccountNumber(), -4, 4) ?: null; + } + + public function getRoutingNumber() + { + return $this->getParameter('routingNumber'); + } + + public function setRoutingNumber($value) + { + // strip non-numeric characters + return $this->setParameter('routingNumber', preg_replace('/\D/', '', $value)); + } + + public function getBankAccountType() + { + return $this->getParameter('bankAccountType'); + } + + public function setBankAccountType($value) + { + return $this->setParameter('bankAccountType', $value); + } + + public function getBankName() + { + return $this->getParameter('bankName'); + } + + public function setBankName($value) + { + return $this->setParameter('bankName', $value); + } + + public function getFirstName() + { + return $this->getBillingFirstName(); + } + + public function setFirstName($value) + { + $this->setBillingFirstName($value); + $this->setShippingFirstName($value); + + return $this; + } + + public function getLastName() + { + return $this->getBillingLastName(); + } + + public function setLastName($value) + { + $this->setBillingLastName($value); + $this->setShippingLastName($value); + + return $this; + } + + public function getName() + { + return $this->getBillingName(); + } + + public function setName($value) + { + $this->setBillingName($value); + $this->setShippingName($value); + + return $this; + } + + public function getStartMonth() + { + return $this->getParameter('startMonth'); + } + + public function setStartMonth($value) + { + return $this->setParameter('startMonth', (int) $value); + } + + public function getStartYear() + { + return $this->getParameter('startYear'); + } + + public function setStartYear($value) + { + return $this->setYearParameter('startYear', $value); + } + + /** + * Get the card start date, using the specified date format string + * + * @param string $format + * + * @return string + */ + public function getStartDate($format) + { + return gmdate($format, gmmktime(0, 0, 0, $this->getStartMonth(), 1, $this->getStartYear())); + } + + public function getIssueNumber() + { + return $this->getParameter('issueNumber'); + } + + public function setIssueNumber($value) + { + return $this->setParameter('issueNumber', $value); + } + + public function getBillingName() + { + return trim($this->getBillingFirstName() . ' ' . $this->getBillingLastName()); + } + + public function setBillingName($value) + { + $names = explode(' ', $value, 2); + $this->setBillingFirstName($names[0]); + $this->setBillingLastName(isset($names[1]) ? $names[1] : null); + + return $this; + } + + public function getBillingFirstName() + { + return $this->getParameter('billingFirstName'); + } + + public function setBillingFirstName($value) + { + return $this->setParameter('billingFirstName', $value); + } + + public function getBillingLastName() + { + return $this->getParameter('billingLastName'); + } + + public function setBillingLastName($value) + { + return $this->setParameter('billingLastName', $value); + } + + public function getBillingCompany() + { + return $this->getParameter('billingCompany'); + } + + public function setBillingCompany($value) + { + return $this->setParameter('billingCompany', $value); + } + + public function getBillingAddress1() + { + return $this->getParameter('billingAddress1'); + } + + public function setBillingAddress1($value) + { + return $this->setParameter('billingAddress1', $value); + } + + public function getBillingAddress2() + { + return $this->getParameter('billingAddress2'); + } + + public function setBillingAddress2($value) + { + return $this->setParameter('billingAddress2', $value); + } + + public function getBillingCity() + { + return $this->getParameter('billingCity'); + } + + public function setBillingCity($value) + { + return $this->setParameter('billingCity', $value); + } + + public function getBillingPostcode() + { + return $this->getParameter('billingPostcode'); + } + + public function setBillingPostcode($value) + { + return $this->setParameter('billingPostcode', $value); + } + + public function getBillingState() + { + return $this->getParameter('billingState'); + } + + public function setBillingState($value) + { + return $this->setParameter('billingState', $value); + } + + public function getBillingCountry() + { + return $this->getParameter('billingCountry'); + } + + public function setBillingCountry($value) + { + return $this->setParameter('billingCountry', $value); + } + + public function getBillingPhone() + { + return $this->getParameter('billingPhone'); + } + + public function setBillingPhone($value) + { + return $this->setParameter('billingPhone', $value); + } + + public function getShippingName() + { + return trim($this->getShippingFirstName() . ' ' . $this->getShippingLastName()); + } + + public function setShippingName($value) + { + $names = explode(' ', $value, 2); + $this->setShippingFirstName($names[0]); + $this->setShippingLastName(isset($names[1]) ? $names[1] : null); + + return $this; + } + + public function getShippingFirstName() + { + return $this->getParameter('shippingFirstName'); + } + + public function setShippingFirstName($value) + { + return $this->setParameter('shippingFirstName', $value); + } + + public function getShippingLastName() + { + return $this->getParameter('shippingLastName'); + } + + public function setShippingLastName($value) + { + return $this->setParameter('shippingLastName', $value); + } + + public function getShippingCompany() + { + return $this->getParameter('shippingCompany'); + } + + public function setShippingCompany($value) + { + return $this->setParameter('shippingCompany', $value); + } + + public function getShippingAddress1() + { + return $this->getParameter('shippingAddress1'); + } + + public function setShippingAddress1($value) + { + return $this->setParameter('shippingAddress1', $value); + } + + public function getShippingAddress2() + { + return $this->getParameter('shippingAddress2'); + } + + public function setShippingAddress2($value) + { + return $this->setParameter('shippingAddress2', $value); + } + + public function getShippingCity() + { + return $this->getParameter('shippingCity'); + } + + public function setShippingCity($value) + { + return $this->setParameter('shippingCity', $value); + } + + public function getShippingPostcode() + { + return $this->getParameter('shippingPostcode'); + } + + public function setShippingPostcode($value) + { + return $this->setParameter('shippingPostcode', $value); + } + + public function getShippingState() + { + return $this->getParameter('shippingState'); + } + + public function setShippingState($value) + { + return $this->setParameter('shippingState', $value); + } + + public function getShippingCountry() + { + return $this->getParameter('shippingCountry'); + } + + public function setShippingCountry($value) + { + return $this->setParameter('shippingCountry', $value); + } + + public function getShippingPhone() + { + return $this->getParameter('shippingPhone'); + } + + public function setShippingPhone($value) + { + return $this->setParameter('shippingPhone', $value); + } + + public function getAddress1() + { + return $this->getParameter('billingAddress1'); + } + + public function setAddress1($value) + { + $this->setParameter('billingAddress1', $value); + $this->setParameter('shippingAddress1', $value); + + return $this; + } + + public function getAddress2() + { + return $this->getParameter('billingAddress2'); + } + + public function setAddress2($value) + { + $this->setParameter('billingAddress2', $value); + $this->setParameter('shippingAddress2', $value); + + return $this; + } + + public function getCity() + { + return $this->getParameter('billingCity'); + } + + public function setCity($value) + { + $this->setParameter('billingCity', $value); + $this->setParameter('shippingCity', $value); + + return $this; + } + + public function getPostcode() + { + return $this->getParameter('billingPostcode'); + } + + public function setPostcode($value) + { + $this->setParameter('billingPostcode', $value); + $this->setParameter('shippingPostcode', $value); + + return $this; + } + + public function getState() + { + return $this->getParameter('billingState'); + } + + public function setState($value) + { + $this->setParameter('billingState', $value); + $this->setParameter('shippingState', $value); + + return $this; + } + + public function getCountry() + { + return $this->getParameter('billingCountry'); + } + + public function setCountry($value) + { + $this->setParameter('billingCountry', $value); + $this->setParameter('shippingCountry', $value); + + return $this; + } + + public function getPhone() + { + return $this->getParameter('billingPhone'); + } + + public function setPhone($value) + { + $this->setParameter('billingPhone', $value); + $this->setParameter('shippingPhone', $value); + + return $this; + } + + public function getCompany() + { + return $this->getParameter('billingCompany'); + } + + public function setCompany($value) + { + $this->setParameter('billingCompany', $value); + $this->setParameter('shippingCompany', $value); + + return $this; + } + + public function getEmail() + { + return $this->getParameter('email'); + } + + public function setEmail($value) + { + return $this->setParameter('email', $value); + } + + public function getBirthday($format = 'Y-m-d') + { + $value = $this->getParameter('birthday'); + + return $value ? $value->format($format) : null; + } + + public function setBirthday($value) + { + if ($value) { + $value = new DateTime($value, new DateTimeZone('UTC')); + } else { + $value = null; + } + + return $this->setParameter('birthday', $value); + } + + public function getGender() + { + return $this->getParameter('gender'); + } + + public function setGender($value) + { + return $this->setParameter('gender', $value); + } +} diff --git a/src/Message/AIMAuthorizeRequest.php b/src/Message/AIMAuthorizeRequest.php index 7e58c4d4..bb6f1a86 100644 --- a/src/Message/AIMAuthorizeRequest.php +++ b/src/Message/AIMAuthorizeRequest.php @@ -9,18 +9,36 @@ class AIMAuthorizeRequest extends AbstractRequest { protected $action = 'AUTH_ONLY'; + const ECHECK = "ECHECK"; + const CREDIT_CARD = "CC"; + public function getData() { - $this->validate('amount', 'card'); - $this->getCard()->validate(); + $this->validate('amount'); $data = $this->getBaseData(); $data['x_customer_ip'] = $this->getClientIp(); - $data['x_card_num'] = $this->getCard()->getNumber(); - $data['x_exp_date'] = $this->getCard()->getExpiryDate('my'); - $data['x_card_code'] = $this->getCard()->getCvv(); $data['x_cust_id'] = $this->getCustomerId(); + if ($card = $this->getCard()) { + $card->validate(); + $data['x_card_num'] = $this->getCard()->getNumber(); + $data['x_exp_date'] = $this->getCard()->getExpiryDate('my'); + $data['x_card_code'] = $this->getCard()->getCvv(); + $data['x_method'] = self::CREDIT_CARD; + } elseif ($bankAccount = $this->getBankAccount()) { + $bankAccount->validate(); + $data['x_bank_aba_code'] = $this->getBankAccount()->getRoutingNumber(); + $data['x_bank_acct_num'] = $this->getBankAccount()->getAccountNumber(); + $data['x_bank_acct_type'] = $this->getBankAccount()->getBankAccountType(); + $data['x_bank_name'] = $this->getBankAccount()->getBankName(); + $data['x_bank_acct_name'] = $this->getBankAccount()->getName(); + $data['x_echeck_type'] = "WEB"; + $data['x_recurring_billing'] = "FALSE";//NEED when we set echeck_type is WEB or TEL + $data['x_method'] = self::ECHECK; + } + + if ($this->getTestMode()) { $data['x_test_request'] = 'TRUE'; } diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index a8d45f19..83e61db0 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -2,6 +2,8 @@ namespace Omnipay\AuthorizeNet\Message; +use Omnipay\AuthorizeNet\BankAccount; + /** * Authorize.Net Abstract Request */ @@ -60,6 +62,20 @@ public function setHashSecret($value) return $this->setParameter('hashSecret', $value); } + public function getBankAccount() + { + return $this->getParameter('bankAccount'); + } + + public function setBankAccount($value) + { + if ($value && !$value instanceof BankAccount) { + $value = new BankAccount($value); + } + + return $this->setParameter('bankAccount', $value); + } + protected function getBaseData() { $data = array(); @@ -110,6 +126,34 @@ protected function getBillingData() $data['x_ship_to_state'] = $card->getShippingState(); $data['x_ship_to_zip'] = $card->getShippingPostcode(); $data['x_ship_to_country'] = $card->getShippingCountry(); + } elseif ($bankAccount = $this->getBankAccount()) { + // customer billing details + $data['x_first_name'] = $bankAccount->getBillingFirstName(); + $data['x_last_name'] = $bankAccount->getBillingLastName(); + $data['x_company'] = $bankAccount->getBillingCompany(); + $data['x_address'] = trim( + $bankAccount->getBillingAddress1()." \n". + $bankAccount->getBillingAddress2() + ); + $data['x_city'] = $bankAccount->getBillingCity(); + $data['x_state'] = $bankAccount->getBillingState(); + $data['x_zip'] = $bankAccount->getBillingPostcode(); + $data['x_country'] = $bankAccount->getBillingCountry(); + $data['x_phone'] = $bankAccount->getBillingPhone(); + $data['x_email'] = $bankAccount->getEmail(); + + // customer shipping details + $data['x_ship_to_first_name'] = $bankAccount->getShippingFirstName(); + $data['x_ship_to_last_name'] = $bankAccount->getShippingLastName(); + $data['x_ship_to_company'] = $bankAccount->getShippingCompany(); + $data['x_ship_to_address'] = trim( + $bankAccount->getShippingAddress1()." \n". + $bankAccount->getShippingAddress2() + ); + $data['x_ship_to_city'] = $bankAccount->getShippingCity(); + $data['x_ship_to_state'] = $bankAccount->getShippingState(); + $data['x_ship_to_zip'] = $bankAccount->getShippingPostcode(); + $data['x_ship_to_country'] = $bankAccount->getShippingCountry(); } return $data;