Skip to content

Commit

Permalink
Amazon Pay PHP SDK 3.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bjguillot committed Nov 21, 2019
1 parent 3b05458 commit 9b59f3a
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.lock

amazon-pay.phar
build
76 changes: 74 additions & 2 deletions AmazonPay/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class Client implements ClientInterface, LoggerAwareInterface
{
const SDK_VERSION = '3.5.0';
const SDK_VERSION = '3.6.0';
const MWS_VERSION = '2013-01-01';
const MAX_ERROR_RETRY = 3;

Expand Down Expand Up @@ -365,7 +365,7 @@ private function setParametersAndPost($parameters, $fieldMappings, $requestParam
if (!is_bool($value)) {
throw new \Exception($param . ' value ' . $value . ' is of type ' . gettype($value) . ' and should be a boolean value');
}
} elseif ($param === 'provider_credit_details' || $param === 'provider_credit_reversal_details' || $param === 'order_item_categories') {
} elseif ($param === 'provider_credit_details' || $param === 'provider_credit_reversal_details' || $param === 'order_item_categories' || $param === 'notification_configuration_list') {
if (!is_array($value)) {
throw new \Exception($param . ' value ' . $value . ' is of type ' . gettype($value) . ' and should be an array value');
}
Expand All @@ -382,6 +382,8 @@ private function setParametersAndPost($parameters, $fieldMappings, $requestParam
$parameters = $this->setProviderCreditReversalDetails($parameters, $value);
} elseif ($param === 'order_item_categories') {
$parameters = $this->setOrderItemCategories($parameters, $value);
} elseif ($param == 'notification_configuration_list') {
$parameters = $this->setNotificationConfigurationList($parameters, $value);
}

} else {
Expand Down Expand Up @@ -459,6 +461,32 @@ private function setOrderItemCategories($parameters, $categories)
}


/* setMerchantNotificationUrls - helper function used by SetMerchantNotificationConfiguration API to set
* one or more Notification Configurations
*/
private function setNotificationConfigurationList($parameters, $configuration)
{
$configurationIndex = 0;
if (!is_array($configuration)) {
throw new \Exception('Notification Configuration List value ' . $configuration . ' is of type ' . gettype($configuration) . ' and should be an array value');
}
foreach ($configuration as $url => $events) {
$configurationIndex = $configurationIndex + 1;
$parameters['NotificationConfigurationList.NotificationConfiguration.' . $configurationIndex . '.NotificationUrl'] = $url;
$eventIndex = 0;
if (!is_array($events)) {
throw new \Exception('Notification Configuration Events value ' . $events . ' is of type ' . gettype($events) . ' and should be an array value');
}
foreach ($events as $event) {
$eventIndex = $eventIndex + 1;
$parameters['NotificationConfigurationList.NotificationConfiguration.' . $configurationIndex . '.EventTypes.EventTypeList.' . $eventIndex] = $event;
}
}

return $parameters;
}


/* setProviderCreditDetails - sets the provider credit details sent via the Capture or Authorize API calls
* @param provider_id - [String]
* @param credit_amount - [String]
Expand Down Expand Up @@ -1336,6 +1364,50 @@ public function closeBillingAgreement($requestParameters = array())
}


/* GetMerchantNotificationConfiguration API Call - Returns details about the defined IPN endpoints
*
* @param requestParameters['merchant_id'] - [String]
* @optional requestParameters['mws_auth_token'] - [String]
*/
public function getMerchantNotificationConfiguration($requestParameters = array())
{
$parameters = array();
$parameters['Action'] = 'GetMerchantNotificationConfiguration';
$requestParameters = array_change_key_case($requestParameters, CASE_LOWER);

$fieldMappings = array(
'merchant_id' => 'SellerId',
'mws_auth_token' => 'MWSAuthToken'
);

$responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters);
return ($responseObject);
}


