Skip to content

Commit

Permalink
Merge pull request #15 from academe/issue13
Browse files Browse the repository at this point in the history
Issue13 support user fields
  • Loading branch information
judgej authored Aug 20, 2019
2 parents 593896c + 31d03dc commit b72eae9
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Message/AuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Academe\AuthorizeNet\Request\Collections\LineItems;
use Academe\AuthorizeNet\Request\Model\LineItem;
use Academe\AuthorizeNet\Request\Model\CardholderAuthentication;
use Academe\AuthorizeNet\Request\Collections\UserFields;
use Academe\AuthorizeNet\Request\Model\UserField;

use Money\Parser\DecimalMoneyParser;
use Money\Currencies\ISOCurrencies;
Expand Down Expand Up @@ -252,6 +254,45 @@ public function getData()
'terminalNumber' => $this->getTerminalNumber(),
]);

if ($sourceUserFields = $this->getUserFields()) {
// Can be provided as key/value array, array of name/value pairs
// or a readymade collection of models.

if ($sourceUserFields instanceof UserFields) {
// Already a collection; just use it.

$userFields = $sourceUserFields;
} else {
$userFields = new UserFields();

if (is_array($sourceUserFields)) {
foreach ($sourceUserFields as $key => $value) {
if (is_string($key) && is_string($value)) {
// key/value pairs: 'key' => 'value'

$userFields->push(new UserField($key, $value));
}

if (is_array($value) && count($value) === 2) {
// name/value pairs: ['name' => 'the name', 'value' => 'the value']

$userFields->push(new UserField($value['name'], $value['value']));
}

if ($value instanceof UserField) {
// An array of UserField objects was supplied.

$userFields->push($value);
}
}
}
}

if ($userFields->count()) {
$transaction = $transaction->withUserFields($userFields);
}
}

return $transaction;
}

Expand Down Expand Up @@ -385,4 +426,14 @@ public function getToken()
. $opaqueDataValue;
}
}

public function setUserFields($value)
{
return $this->setParameter('userFields', $value);
}

public function getUserFields()
{
return $this->getParameter('userFields');
}
}

0 comments on commit b72eae9

Please sign in to comment.