diff --git a/composer.json b/composer.json index 923a5167..6fee169f 100644 --- a/composer.json +++ b/composer.json @@ -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.*" } } diff --git a/composer.lock b/composer.lock index 0942c74c..51157383 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f8ac084417c133d5c7e6a5e2f177d2b8", + "content-hash": "2ca89bcc208c35341a0045d630cdf10c", "packages": [ { "name": "amzn/amazon-pay-api-sdk-php", - "version": "2.5.2", + "version": "2.6.2", "source": { "type": "git", "url": "https://github.com/amzn/amazon-pay-api-sdk-php.git", - "reference": "5b38287612427ecb0806d2cc626c92eb49173c6f" + "reference": "4583b7320e96e18fe32bef1830d7a968c0cebf49" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/amzn/amazon-pay-api-sdk-php/zipball/5b38287612427ecb0806d2cc626c92eb49173c6f", - "reference": "5b38287612427ecb0806d2cc626c92eb49173c6f", + "url": "https://api.github.com/repos/amzn/amazon-pay-api-sdk-php/zipball/4583b7320e96e18fe32bef1830d7a968c0cebf49", + "reference": "4583b7320e96e18fe32bef1830d7a968c0cebf49", "shasum": "" }, "require": { @@ -57,9 +57,9 @@ ], "support": { "issues": "https://github.com/amzn/amazon-pay-api-sdk-php/issues", - "source": "https://github.com/amzn/amazon-pay-api-sdk-php/tree/2.5.2" + "source": "https://github.com/amzn/amazon-pay-api-sdk-php/tree/2.6.2" }, - "time": "2023-03-06T12:45:51+00:00" + "time": "2023-06-29T07:33:20+00:00" }, { "name": "paragonie/constant_time_encoding", diff --git a/includes/class-wc-amazon-payments-advanced-api-abstract.php b/includes/class-wc-amazon-payments-advanced-api-abstract.php index ac448a7d..ef708f13 100644 --- a/includes/class-wc-amazon-payments-advanced-api-abstract.php +++ b/includes/class-wc-amazon-payments-advanced-api-abstract.php @@ -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, + ); + } } diff --git a/includes/class-wc-amazon-payments-advanced-api.php b/includes/class-wc-amazon-payments-advanced-api.php index cb5eddd8..62f07956 100644 --- a/includes/class-wc-amazon-payments-advanced-api.php +++ b/includes/class-wc-amazon-payments-advanced-api.php @@ -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' ) ); } @@ -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 ); } /** @@ -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' ) ); } @@ -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 ), @@ -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'] ); @@ -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'] ); @@ -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'] ); @@ -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'] ); @@ -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 ), @@ -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'] ); @@ -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 ), @@ -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'] ); @@ -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 ) ) { @@ -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 ), @@ -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'] ); @@ -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 ) );