/* SetMerchantNotificationConfiguration API Call - Set IPN endpoints
*
* @param requestParameters['merchant_id'] - [String]
* @param requestParameters['notification_configuration_list'] - [Array]
* @optional requestParameters['mws_auth_token'] - [String]
*/
public function setMerchantNotificationConfiguration($requestParameters = array())
{
$parameters = array();
$parameters['Action'] = 'SetMerchantNotificationConfiguration';
$requestParameters = array_change_key_case($requestParameters, CASE_LOWER);

$fieldMappings = array(
'merchant_id' => 'SellerId',
'notification_configuration_list' => array(),
'mws_auth_token' => 'MWSAuthToken'
);

$responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters);
return ($responseObject);
}


/* charge convenience method
* Performs the API calls
* 1. SetOrderReferenceDetails / SetBillingAgreementDetails
Expand Down
17 changes: 17 additions & 0 deletions AmazonPay/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,23 @@ public function authorizeOnBillingAgreement($requestParameters = array());
public function closeBillingAgreement($requestParameters = array());


/* GetMerchantNotificationConfiguration API Call - Returns details about the defined IPN endpoints
*
* @param requestParameters['merchant_id'] - [String]
* @optional requestParameters['mws_auth_token'] - [String]
*/
public function getMerchantNotificationConfiguration($requestParameters = array());


/* SetMerchantNotificationConfiguration API Call - Set IPN endpoints
*
* @param requestParameters['merchant_id'] - [String]
* @param requestParameters['notification_configuration_list'] - [Array]
* @optional requestParameters['mws_auth_token'] - [String]
*/
public function setMerchantNotificationConfiguration($requestParameters = array());


