Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak/plugin header updates #196

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
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,
);
}
}
30 changes: 17 additions & 13 deletions includes/class-wc-amazon-payments-advanced-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static function validate_api_keys() {
$payload = self::create_checkout_session_params();

$headers = array( 'x-amz-pay-Idempotency-Key' => uniqid() );
$result = $client->createCheckoutSession( $payload, $headers );
$result = $client->createCheckoutSession( $payload, array_merge( $headers, self::get_amazon_pay_platform_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 +99,7 @@ public static function validate_api_keys() {
* @return array
*/
public static function trigger_alexa_notifications( $payload ) {
return self::get_client()->deliveryTrackers( $payload );
return self::get_client()->deliveryTrackers( $payload, self::get_amazon_pay_platform_headers() );
}

/**
Expand Down Expand Up @@ -486,7 +486,7 @@ 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 );
$result = $client->getCheckoutSession( $checkout_session_id, self::get_amazon_pay_platform_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 @@ -543,7 +543,7 @@ public static function update_checkout_session_data( $checkout_session_id, $data
)
);

$result = $client->updateCheckoutSession( $checkout_session_id, $data, $headers );
$result = $client->updateCheckoutSession( $checkout_session_id, $data, array_merge( $headers, self::get_amazon_pay_platform_headers() ) );

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

Expand All @@ -567,7 +567,7 @@ public static function update_checkout_session_data( $checkout_session_id, $data
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 );
$result = $client->completeCheckoutSession( $checkout_session_id, $data, self::get_amazon_pay_platform_headers() );

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

Expand Down Expand Up @@ -619,7 +619,7 @@ 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 );
$result = $client->getChargePermission( $charge_permission_id, self::get_amazon_pay_platform_headers() );

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

Expand All @@ -638,7 +638,7 @@ 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 );
$result = $client->getCharge( $charge_id, self::get_amazon_pay_platform_headers() );

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

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

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

Expand Down Expand Up @@ -723,7 +723,8 @@ public static function capture_charge( $charge_id, $data = array() ) {
$headers,
array(
'x-amz-pay-idempotency-key' => self::generate_uuid(),
)
),
self::get_amazon_pay_platform_headers()
)
);

Expand Down Expand Up @@ -779,7 +780,8 @@ public static function refund_charge( $charge_id, $amount = null, $data = array(
$headers,
array(
'x-amz-pay-idempotency-key' => self::generate_uuid(),
)
),
self::get_amazon_pay_platform_headers()
)
);

Expand Down Expand Up @@ -810,7 +812,8 @@ public static function cancel_charge( $charge_id, $reason = 'Order Cancelled' )
$charge_id,
array(
'cancellationReason' => $reason, // TODO: Make dynamic.
)
),
self::get_amazon_pay_platform_headers()
);

$response = json_decode( $result['response'] );
Expand Down Expand Up @@ -943,7 +946,8 @@ public static function create_charge( $charge_permission_id, $data ) {
$headers,
array(
'x-amz-pay-idempotency-key' => self::generate_uuid(),
)
),
self::get_amazon_pay_platform_headers()
)
);

Expand Down Expand Up @@ -978,7 +982,7 @@ public static function close_charge_permission( $charge_permission_id, $reason =
array(
'closureReason' => $reason, // TODO: Make dynamic.
),
$headers
array_merge( $headers, self::get_amazon_pay_platform_headers() )
);

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