Skip to content

Commit

Permalink
Ensure API data type property order (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mglaman committed Jul 2, 2017
1 parent b55014f commit 7201ced
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 18 deletions.
14 changes: 11 additions & 3 deletions src/DataTypes/BankAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

namespace CommerceGuys\AuthNet\DataTypes;

use CommerceGuys\AuthNet\DataTypes\PaymentMethodInterface;

class BankAccount extends BaseDataType implements PaymentMethodInterface
{

protected $propertyMap = [
'accountType',
// Format of routingNumber should be nine digits or four X's followed by the last four digits.
'routingNumber',
// Format of accountNumber should be numeric string or four X's followed by the last four digits.
'accountNumber',
'nameOnAccount',
'echeckType',
'bankName',
'checkNumber',
];
}
10 changes: 9 additions & 1 deletion src/DataTypes/BaseDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

abstract class BaseDataType implements DataTypeInterface
{
protected $propertyMap = [];
protected $properties;

public function __construct(array $values = [])
Expand All @@ -25,7 +26,14 @@ protected function validate(array $values)

public function toArray()
{
return $this->properties;
$sorted_properties = [];
foreach ($this->propertyMap as $property_key) {
if (isset($this->properties[$property_key])) {
$sorted_properties[$property_key] = $this->properties[$property_key];
}
}
$sorted_properties += $this->properties;
return $sorted_properties;
}

public function getType()
Expand Down
12 changes: 12 additions & 0 deletions src/DataTypes/BillTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,16 @@
class BillTo extends BaseDataType
{

protected $propertyMap = [
'firstName',
'lastName',
'company',
'address',
'city',
'state',
'zip',
'country',
'phoneNumber',
'faxNumber',
];
}
7 changes: 5 additions & 2 deletions src/DataTypes/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace CommerceGuys\AuthNet\DataTypes;

use CommerceGuys\AuthNet\DataTypes\PaymentMethodInterface;

class CreditCard extends BaseDataType implements PaymentMethodInterface
{

protected $propertyMap = [
'cardNumber',
'expirationDate',
'cardCode',
];
}
8 changes: 8 additions & 0 deletions src/DataTypes/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@
class LineItem extends BaseDataType
{

protected $propertyMap = [
'itemId',
'name',
'description',
'quantity',
// Price of an item per unit, excluding tax, freight, and duty.
'unitPrice',
];
}
4 changes: 4 additions & 0 deletions src/DataTypes/MerchantAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
class MerchantAuthentication extends BaseDataType
{

protected $propertyMap = [
'name',
'transactionKey',
];
}
4 changes: 4 additions & 0 deletions src/DataTypes/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
class Order extends BaseDataType
{

protected $propertyMap = [
'invoiceNumber',
'description',
];
}
9 changes: 8 additions & 1 deletion src/DataTypes/PaymentProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

namespace CommerceGuys\AuthNet\DataTypes;

// @todo If the billTo element is not before payment, an error occurs.
class PaymentProfile extends BaseDataType
{

protected $propertyMap = [
'customerType',
'billTo',
'payment',
'defaultPaymentProfile',
];

public function addPayment(PaymentMethodInterface $paymentMethod)
{
$this->properties['payment'][$paymentMethod->getType()] = $paymentMethod->toArray();
Expand Down
7 changes: 7 additions & 0 deletions src/DataTypes/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class Profile extends BaseDataType
{
protected $propertyMap = [
'merchantCustomerId',
'description',
'email',
'paymentProfiles',
];

public function addPaymentProfile(PaymentProfile $profile)
{
$this->properties['paymentProfiles'] = $profile->toArray();
Expand Down
18 changes: 18 additions & 0 deletions src/DataTypes/ShipTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace CommerceGuys\AuthNet\DataTypes;

class ShipTo extends BaseDataType
{

protected $propertyMap = [
'firstName',
'lastName',
'company',
'address',
'city',
'state',
'zip',
'country',
];
}
30 changes: 29 additions & 1 deletion src/DataTypes/TransactionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,38 @@ class TransactionRequest extends BaseDataType
const REFUND = 'refundTransaction';
const VOID = 'voidTransaction';

protected $propertyMap = [
'transactionType',
'amount',
'payment',
'profile',
'solution',
'order',
'lineItems',
'tax',
'shipping',
'taxExempt',
'poNumber',
'customer',
'billTo',
'shipTo',
'customerIP',
'cardholderAuthentication',
'retail',
'employeeId',
'transactionSettings',
'userFields',
];

protected $properties = [
'solution' => [
'id' => 'A1000009',
],
];

public function addPayment(CreditCard $creditCard)
{
$this->properties['payment'][$creditCard->getType()] = $creditCard->toArray();
$this->properties['solution']['id'] = 'A1000009';
}
public function addOrder(Order $order)
{
Expand Down
19 changes: 9 additions & 10 deletions tests/CustomerPaymentProfileRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function testCreateCustomerPaymentProfileWithCreditCard()
$request->setProfile($profile);
$request->setValidationMode('none');
$response = $request->execute();

$customerProfileId = $response->customerProfileId;

$request = new CreateCustomerPaymentProfileRequest($this->configurationXml, $this->client);
Expand Down Expand Up @@ -104,7 +103,15 @@ public function testCreateCustomerPaymentProfileWithBankAccount()
$paymentProfile = new PaymentProfile([
'customerType' => 'individual',
]);
// @note: You must add the billTo first.

// Add payment first, to assert properties mapped in proper order.
$paymentProfile->addPayment(new BankAccount([
'accountType' => 'checking',
'routingNumber' => '111000614',
'accountNumber' => '123456789',
'nameOnAccount' => 'Dwayne Johnson',
'bankName' => 'Bank of America'
]));
$paymentProfile->addBillTo(new BillTo([
'firstName' => 'Johnny',
'lastName' => 'Appleseed',
Expand All @@ -116,14 +123,6 @@ public function testCreateCustomerPaymentProfileWithBankAccount()
'phoneNumber' => '5555555555',
]));

$paymentProfile->addPayment(new BankAccount([
'accountType' => 'checking',
'routingNumber' => '111000614',
'accountNumber' => '123456789',
'nameOnAccount' => 'Dwayne Johnson',
'bankName' => 'Bank of America'
]));

$request->setPaymentProfile($paymentProfile);

$response = $request->execute();
Expand Down

0 comments on commit 7201ced

Please sign in to comment.