/* charge convenience method
* Performs the API calls
* 1. SetOrderReferenceDetails / SetBillingAgreementDetails
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.6.0 - November 2019
- Add GetMerchantNotificationConfiguration API call
- Add SetMerchantNotificationConfiguration API call

3.5.0 - August 2019
- Added additional attributes (success_url, failure_url) to ConfirmBillingAgreement and (subscription_amount, currency_code) to SetBillingAgreement Details.. See Amazon Pay Strong Customer Authentication (SCA) Upgrade Integration Guide for more information.
- Synced missing changes in ClientInterface.php
Expand Down
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ See the [API Response](https://github.com/amzn/amazon-pay-sdk-php#api-response)
### IPN Handling

1. To receive IPN's successfully you will need an valid SSL on your domain.
2. You can set up your Notification endpoints in Seller Central by accessing the Integration Settings page in the Settings tab.
2. You can set up your Notification endpoints by either (a) using the Seller Central Integration Settings page Settings tab, or (b) by using the SetMerchantNotificationConfiguration API call.
3. IpnHandler.php class handles verification of the source and the data of the IPN

Add the below code into any file and set the URL to the file location in Merchant/Integrator URL by accessing Integration Settings page in the Settings tab.
Expand All @@ -320,6 +320,34 @@ $ipnHandler = new IpnHandler($headers, $body);
```
See the [IPN Response](https://github.com/amzn/amazon-pay-sdk-php#ipn-response) section for information on parsing the IPN response.

#### Setting notification endpoints using SetMerchantNotificationConfiguration API

```php
$client = new AmazonPay\Client($config);

// possible array values: ALL, ORDER_REFERENCE, PAYMENT_AUTHORIZE, PAYMENT_CAPTURE, PAYMENT_REFUND, BILLING_AGREEMENT, CHARGEBACK_DETAILED
$notificationConfiguration['https://dev.null/ipn/onetime'] = array('ORDER_REFERENCE', 'PAYMENT_AUTHORIZE', 'PAYMENT_CAPTURE');
$notificationConfiguration['https://dev.null/ipn/recurring'] = array('BILLING_AGREEMENT');
$notificationConfiguration['https://dev.null/ipn/refunds'] = array('PAYMENT_REFUND', 'CHARGEBACK_DETAILED');
$requestParameters['notification_configuration_list'] = $notificationConfiguration;

// or, if you prefer all IPNs come to the same endpoint, do this one-liner instead:
// $requestParameters['notification_configuration_list'] = array('https://dev.null/ipn' => array('ALL'));

// if you are calling on behalf of another merhcant using delegated access, be sure to set the merchant ID and auth token:
// $requestParameters['merchant_id'] = 'A3URCZVLDMDI45';
// $requestParameters['mws_auth_token'] = 'amzn.mws.d6ac8f2d-6a5f-b06a-bc12-1d0dbf4ca63d';

$response = $client->setMerchantNotificationConfiguration($requestParameters);
if ($response->toArray()['ResponseStatus'] !== '200') {
print "error occured calling API";
}

// to troubleshoot, you can call GetMerchantNotificationConfiguration to view current IPN settings
$response = $client->getMerchantNotificationConfiguration($requestParameters);
print $response->toXml();
```

### Convenience Methods

#### Charge Method
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "amzn/amazon-pay-sdk-php",
"type": "library",
"description": "Amazon Pay SDK (PHP)",
"version": "3.5.0",
"version": "3.6.0",
"keywords": [
"amazon",
"pay",
Expand Down
57 changes: 56 additions & 1 deletion tst/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,6 @@ public function testCloseBillingAgreement()

$action = 'CloseBillingAgreement';


$parameters = $this->setParametersAndPost($fieldMappings, $action);
$expectedParameters = $parameters['expectedParameters'];
$apiCallParams = $parameters['apiCallParams'];
Expand All @@ -1029,6 +1028,58 @@ public function testCloseBillingAgreement()
$this->assertEquals($apiParametersString, $expectedStringParams);
}

public function testGetMerchantNotificationConfiguration()
{
$client = new Client($this->configParams);
$fieldMappings = array(
'merchant_id' => 'SellerId',
'mws_auth_token' => 'MWSAuthToken'
);

$action = 'GetMerchantNotificationConfiguration';

$parameters = $this->setParametersAndPost($fieldMappings, $action);
$expectedParameters = $parameters['expectedParameters'];
$apiCallParams = $parameters['apiCallParams'];

$expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);

$response = $client->getMerchantNotificationConfiguration($apiCallParams);

$apiParametersString = $client->getParameters();

$this->assertEquals($apiParametersString, $expectedStringParams);
}

public function testSetMerchantNotificationConfiguration()
{
$client = new Client($this->configParams);
$fieldMappings = array(
'merchant_id' => 'SellerId',
'notification_configuration_list' => array(),
'mws_auth_token' => 'MWSAuthToken'
);

$action = 'SetMerchantNotificationConfiguration';

$parameters = $this->setParametersAndPost($fieldMappings, $action);
$expectedParameters = $parameters['expectedParameters'];
$expectedParameters['NotificationConfigurationList.NotificationConfiguration.1.NotificationUrl'] = 'https://dev.null/one';
$expectedParameters['NotificationConfigurationList.NotificationConfiguration.2.NotificationUrl'] = 'https://dev.null/two';
$expectedParameters['NotificationConfigurationList.NotificationConfiguration.1.EventTypes.EventTypeList.1'] = 'ORDER_REFERENCE';
$expectedParameters['NotificationConfigurationList.NotificationConfiguration.1.EventTypes.EventTypeList.2'] = 'PAYMENT_AUTHORIZE';
$expectedParameters['NotificationConfigurationList.NotificationConfiguration.2.EventTypes.EventTypeList.1'] = 'ALL';
$apiCallParams = $parameters['apiCallParams'];

$expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);

$response = $client->setMerchantNotificationConfiguration($apiCallParams);

$apiParametersString = $client->getParameters();

$this->assertEquals($apiParametersString, $expectedStringParams);
}

public function testCharge()
{
$client = new Client($this->configParams);
Expand Down Expand Up @@ -1183,6 +1234,10 @@ private function setParametersAndPost($fieldMappings, $action)
$apiCallParams[$parm] = array('Antiques', 'Electronics');
} elseif ($parm === 'order_status_list') {
$apiCallParams[$parm] = array('Open', 'Closed');
} elseif ($parm === 'notification_configuration_list') {
$notificationConfiguration['https://dev.null/one'] = array('ORDER_REFERENCE', 'PAYMENT_AUTHORIZE');
$notificationConfiguration['https://dev.null/two'] = array('ALL');
$apiCallParams[$parm] = $notificationConfiguration;
} elseif (!isset($expectedParameters[$value])) {
$unique_id = uniqid();
$expectedParameters[$value] = $unique_id;
Expand Down

0 comments on commit 9b59f3a

Please sign in to comment.