Skip to content
Merged
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
54 changes: 54 additions & 0 deletions src/AuthenticateTestRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace CommerceGuys\AuthNet;

use CommerceGuys\AuthNet\DataTypes\MerchantAuthentication;
use CommerceGuys\AuthNet\Request\RequestInterface;

/**
* Represents the authenticateTestRequest endpoint for testing API credentials.
*
* This request verifies that the provided API Login ID and Transaction Key
* are valid and that the merchant is authorized to submit transactions.
*
* Commonly used to validate configuration forms or connectivity.
*
* @link https://developer.authorize.net/api/reference/index.html#gettingstarted-section-section-header
*/
class AuthenticateTestRequest extends BaseApiRequest
{

/**
* Sets the merchant authentication credentials.
*
* @param \CommerceGuys\AuthNet\DataTypes\MerchantAuthentication $merchantAuthentication
*
* @return $this
*/
public function setMerchantAuthentication(MerchantAuthentication $merchantAuthentication): self
{
$this->merchantAuthentication = $merchantAuthentication;
return $this;
}

/**
* Gets the merchant authentication credentials.
*
* @return \CommerceGuys\AuthNet\DataTypes\MerchantAuthentication|null
*/
public function getMerchantAuthentication(): ?MerchantAuthentication
{
return $this->merchantAuthentication;
}

/**
* {@inheritdoc}
*/
protected function attachData(RequestInterface $request): void
{
// Only attach merchant authentication if available.
if ($this->merchantAuthentication) {
$request->addDataType($this->merchantAuthentication);
}
}
}
50 changes: 50 additions & 0 deletions src/CreateCustomerProfileFromTransactionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace CommerceGuys\AuthNet;

use GuzzleHttp\Client;
use CommerceGuys\AuthNet\Request\RequestInterface;

/**
* Creates a customer profile, payment profile, and shipping address
* from an existing successful transaction.
*
* This request uses a transaction ID (`transId`) from a prior successful
* payment to generate a new customer profile in Authorize.Net.
*
* @link https://developer.authorize.net/api/reference/index.html#customer-profiles-create-a-customer-profile-from-a-transaction
*/
class CreateCustomerProfileFromTransactionRequest extends BaseApiRequest
{
/**
* @var string The transaction ID from a successful payment.
*/
protected $transId;

/**
* Constructs a new CreateCustomerProfileFromTransactionRequest object.
*
* @param Configuration $configuration
* The configuration object containing API credentials.
* @param Client $client
* The HTTP client for making requests.
* @param string $transId
* The transaction ID from an existing successful transaction.
*/
public function __construct(
Configuration $configuration,
Client $client,
$transId
) {
parent::__construct($configuration, $client);
$this->transId = $transId;
}

/**
* {@inheritdoc}
*/
protected function attachData(RequestInterface $request)
{
$request->addData('transId', $this->transId);
}
}
34 changes: 34 additions & 0 deletions src/DataTypes/HostedPaymentSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace CommerceGuys\AuthNet\DataTypes;

/**
* Collection of hosted payment settings.
*/
class HostedPaymentSettings extends BaseDataType
{

/**
* Adds a setting to the hosted payment configuration.
*
* @param Setting $setting
*/
public function addSetting(Setting $setting): void
{
$this->properties[] = ['setting' => $setting->toArray()];
}

/**
* Removes setting from the hosted payment configuration.
*
* @param string $name
*/
public function removeSetting(string $name): void
{
foreach ($this->properties as $key => $property) {
if ($property['setting']['settingName'] == $name) {
unset($this->properties[$key]);
}
}
}
}
37 changes: 37 additions & 0 deletions src/DataTypes/Setting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace CommerceGuys\AuthNet\DataTypes;

