Skip to content

Commit

Permalink
Tweak/plugin header updates (#275)
Browse files Browse the repository at this point in the history
Adds 3 new headers to every Request to the Amazon Pay API.

---------

Co-authored-by: Dimitrios Pantazis <dpanta94@gmail.com>
  • Loading branch information
alexiglesias31 and dpanta94 authored Oct 17, 2023
1 parent 20c0f21 commit e9e4dc9
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 45 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"yoast/phpunit-polyfills": "^1.0"
},
"require": {
"amzn/amazon-pay-api-sdk-php": "2.5.2",
"amzn/amazon-pay-api-sdk-php": "2.6.2",
"php": "7.*"
}
}
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions includes/class-wc-amazon-payments-advanced-api-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,18 @@ public static function validate_api_keys() {
return false;
}

/**
* Returns extra headers to be added to requests against Amazon Pay API.
*
* @return array
*/
protected static function get_amazon_pay_platform_headers() {
$version_suffix = wc_apa()->get_gateway() instanceof WC_Gateway_Amazon_Payments_Advanced_Legacy ? '-legacy' : '';

return array(
'x-amz-pay-platform-version' => WC()->version,
'x-amz-pay-integrator-version' => wc_apa()->version . $version_suffix,
'x-amz-pay-integrator-id' => static::AMAZON_PAY_FOR_WOOCOMMERCE_SP_ID,
);
}
}
178 changes: 141 additions & 37 deletions includes/class-wc-amazon-payments-advanced-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,20 @@ public static function validate_api_keys() {
$client = self::get_client();
$payload = self::create_checkout_session_params();

$headers = array( 'x-amz-pay-Idempotency-Key' => uniqid() );
$result = $client->createCheckoutSession( $payload, $headers );
$headers = array_merge(
array( 'x-amz-pay-Idempotency-Key' => uniqid() ),
self::get_amazon_pay_platform_headers()
);

wc_apa()->log(
'Validating API keys.',
array(
'payload' => $payload,
'headers' => $headers,
)
);

$result = $client->createCheckoutSession( $payload, $headers );
if ( ! isset( $result['status'] ) || 201 !== $result['status'] ) {
throw new Exception( __( 'Error: API is not responding.', 'woocommerce-gateway-amazon-payments-advanced' ) );
}
Expand All @@ -99,7 +111,17 @@ public static function validate_api_keys() {
* @return array
*/
public static function trigger_alexa_notifications( $payload ) {
return self::get_client()->deliveryTrackers( $payload );
$headers = self::get_amazon_pay_platform_headers();

wc_apa()->log(
'Enabling Alexa notifications.',
array(
'payload' => $payload,
'headers' => $headers,
)
);

return self::get_client()->deliveryTrackers( $payload, $headers );
}

/**
Expand Down Expand Up @@ -486,7 +508,17 @@ public static function get_create_checkout_classic_session_config( $payload ) {
*/
public static function get_checkout_session_data( $checkout_session_id ) {
$client = self::get_client();
$result = $client->getCheckoutSession( $checkout_session_id );

$headers = self::get_amazon_pay_platform_headers();

wc_apa()->log(
sprintf( 'Retrieving session data for session %s', $checkout_session_id ),
array(
'headers' => $headers,
)
);

$result = $client->getCheckoutSession( $checkout_session_id, $headers );
if ( ! isset( $result['status'] ) || 200 !== $result['status'] ) {
return new WP_Error( $result['status'], __( 'Error while getting checkout session.', 'woocommerce-gateway-amazon-payments-advanced' ) );
}
Expand Down Expand Up @@ -533,7 +565,10 @@ protected static function normalize_address( $address ) {
public static function update_checkout_session_data( $checkout_session_id, $data = array() ) {
$client = self::get_client();

$headers = self::get_extra_headers( __FUNCTION__ );
$headers = array_merge(
self::get_extra_headers( __FUNCTION__ ),
self::get_amazon_pay_platform_headers()
);

wc_apa()->log(
sprintf( 'Checkout Session ID %s', $checkout_session_id ),
Expand Down Expand Up @@ -565,9 +600,18 @@ public static function update_checkout_session_data( $checkout_session_id, $data
* @return object|WP_Error API Response, or WP_Error.
*/
public static function complete_checkout_session( $checkout_session_id, $data = array() ) {
$client = self::get_client();
wc_apa()->log( sprintf( 'Checkout Session ID %s', $checkout_session_id ), $data );
$result = $client->completeCheckoutSession( $checkout_session_id, $data );
$client = self::get_client();
$headers = self::get_amazon_pay_platform_headers();

wc_apa()->log(
sprintf( 'Completing Checkout Session ID %s', $checkout_session_id ),
array(
'data' => $data,
'headers' => $headers,
)
);

$result = $client->completeCheckoutSession( $checkout_session_id, $data, $headers );

$response = json_decode( $result['response'] );

Expand Down Expand Up @@ -619,7 +663,17 @@ public static function get_languages_per_region() {
*/
public static function get_charge_permission( $charge_permission_id ) {
$client = self::get_client();
$result = $client->getChargePermission( $charge_permission_id );

$headers = self::get_amazon_pay_platform_headers();

wc_apa()->log(
sprintf( 'Retrieving Charge Permission ID %s', $charge_permission_id ),
array(
'headers' => $headers,
)
);

$result = $client->getChargePermission( $charge_permission_id, $headers );

$response = json_decode( $result['response'] );

Expand All @@ -638,7 +692,17 @@ public static function get_charge_permission( $charge_permission_id ) {
*/
public static function get_charge( $charge_id ) {
$client = self::get_client();
$result = $client->getCharge( $charge_id );

$headers = self::get_amazon_pay_platform_headers();

wc_apa()->log(
sprintf( 'Retrieving Charge ID %s', $charge_id ),
array(
'headers' => $headers,
)
);

$result = $client->getCharge( $charge_id, $headers );

$response = json_decode( $result['response'] );

Expand All @@ -657,7 +721,17 @@ public static function get_charge( $charge_id ) {
*/
public static function get_refund( $refund_id ) {
$client = self::get_client();
$result = $client->getRefund( $refund_id );

$headers = self::get_amazon_pay_platform_headers();

wc_apa()->log(
sprintf( 'Retrieving Refund ID %s', $refund_id ),
array(
'headers' => $headers,
)
);

$result = $client->getRefund( $refund_id, $headers );

$response = json_decode( $result['response'] );

Expand Down Expand Up @@ -706,7 +780,10 @@ public static function capture_charge( $charge_id, $data = array() ) {
// TODO: Test with lower amount of captured than charge (multiple charges per capture).
}

$headers = self::get_extra_headers( __FUNCTION__ );
$headers = array_merge(
self::get_extra_headers( __FUNCTION__ ),
self::get_amazon_pay_platform_headers()
);

wc_apa()->log(
sprintf( 'Charge ID %s.', $charge_id ),
Expand All @@ -716,15 +793,17 @@ public static function capture_charge( $charge_id, $data = array() ) {
)
);

$headers = array_merge(
$headers,
array(
'x-amz-pay-idempotency-key' => self::generate_uuid(),
)
);

$result = $client->captureCharge(
$charge_id,
$data,
array_merge(
$headers,
array(
'x-amz-pay-idempotency-key' => self::generate_uuid(),
)
)
$headers
);

$response = json_decode( $result['response'] );
Expand Down Expand Up @@ -763,7 +842,10 @@ public static function refund_charge( $charge_id, $amount = null, $data = array(
$data['refundAmount']['amount'] = $amount;
}

$headers = self::get_extra_headers( __FUNCTION__ );
$headers = array_merge(
self::get_extra_headers( __FUNCTION__ ),
self::get_amazon_pay_platform_headers()
);

wc_apa()->log(
sprintf( 'Charge ID %s.', $charge_id ),
Expand All @@ -773,14 +855,16 @@ public static function refund_charge( $charge_id, $amount = null, $data = array(
)
);

$headers = array_merge(
$headers,
array(
'x-amz-pay-idempotency-key' => self::generate_uuid(),
)
);

$result = $client->createRefund(
$data,
array_merge(
$headers,
array(
'x-amz-pay-idempotency-key' => self::generate_uuid(),
)
)
$headers
);

$response = json_decode( $result['response'] );
Expand All @@ -804,15 +888,27 @@ public static function refund_charge( $charge_id, $amount = null, $data = array(
*/
public static function cancel_charge( $charge_id, $reason = 'Order Cancelled' ) {
$client = self::get_client();
wc_apa()->log( sprintf( 'Charge ID %s.', $charge_id ) );

$result = $client->cancelCharge(
$charge_id,
$data = array(
'cancellationReason' => $reason, // TODO: Make dynamic.
);

$headers = self::get_amazon_pay_platform_headers();

wc_apa()->log(
sprintf( 'Charge ID %s.', $charge_id ),
array(
'cancellationReason' => $reason, // TODO: Make dynamic.
'data' => $data,
'headers' => $headers,
)
);

$result = $client->cancelCharge(
$charge_id,
$data,
$headers
);

$response = json_decode( $result['response'] );

if ( ! isset( $result['status'] ) || ! in_array( $result['status'], array( 200, 201 ), true ) ) {
Expand Down Expand Up @@ -927,7 +1023,10 @@ public static function create_charge( $charge_permission_id, $data ) {
$data['chargeAmount'] = (array) $charge_permission->limits->amountBalance; // phpcs:ignore WordPress.NamingConventions
}

$headers = self::get_extra_headers( __FUNCTION__ );
$headers = array_merge(
self::get_extra_headers( __FUNCTION__ ),
self::get_amazon_pay_platform_headers()
);

wc_apa()->log(
sprintf( 'Charge Permission ID %s.', $charge_permission_id ),
Expand All @@ -937,14 +1036,16 @@ public static function create_charge( $charge_permission_id, $data ) {
)
);

$headers = array_merge(
$headers,
array(
'x-amz-pay-idempotency-key' => self::generate_uuid(),
)
);

$result = $client->createCharge(
$data,
array_merge(
$headers,
array(
'x-amz-pay-idempotency-key' => self::generate_uuid(),
)
)
$headers
);

$response = json_decode( $result['response'] );
Expand All @@ -969,7 +1070,10 @@ public static function create_charge( $charge_permission_id, $data ) {
public static function close_charge_permission( $charge_permission_id, $reason = 'Subscription Cancelled' ) {
$client = self::get_client();

$headers = self::get_extra_headers( __FUNCTION__ );
$headers = array_merge(
self::get_extra_headers( __FUNCTION__ ),
self::get_amazon_pay_platform_headers()
);

wc_apa()->log( sprintf( 'Charge Permission ID %s.', $charge_permission_id ), array( 'headers' => $headers ) );

Expand Down

0 comments on commit e9e4dc9

Please sign in to comment.