/**
* Represents a single setting used in HostedPaymentSettings.
*
* @see \CommerceGuys\AuthNet\DataTypes\HostedPaymentSettings
*/
class Setting extends BaseDataType
{

protected $propertyMap = [
'settingName',
'settingValue',
];

/**
* Constructs a new Setting object.
*
* @param string $name
* The name of the setting (e.g., hostedPaymentReturnOptions).
* @param mixed $value
* The value, which will be JSON-encoded if it's an array.
*/
public function __construct(string $name, $value)
{
parent::__construct();
$this->properties['settingName'] = $name;
// Encode the value.
if (is_array($value)) {
$value = json_encode($value, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
}
$this->properties['settingValue'] = $value;
}

}
14 changes: 13 additions & 1 deletion src/GetCustomerProfileRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class GetCustomerProfileRequest extends BaseApiRequest
{
protected $customerProfileId;
protected $unmaskExpirationDate = false;

public function __construct(
Configuration $configuration,
Expand All @@ -23,8 +24,19 @@ public function __construct(
$this->customerProfileId = $customerProfileId;
}

protected function attachData(RequestInterface $request)
/**
* @param boolean $unmaskExpirationDate
*/
public function setUnmaskExpirationDate($unmaskExpirationDate)
{
$this->unmaskExpirationDate = $unmaskExpirationDate;
}

protected function attachData(RequestInterface $request)
{
$request->addData('customerProfileId', $this->customerProfileId);
if ($this->unmaskExpirationDate) {
$request->addData('unmaskExpirationDate', $this->unmaskExpirationDate);
}
}
}
91 changes: 91 additions & 0 deletions src/GetHostedPaymentPageRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace CommerceGuys\AuthNet;

use CommerceGuys\AuthNet\DataTypes\HostedPaymentSettings;
use CommerceGuys\AuthNet\DataTypes\TransactionRequest;
use CommerceGuys\AuthNet\Request\RequestInterface;
use GuzzleHttp\Client;

/**
* Retrieves a token to launch the Accept Hosted payment form.
*
* @link https://developer.authorize.net/api/reference/index.html#accept-suite-get-an-accept-payment-page
*/
class GetHostedPaymentPageRequest extends BaseApiRequest
{
/**
* @var \CommerceGuys\AuthNet\DataTypes\TransactionRequest|null
*/
protected $transactionRequest;

/**
* @var \CommerceGuys\AuthNet\DataTypes\HostedPaymentSettings|null;
*/
protected $hostedPaymentSettings;

/**
* Constructs a new GetHostedPaymentPageRequest object.
*
* @param Configuration $configuration
* The configuration object containing API credentials.
* @param Client $client
* The HTTP client for making requests.
* @param \CommerceGuys\AuthNet\DataTypes\TransactionRequest|null $transactionRequest
* The transaction request details.
* @param \CommerceGuys\AuthNet\DataTypes\HostedPaymentSettings|null $hostedPaymentSettings
* The settings for the Accept Hosted payment form.
*/
public function __construct(
Configuration $configuration,
Client $client,
TransactionRequest $transactionRequest = null,
HostedPaymentSettings $hostedPaymentSettings = null
) {
parent::__construct($configuration, $client);
$this->transactionRequest = $transactionRequest;
$this->hostedPaymentSettings = $hostedPaymentSettings;
}

/**
* {@inheritdoc}
*/
protected function attachData(RequestInterface $request)
{
// Attach transaction details.
if ($this->transactionRequest) {
$request->addDataType($this->transactionRequest);
}
// Add hosted payment settings.
if ($this->hostedPaymentSettings) {
$request->addDataType($this->hostedPaymentSettings);
}
}

/**
* Sets the transaction request.
*
* @param \CommerceGuys\AuthNet\DataTypes\TransactionRequest $transactionRequest
* The transaction details.
*
* @return $this
*/
public function setTransactionRequest(TransactionRequest $transactionRequest)
{
$this->transactionRequest = $transactionRequest;
return $this;
}

/**
* Sets the hosted payment settings.
*
* @param \CommerceGuys\AuthNet\DataTypes\HostedPaymentSettings $hostedPaymentSettings
*
* @return $this
*/
public function setHostedPaymentSettings(HostedPaymentSettings $hostedPaymentSettings)
{
$this->hostedPaymentSettings = $hostedPaymentSettings;
return $this;
}
}
2 changes: 1 addition & 1 deletion tests/ARBCreateSubscriptionRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testARBCreateSubscriptionRequestCRUD()
{
$interval = new Interval(['length' => 7, 'unit' => 'days']);
$paymentSchedule = new PaymentSchedule([
'startDate' => '2024-08-30',
'startDate' => (new \DateTime('+1 day'))->format('Y-m-d'),
'totalOccurrences' => 9999,
]);
$paymentSchedule->addInterval($interval);
Expand Down
63 changes: 63 additions & 0 deletions tests/AuthenticateRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace CommerceGuys\AuthNet\Tests;

use CommerceGuys\AuthNet\AuthenticateTestRequest;
use CommerceGuys\AuthNet\DataTypes\MerchantAuthentication;
use CommerceGuys\AuthNet\Tests\TestBase;

/**
* Tests the AuthenticateTestRequest.
*
* This test verifies that the provided merchant credentials are valid
* using both XML and JSON formats.
*/
class AuthenticateTestRequestTest extends TestBase
{
/**
* Tests the authenticateTestRequest using XML format.
*/
public function testAuthenticateTestXml(): void
{
$request = new AuthenticateTestRequest(
$this->configurationXml,
$this->client
);

// Explicitly set authentication in case configuration does not preload it.
$request->setMerchantAuthentication(new MerchantAuthentication([
'name' => $this->configurationXml->getApiLogin(),
'transactionKey' => $this->configurationXml->getTransactionKey(),
]));

/** @var \CommerceGuys\AuthNet\Response\XmlResponse $response */
$response = $request->execute();

$this->assertEquals('Ok', $response->getResultCode());
$this->assertEquals('I00001', $response->getMessages()[0]->getCode());
$this->assertEquals('Successful.', $response->getMessages()[0]->getText());
}

/**
* Tests the authenticateTestRequest using JSON format.
*/
public function testAuthenticateTestJson(): void
{
$request = new AuthenticateTestRequest(
$this->configurationJson,
$this->client
);

$request->setMerchantAuthentication(new MerchantAuthentication([
'name' => $this->configurationJson->getApiLogin(),
'transactionKey' => $this->configurationJson->getTransactionKey(),
]));

/** @var \CommerceGuys\AuthNet\Response\JsonResponse $response */
$response = $request->execute();

$this->assertEquals('Ok', $response->getResultCode());
$this->assertEquals('I00001', $response->getMessages()[0]->getCode());
$this->assertEquals('Successful.', $response->getMessages()[0]->getText());
}
}
Loading