Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/sumup-payment-gateway-for-woocommerce/build/index.asset.php
+++ b/sumup-payment-gateway-for-woocommerce/build/index.asset.php
@@ -1 +1,9 @@
-<?php return array('dependencies' => array('react', 'react-jsx-runtime', 'wp-element', 'wp-html-entities'), 'version' => '9e7b68647a16a2c30dd6');
+<?php return array(
+ 'dependencies' => array(
+ 'react',
+ 'react-jsx-runtime',
+ 'wp-element',
+ 'wp-html-entities'
+ ),
+ 'version' => 'b45e7ce5e3fc63761a1f'
+);
--- a/sumup-payment-gateway-for-woocommerce/build/settings.asset.php
+++ b/sumup-payment-gateway-for-woocommerce/build/settings.asset.php
@@ -1 +1,6 @@
-<?php return array('dependencies' => array(), 'version' => '8fc4c5f444c8272cccc3');
+<?php return array(
+ 'dependencies' => array(
+
+ ),
+ 'version' => 'b5a29e1f7664ac5f6662'
+);
--- a/sumup-payment-gateway-for-woocommerce/build/sumup-gateway.asset.php
+++ b/sumup-payment-gateway-for-woocommerce/build/sumup-gateway.asset.php
@@ -1 +1,6 @@
-<?php return array('dependencies' => array(), 'version' => '0f13b6438a4be8e5cfbd');
+<?php return array(
+ 'dependencies' => array(
+
+ ),
+ 'version' => '57b72f2e447d518b1e8d'
+);
--- a/sumup-payment-gateway-for-woocommerce/build/sumup-process-checkout.asset.php
+++ b/sumup-payment-gateway-for-woocommerce/build/sumup-process-checkout.asset.php
@@ -1 +1,6 @@
-<?php return array('dependencies' => array(), 'version' => 'd06c258565eabc9c793a');
+<?php return array(
+ 'dependencies' => array(
+
+ ),
+ 'version' => '5cf976b9547eb41f60d9'
+);
--- a/sumup-payment-gateway-for-woocommerce/example/wp-content/plugins/index.php
+++ b/sumup-payment-gateway-for-woocommerce/example/wp-content/plugins/index.php
@@ -1,2 +0,0 @@
-<?php
-// Silence is golden.
--- a/sumup-payment-gateway-for-woocommerce/includes/api/class-sumup-api-handler.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/api/class-sumup-api-handler.php
@@ -1,22 +1,21 @@
<?php
// Exit if run outside WP.
-if (!defined('ABSPATH')) {
+if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Sumup_Api_Handler.
*/
-class Sumup_Api_Handler
-{
+class Sumup_Api_Handler {
+
/**
* Sumup_Api_Handler constructor.
*/
- public function __construct()
- {
- add_action('woocommerce_api_sumup_api_handler', array($this, 'handler'));
+ public function __construct() {
+ add_action( 'woocommerce_api_sumup_api_handler', array( $this, 'handler' ) );
// Include API files.
$this->includes();
@@ -25,63 +24,62 @@
/**
* Include all API files.
*/
- private function includes()
- {
- include_once dirname(__FILE__) . '/handlers/class-sumup-create-checkout.php';
- include_once dirname(__FILE__) . '/handlers/class-sumup-store-payment-instructions.php';
- include_once dirname(__FILE__) . '/handlers/class-sumup-validation-website-handler.php';
- include_once dirname(__FILE__) . '/handlers/class-sumup-connect-website-handler.php';
- include_once dirname(__FILE__) . '/handlers/class-sumup-validate-redirect.php';
+ private function includes() {
+ include_once __DIR__ . '/handlers/class-sumup-store-payment-instructions.php';
+ include_once __DIR__ . '/handlers/class-sumup-validation-website-handler.php';
+ include_once __DIR__ . '/handlers/class-sumup-connect-website-handler.php';
+ include_once __DIR__ . '/handlers/class-sumup-validate-redirect.php';
}
/**
* Get all the setup handlers.
* @return array
*/
- public function get_handlers()
- {
- return apply_filters('sumup_api_handlers', array());
+ public function get_handlers() {
+ return apply_filters( 'sumup_api_handlers', array() );
}
/**
* Execute the request.
*/
- public function handler()
- {
+ public function handler() {
$handlers = $this->get_handlers();
- $action = isset($_GET['action']) ? sanitize_key(wp_unslash($_GET['action'])) : '';
+ // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Dispatch only: browser handlers verify their nonce and order key; provider connection handlers verify a pending connection.
+ $action = isset( $_GET['action'] ) ? sanitize_key( wp_unslash( $_GET['action'] ) ) : '';
// Return an error in case action doesn't exists .
- if (empty($action) || !array_key_exists($action, $handlers)) {
+ if ( empty( $action ) || ! array_key_exists( $action, $handlers ) ) {
wp_send_json(
array(
- 'result' => 'error',
+ 'result' => 'error',
'message' => "Invalid request. Invalid param 'action'.",
- ), 500);
+ ),
+ 500
+ );
}
- $expected_method = isset($handlers[$action]['method']) ? strtoupper($handlers[$action]['method']) : '';
- if ($expected_method && strtoupper(wp_unslash($_SERVER['REQUEST_METHOD'] ?? '')) !== $expected_method) {
+ $expected_method = isset( $handlers[ $action ]['method'] ) ? strtoupper( $handlers[ $action ]['method'] ) : '';
+ if ( $expected_method && strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ?? '' ) ) ) !== $expected_method ) {
wp_send_json(
array(
- 'result' => 'error',
- 'message' => __('Invalid request method.', 'sumup-payment-gateway-for-woocommerce'),
+ 'result' => 'error',
+ 'message' => __( 'Invalid request method.', 'sumup-payment-gateway-for-woocommerce' ),
),
405
);
}
- call_user_func($handlers[$action]['callback']);
+ call_user_func( $handlers[ $action ]['callback'] );
}
- public function send_response($result = 'success', $message = '', $data = array(), $code = 200)
- {
+ public function send_response( $result = 'success', $message = '', $data = array(), $code = 200 ) {
wp_send_json(
array(
- 'result' => $result,
+ 'result' => $result,
'message' => $message,
- 'data' => $data,
- ), $code);
+ 'data' => $data,
+ ),
+ $code
+ );
}
-
}
--- a/sumup-payment-gateway-for-woocommerce/includes/api/class-sumup-connect.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/api/class-sumup-connect.php
@@ -1,81 +1,38 @@
<?php
-if (! defined('ABSPATH')) {
+if ( ! defined( 'ABSPATH' ) ) {
exit;
}
-add_action('rest_api_init', function () {
- register_rest_route(
- 'sumup_connection/v1',
- 'connect',
- array(
- 'methods' => array('POST'),
- 'callback' => 'sumup_connect',
- 'permission_callback' => '__return_true',
- )
- );
-});
+add_action(
+ 'rest_api_init',
+ function () {
+ register_rest_route(
+ 'sumup_connection/v1',
+ 'connect',
+ array(
+ 'methods' => array( 'POST' ),
+ 'callback' => 'sumup_connect',
+ 'permission_callback' => '__return_true',
+ )
+ );
+ }
+);
/**
* Connect endpoint
*/
-function sumup_connect($request)
-{
- $post_data = json_decode($request->get_body(), true);
- $connection_id = isset($post_data['id']) ? sanitize_text_field($post_data['id']) : '';
-
- WC_SUMUP_LOGGER::log("Receive connect data");
-
- if (empty($connection_id)) {
- WC_SUMUP_LOGGER::log('Rejecting connect callback: missing connection ID');
- $reponse_body = array('status' => 'error', 'message' => 'Invalid connection ID');
- $response = new WP_REST_Response($reponse_body);
- $response->set_status(400);
- return $response;
- }
-
- if (!sumup_has_pending_connection_id($connection_id)) {
- WC_SUMUP_LOGGER::log('Rejecting connect callback: unknown connection ID ' . $connection_id);
- $reponse_body = array('status' => 'error', 'message' => 'Invalid connection ID');
- $response = new WP_REST_Response($reponse_body);
- $response->set_status(400);
- return $response;
- }
-
- if (! isset($post_data['merchant']['email'])) {
- $reponse_body = array('status' => 'error', 'message' => 'Invalid merchant email');
- $response = new WP_REST_Response($reponse_body);
- $response->set_status(400);
- return $response;
+function sumup_connect( $request ) {
+ $result = WC_Sumup_Connection::accept( json_decode( $request->get_body(), true ) );
+ if ( is_wp_error( $result ) ) {
+ return new WP_REST_Response(
+ array(
+ 'status' => 'error',
+ 'message' => $result->get_error_message(),
+ ),
+ 400
+ );
}
-
- if (! isset($post_data['merchant']['api_key'])) {
- $reponse_body = array('status' => 'error', 'message' => 'Invalid API key');
- $response = new WP_REST_Response($reponse_body);
- $response->set_status(400);
- return $response;
- }
-
- if (! isset($post_data['merchant']['merchant_code'])) {
- $reponse_body = array('status' => 'error', 'message' => 'Invalid merchant code');
- $response = new WP_REST_Response($reponse_body);
- $response->set_status(400);
- return $response;
- }
-
- $settings = get_option('woocommerce_sumup_settings');
- $settings['pay_to_email'] = $post_data['merchant']['email'];
- $settings['api_key'] = $post_data['merchant']['api_key'];
- $settings['merchant_id'] = $post_data['merchant']['merchant_code'];
- $settings['enabled'] = 'no';
- update_option('woocommerce_sumup_settings', $settings);
- update_option('sumup_connection_status', 'pending', false);
-
- sumup_delete_pending_connection_id($connection_id);
-
- $reponse_body = array('status' => 'connected');
- $response = new WP_REST_Response($reponse_body);
- $response->set_status(200);
- return $response;
+ return new WP_REST_Response( $result, 200 );
}
--- a/sumup-payment-gateway-for-woocommerce/includes/api/class-sumup-disconnect.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/api/class-sumup-disconnect.php
@@ -1,6 +1,6 @@
<?php
-if (!defined('ABSPATH')) {
+if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
@@ -8,40 +8,42 @@
* @autor Sumup
*/
-add_action('rest_api_init', function () {
- register_rest_route(
- 'sumup_disconnection/v1',
- 'disconnect',
- array(
- 'methods' => array('POST'),
- 'callback' => 'sumup_disconnect',
- 'permission_callback' => function () {
- return current_user_can('manage_woocommerce') || current_user_can('manage_options');
- },
- )
- );
-});
+add_action(
+ 'rest_api_init',
+ function () {
+ register_rest_route(
+ 'sumup_disconnection/v1',
+ 'disconnect',
+ array(
+ 'methods' => array( 'POST' ),
+ 'callback' => 'sumup_disconnect',
+ 'permission_callback' => function () {
+ return current_user_can( 'manage_woocommerce' ) || current_user_can( 'manage_options' );
+ },
+ )
+ );
+ }
+);
/**
* Disconnect endpoint
*/
-function sumup_disconnect(): WP_REST_Response
-{
+function sumup_disconnect(): WP_REST_Response {
- $settings = get_option('woocommerce_sumup_settings', array());
- if (!is_array($settings)) {
+ $settings = get_option( 'woocommerce_sumup_settings', array() );
+ if ( ! is_array( $settings ) ) {
$settings = array();
}
/**
* Verify if already excluded the credentials
*/
- if (empty($settings['api_key']) && empty($settings['client_id']) && empty($settings['client_secret'])) {
+ if ( empty( $settings['api_key'] ) && empty( $settings['client_id'] ) && empty( $settings['client_secret'] ) ) {
return new WP_REST_Response(
array(
- 'status' => 'error',
- 'message' => __('Account already disconnected', 'sumup-payment-gateway-for-woocommerce'),
+ 'status' => 'error',
+ 'message' => __( 'Account already disconnected', 'sumup-payment-gateway-for-woocommerce' ),
),
400
);
@@ -50,32 +52,35 @@
/**
* Delete the credentials
*/
- if (isset($settings['api_key'])) {
+ if ( isset( $settings['api_key'] ) ) {
$settings['api_key'] = null;
}
- if (isset($settings['client_id']) && isset($settings['client_secret'])) {
- $settings['client_id'] = null;
+ if ( isset( $settings['client_id'] ) && isset( $settings['client_secret'] ) ) {
+ $settings['client_id'] = null;
$settings['client_secret'] = null;
}
- if (isset($settings['merchant_id'])) {
+ if ( isset( $settings['merchant_id'] ) ) {
$settings['merchant_id'] = null;
}
- if (isset($settings['pay_to_email'])) {
+ if ( isset( $settings['pay_to_email'] ) ) {
$settings['pay_to_email'] = null;
}
$settings['enabled'] = 'no';
- update_option('woocommerce_sumup_settings', $settings);
- update_option('sumup_connection_status', 'not_connected', false);
- delete_option('sumup_valid_credentials');
+ update_option( 'woocommerce_sumup_settings', $settings );
+ update_option( 'sumup_connection_status', 'not_connected', false );
+ delete_option( 'sumup_google_pay_domain_status' );
+ delete_option( 'sumup_connection_currency' );
+ update_option( 'sumup_valid_currency', 0, false );
+ delete_option( 'sumup_valid_credentials' );
/*
* Redirect to sumup payment admin page
*/
return new WP_REST_Response(
array(
- 'status' => 'disconnected',
- 'redirect_url' => admin_url('admin.php?page=wc-settings&tab=checkout§ion=sumup'),
+ 'status' => 'disconnected',
+ 'redirect_url' => admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=sumup' ),
),
200
);
--- a/sumup-payment-gateway-for-woocommerce/includes/api/class-sumup-validate.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/api/class-sumup-validate.php
@@ -1,28 +1,34 @@
<?php
-if (! defined('ABSPATH')) {
+if ( ! defined( 'ABSPATH' ) ) {
exit;
}
-add_action('rest_api_init', function () {
- register_rest_route('sumup_connection/v1', 'validate', array(
- 'methods' => 'GET',
- 'callback' => 'sumup_validate_website',
- 'permission_callback' => '__return_true',
- ));
-});
+add_action(
+ 'rest_api_init',
+ function () {
+ register_rest_route(
+ 'sumup_connection/v1',
+ 'validate',
+ array(
+ 'methods' => 'GET',
+ 'callback' => 'sumup_validate_website',
+ 'permission_callback' => '__return_true',
+ )
+ );
+ }
+);
/**
* Validate endpoint
*/
-function sumup_validate_website($request)
-{
- $ip = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field($_SERVER['REMOTE_ADDR']) : '';
- $ua = isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field($_SERVER['HTTP_USER_AGENT']) : '';
- WC_SUMUP_LOGGER::log("Validate website callback ip=" . $ip . " ua=" . $ua);
- $reponse_body = array('status' => 'valid website');
- $response = new WP_REST_Response($reponse_body);
- $response->set_status(200);
+function sumup_validate_website() {
+ $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
+ $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
+ WC_SUMUP_LOGGER::log( 'Validate website callback ip=' . $ip . ' ua=' . $ua );
+ $reponse_body = array( 'status' => 'valid website' );
+ $response = new WP_REST_Response( $reponse_body );
+ $response->set_status( 200 );
return $response;
}
--- a/sumup-payment-gateway-for-woocommerce/includes/api/handlers/class-sumup-connect-website-handler.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/api/handlers/class-sumup-connect-website-handler.php
@@ -1,15 +1,14 @@
<?php
-if (!defined('ABSPATH')) {
+if ( ! defined( 'ABSPATH' ) ) {
exit;
}
-class Sumup_API_Connection_Website_Handler extends Sumup_Api_Handler
-{
+class Sumup_API_Connection_Website_Handler extends Sumup_Api_Handler {
- public function __construct()
- {
- add_filter('sumup_api_handlers', array($this, 'add_handlers'));
+
+ public function __construct() {
+ add_filter( 'sumup_api_handlers', array( $this, 'add_handlers' ) );
}
/**
@@ -19,11 +18,10 @@
* @throws Exception
*/
- public function add_handlers($handlers)
- {
+ public function add_handlers( $handlers ) {
$handlers['connection_website'] = array(
- 'callback' => array($this, 'handle'),
- 'method' => 'POST',
+ 'callback' => array( $this, 'handle' ),
+ 'method' => 'POST',
);
return $handlers;
@@ -32,63 +30,18 @@
/**
* Handle the request.
*/
- public function handle()
- {
- $json_data = file_get_contents('php://input');
- $post_data = json_decode($json_data, true);
- $connection_id = isset($post_data['id']) ? sanitize_text_field($post_data['id']) : '';
-
- WC_SUMUP_LOGGER::log( "Receive connect data handler");
-
- if ( empty( $connection_id ) ) {
- WC_SUMUP_LOGGER::log( 'Rejecting legacy connect callback: missing connection ID' );
- $reponse_body = array('status' => 'error', 'message' => 'Invalid connection ID');
- $this->send_response($reponse_body['status'],$reponse_body['message'],array() ,400);
- }
-
- if ( ! sumup_has_pending_connection_id( $connection_id ) ) {
- WC_SUMUP_LOGGER::log( 'Rejecting legacy connect callback: unknown connection ID ' . $connection_id );
- $reponse_body = array('status' => 'error', 'message' => 'Invalid connection ID');
- $this->send_response($reponse_body['status'],$reponse_body['message'],array() ,400);
- }
-
- if (!isset($post_data['merchant']['email'])) {
- $reponse_body = array('status' => 'error', 'message' => 'Invalid merchant email');
- $this->send_response($reponse_body['status'],$reponse_body['message'],array() ,400);
- }
-
- if (!isset($post_data['merchant']['api_key'])) {
- $reponse_body = array('status' => 'error', 'message' => 'Invalid API key');
- $this->send_response($reponse_body['status'],$reponse_body['message'],array() ,400);
- }
-
- if (!isset($post_data['merchant']['merchant_code'])) {
- $reponse_body = array('status' => 'error', 'message' => 'Invalid merchant code');
- $this->send_response($reponse_body['status'],$reponse_body['message'],array() ,400);
- }
-
- $settings = get_option('woocommerce_sumup_settings');
- if (!isset($settings)) {
- set_transient('pay_to_email', $post_data['merchant']['email']);
- set_transient('api_key', $post_data['merchant']['api_key']);
- set_transient('merchant_id', $post_data['merchant']['merchant_code']);
- }else{
- $settings['pay_to_email'] = $post_data['merchant']['email'];
- $settings['api_key'] = $post_data['merchant']['api_key'];
- $settings['merchant_id'] = $post_data['merchant']['merchant_code'];
- $settings['enabled'] = 'no';
- update_option('woocommerce_sumup_settings', $settings);
+ public function handle() {
+ $result = WC_Sumup_Connection::accept( $this->get_request_data() );
+ if ( is_wp_error( $result ) ) {
+ $this->send_response( 'error', $result->get_error_message(), array(), 400 );
}
-
- update_option('sumup_connection_status', 'pending', false);
-
- sumup_delete_pending_connection_id($connection_id);
-
- $reponse_body = array('status' => 'connected');
- $this->send_response($reponse_body);
-
+ // Preserve the legacy callback's response envelope.
+ $this->send_response( $result );
}
+ protected function get_request_data() {
+ return json_decode( file_get_contents( 'php://input' ), true );
+ }
}
new Sumup_API_Connection_Website_Handler();
--- a/sumup-payment-gateway-for-woocommerce/includes/api/handlers/class-sumup-create-checkout.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/api/handlers/class-sumup-create-checkout.php
@@ -1,246 +0,0 @@
-<?php
-
-if (!defined('ABSPATH')) {
- exit;
-}
-
-/**
- * API for create checkout on Woocommerce checkout blocks.
- */
-
-class Sumup_API_Create_Chekout_Handler extends Sumup_Api_Handler
-{
-
- public function __construct()
- {
- add_filter('sumup_api_handlers', array($this, 'add_handlers'));
- }
-
- public function add_handlers($handlers)
- {
- $handlers['create_checkout'] = array(
- 'callback' => array($this, 'handle'),
- 'method' => 'POST',
- );
-
- return $handlers;
- }
-
- /**
- * Handle the request.
- */
- public function handle()
- {
- $json_data = file_get_contents('php://input');
- $post_data = json_decode($json_data, true);
-
- if (!is_array($post_data)) {
- $this->send_response('error', __('Invalid request payload.', 'sumup-payment-gateway-for-woocommerce'), array(), 400);
- }
-
- $nonce = isset($post_data['nonce']) ? sanitize_text_field(wp_unslash($post_data['nonce'])) : '';
- if (!wp_verify_nonce($nonce, 'sumup-create-checkout')) {
- $this->send_response('error', __('Invalid request nonce.', 'sumup-payment-gateway-for-woocommerce'), array(), 403);
- }
-
- $order_id = isset($post_data['order_id']) ? absint($post_data['order_id']) : 0;
- if (!$order_id) {
- $this->send_response('error', __('Invalid order ID.', 'sumup-payment-gateway-for-woocommerce'), array(), 400);
- }
-
- $order_key = isset($post_data['order_key']) ? wc_clean(wp_unslash($post_data['order_key'])) : '';
- if (empty($order_key)) {
- $this->send_response('error', __('Invalid order key.', 'sumup-payment-gateway-for-woocommerce'), array(), 400);
- }
-
- $is_checkout_blocks = !empty($post_data['isCheckoutBlocks']);
- $result = $this->create_checkout($order_id, $order_key, $is_checkout_blocks);
-
- if (isset($result['status']) && 'error' === $result['status']) {
- $this->send_response($result['status'], $result['message'], array(), 400);
- }
-
- $this->send_response('success', '', $result);
-
- }
-
- private function create_checkout($order_id, $order_key = '', $is_checkout_blocks = false)
- {
-
- if (!sumup_gateway_is_configured()) {
- $message = __('Merchant account settings are incorrectly configured. Check the plugin settings page.', 'sumup-payment-gateway-for-woocommerce');
- return array(
- 'status' => 'error',
- 'message' => $message,
- 'data' => null,
- );
- }
-
- $sumup_settings = get_option('woocommerce_sumup_settings', false);
- if (empty($sumup_settings)) {
- $unavaliable_message = __('Sumup is temporarily unavailable. Please contact site admin for more information.', 'sumup-payment-gateway-for-woocommerce');
-
- return array(
- 'status' => 'error',
- 'message' => $unavaliable_message,
- 'data' => null,
- );
- }
-
- if (empty($sumup_settings['merchant_id']) && empty($sumup_settings['pay_to_email'])) {
- WC_SUMUP_LOGGER::log(
- 'Gateway configuration is incomplete: missing Merchant code.',
- array(
- 'flow' => 'blocks_checkout_create',
- 'merchant_code' => $sumup_settings['merchant_id'] ?? '',
- ),
- 'error'
- );
- $message = current_user_can('manage_options')
- ? __('Please fill "Merchant code" on the plugin settings.', 'sumup-payment-gateway-for-woocommerce')
- : __('Sorry, SumUp is not available. Try again soon.', 'sumup-payment-gateway-for-woocommerce');
- return array(
- 'status' => 'error',
- 'message' => $message,
- 'data' => null,
- );
- }
-
- $order = wc_get_order($order_id);
- if (!$order instanceof WC_Order) {
- return array(
- 'status' => 'error',
- 'message' => __('Order ID is not available to make the payment. Try again soon or contact the website support.', 'sumup-payment-gateway-for-woocommerce'),
- 'data' => null,
- );
- }
-
- $gateway = $this->get_sumup_gateway('sumup');
- $log_context = $gateway->get_order_log_context(
- $order,
- array(
- 'flow' => 'blocks_checkout_create',
- 'merchant_code' => $sumup_settings['merchant_id'] ?? '',
- )
- );
- $order_access = $gateway->validate_order_access_for_checkout($order, $order_key);
- if (!$order_access['valid']) {
- WC_SUMUP_LOGGER::log('Rejected checkout creation because order validation failed.', $log_context, 'warning');
- return array(
- 'status' => 'error',
- 'message' => $order_access['error'],
- 'data' => null,
- );
- }
-
- $access_token = Wc_Sumup_Access_Token::get($sumup_settings['client_id'], $sumup_settings['client_secret'], $sumup_settings['api_key'], false, $log_context);
- if (!isset($access_token['access_token'])) {
- WC_SUMUP_LOGGER::log('Error on request to get access token.', $log_context, 'error');
- $message = current_user_can('manage_options') ? 'Error to generate SumUp access token.' : 'Sorry, SumUp is not available. Try again soon.';
-
- return array(
- 'status' => 'error',
- 'message' => $message,
- 'data' => null,
- );
- }
-
- $sumup_settings['sumup_access_token'] = $access_token['access_token'];
- $sumup_settings['sumup_token_fetched_date'] = date('Y/m/d H:i:s');
- update_option('woocommerce_sumup_settings', $sumup_settings);
-
- $sumup_checkout = $order->get_meta('_sumup_checkout_data');
- if ($gateway->should_refresh_checkout_for_order($order, $sumup_checkout)) {
- if (!empty($sumup_checkout['id'])) {
- WC_SUMUP_LOGGER::log('Refreshing stored SumUp checkout in Blocks because the order context changed.', $gateway->get_checkout_log_context($sumup_checkout, $log_context), 'info');
- }
-
- $gateway->clear_checkout_for_order($order);
- $sumup_checkout = array();
- } else {
- $sumup_checkout = $gateway->enrich_checkout_data_for_order(
- $order,
- $sumup_checkout,
- $is_checkout_blocks
- );
- }
-
- if (empty($sumup_checkout)) {
- $checkout_data = $gateway->build_checkout_request_data($order);
- $sumup_checkout = Wc_Sumup_Checkout::create($sumup_settings['sumup_access_token'], $checkout_data, $log_context);
- if (empty($sumup_checkout)) {
- WC_SUMUP_LOGGER::log('Error on request to create SumUp checkout ID.', $log_context, 'error');
- $message = current_user_can('manage_options') ? 'Error to generate SumUp checkout id.' : 'Sorry, SumUp is not available. Try again soon.';
- return array(
- 'status' => 'error',
- 'message' => $message,
- 'data' => null,
- );
- }
- $sumup_checkout = $gateway->enrich_checkout_data_for_order($order, $sumup_checkout, $is_checkout_blocks);
-
- $order->update_meta_data('_sumup_checkout_data', $sumup_checkout);
- $order->save();
- }
-
- /**
- * Fallback to fill merchant code to "old" users. Temporary solution while SumUp team check other ways to enable request to get merchant_code.
- */
- if (empty($sumup_settings['merchant_id']) && isset($sumup_checkout['merchant_code'])) {
- $gateway->update_option('merchant_id', $sumup_checkout['merchant_code']);
- }
-
- if (isset($sumup_checkout['id'])) {
- return array_merge(
- $gateway->get_widget_context_for_order($order),
- array(
- "checkoutId" => $sumup_checkout['id'],
- )
- );
-
- }
-
- if (isset($sumup_checkout['error_code'])) {
- $error = isset($sumup_checkout['error_message']) ? $sumup_checkout['error_message'] : $sumup_checkout['message'];
- WC_SUMUP_LOGGER::log(
- 'SumUp create checkout request failed.',
- array_merge(
- $gateway->get_checkout_log_context($sumup_checkout, $log_context),
- array(
- 'error' => $error,
- 'error_code' => $sumup_checkout['error_code'],
- )
- ),
- 'warning'
- );
- $message = current_user_can('manage_options') ? 'Error from response to create checkout on SumUp. Check the logs.' : 'Sorry, SumUp is not available. Try again soon.';
- return array(
- 'status' => 'error',
- 'message' => $message,
- 'data' => null,
- );
- }
- }
-
- /**
- * Get Sumup active gateway.
- *
- * @param string $gateway
- *
- * @return sumup_Gateway
- * @throws Exception
- */
- protected function get_sumup_gateway($gateway)
- {
- $payment_gateways = WC()->payment_gateways()->get_available_payment_gateways();
-
- if (isset($payment_gateways[$gateway])) {
- return $payment_gateways[$gateway];
- }
-
- throw new Exception('Sumup payment method not found');
- }
-
-}
-
-new Sumup_API_Create_Chekout_Handler();
--- a/sumup-payment-gateway-for-woocommerce/includes/api/handlers/class-sumup-store-payment-instructions.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/api/handlers/class-sumup-store-payment-instructions.php
@@ -1,24 +1,22 @@
<?php
-if (!defined('ABSPATH')) {
+if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
- * API for storing pending PIX/Boleto instructions against a WooCommerce order.
+ * Compatibility endpoint for pending PIX/Boleto widget responses.
*/
-class Sumup_API_Store_Payment_Instructions_Handler extends Sumup_Api_Handler
-{
- public function __construct()
- {
- add_filter('sumup_api_handlers', array($this, 'add_handlers'));
+class Sumup_API_Store_Payment_Instructions_Handler extends Sumup_Api_Handler {
+
+ public function __construct() {
+ add_filter( 'sumup_api_handlers', array( $this, 'add_handlers' ) );
}
- public function add_handlers($handlers)
- {
+ public function add_handlers( $handlers ) {
$handlers['store_payment_instructions'] = array(
- 'callback' => array($this, 'handle'),
- 'method' => 'POST',
+ 'callback' => array( $this, 'handle' ),
+ 'method' => 'POST',
);
return $handlers;
@@ -29,81 +27,80 @@
*
* @return void
*/
- public function handle()
- {
- $json_data = file_get_contents('php://input');
- $post_data = json_decode($json_data, true);
+ public function handle() {
+ $post_data = $this->get_request_data();
- if (!is_array($post_data)) {
- $this->send_response('error', __('Invalid request payload.', 'sumup-payment-gateway-for-woocommerce'), array(), 400);
+ if ( ! is_array( $post_data ) ) {
+ $this->send_response( 'error', __( 'Invalid request payload.', 'sumup-payment-gateway-for-woocommerce' ), array(), 400 );
}
- $nonce = isset($post_data['nonce']) ? sanitize_text_field(wp_unslash($post_data['nonce'])) : '';
- if (!wp_verify_nonce($nonce, 'sumup-store-payment-instructions')) {
- $this->send_response('error', __('Invalid request nonce.', 'sumup-payment-gateway-for-woocommerce'), array(), 403);
+ $nonce = is_string( $post_data['nonce'] ?? null ) ? sanitize_text_field( wp_unslash( $post_data['nonce'] ) ) : '';
+ if ( ! wp_verify_nonce( $nonce, 'sumup-store-payment-instructions' ) ) {
+ $this->send_response( 'error', __( 'Invalid request nonce.', 'sumup-payment-gateway-for-woocommerce' ), array(), 403 );
}
- $order_id = isset($post_data['order_id']) ? absint($post_data['order_id']) : 0;
- if (!$order_id) {
- $this->send_response('error', __('Invalid order ID.', 'sumup-payment-gateway-for-woocommerce'), array(), 400);
+ $order_id = is_scalar( $post_data['order_id'] ?? null ) ? absint( $post_data['order_id'] ) : 0;
+ if ( ! $order_id ) {
+ $this->send_response( 'error', __( 'Invalid order ID.', 'sumup-payment-gateway-for-woocommerce' ), array(), 400 );
}
- $order_key = isset($post_data['order_key']) ? wc_clean(wp_unslash($post_data['order_key'])) : '';
- if (empty($order_key)) {
- $this->send_response('error', __('Invalid order key.', 'sumup-payment-gateway-for-woocommerce'), array(), 400);
+ $order_key = is_string( $post_data['order_key'] ?? null ) ? wc_clean( wp_unslash( $post_data['order_key'] ) ) : '';
+ if ( empty( $order_key ) ) {
+ $this->send_response( 'error', __( 'Invalid order key.', 'sumup-payment-gateway-for-woocommerce' ), array(), 400 );
}
- $order = wc_get_order($order_id);
- if (!$order instanceof WC_Order) {
- $this->send_response('error', __('Order is not available.', 'sumup-payment-gateway-for-woocommerce'), array(), 404);
+ $order = wc_get_order( $order_id );
+ if ( ! $order instanceof WC_Order ) {
+ $this->send_response( 'error', __( 'Order is not available.', 'sumup-payment-gateway-for-woocommerce' ), array(), 404 );
}
- if (!hash_equals($order->get_order_key(), $order_key)) {
- WC_SUMUP_LOGGER::log('Rejected payment instructions storage because order key validation failed. Order ID: ' . $order_id);
- $this->send_response('error', __('Order validation failed.', 'sumup-payment-gateway-for-woocommerce'), array(), 403);
+ if ( ! hash_equals( $order->get_order_key(), $order_key ) ) {
+ WC_SUMUP_LOGGER::log( 'Rejected payment instructions storage because order key validation failed. Order ID: ' . $order_id );
+ $this->send_response( 'error', __( 'Order validation failed.', 'sumup-payment-gateway-for-woocommerce' ), array(), 403 );
}
- if (is_user_logged_in()) {
- $order_customer_id = (int) $order->get_customer_id();
+ if ( is_user_logged_in() ) {
+ $order_customer_id = (int) $order->get_customer_id();
$current_customer_id = get_current_user_id();
- if ($order_customer_id > 0 && $order_customer_id !== $current_customer_id) {
- WC_SUMUP_LOGGER::log('Rejected payment instructions storage because the order belongs to a different customer. Order ID: ' . $order_id);
- $this->send_response('error', __('Order validation failed.', 'sumup-payment-gateway-for-woocommerce'), array(), 403);
+ if ( $order_customer_id > 0 && $order_customer_id !== $current_customer_id ) {
+ WC_SUMUP_LOGGER::log( 'Rejected payment instructions storage because the order belongs to a different customer. Order ID: ' . $order_id );
+ $this->send_response( 'error', __( 'Order validation failed.', 'sumup-payment-gateway-for-woocommerce' ), array(), 403 );
}
}
- $payment_method = isset($post_data['payment_method']) ? sanitize_text_field(wp_unslash($post_data['payment_method'])) : '';
- $instructions = array(
- 'payment_method' => $payment_method,
- 'pix_code' => isset($post_data['pix_code']) ? sanitize_textarea_field(wp_unslash($post_data['pix_code'])) : '',
- 'qr_code_image' => isset($post_data['qr_code_image']) ? esc_url_raw(wp_unslash($post_data['qr_code_image'])) : '',
- 'boleto_download_url' => isset($post_data['boleto_download_url']) ? esc_url_raw(wp_unslash($post_data['boleto_download_url'])) : '',
- 'boleto_barcode' => isset($post_data['boleto_barcode']) ? sanitize_textarea_field(wp_unslash($post_data['boleto_barcode'])) : '',
- );
+ // A webhook may settle the order before pending instructions arrive.
+ if ( $order->is_paid() ) {
+ $this->send_response( 'success', '', array( 'stored' => false ) );
+ }
- $gateway = $this->get_sumup_gateway('sumup');
- $gateway->store_pending_payment_instructions($order, $instructions);
+ $gateway = $this->get_sumup_gateway( 'sumup' );
+ $result = $gateway->verify_pending_payment_widget( $order, $post_data );
+ if ( is_wp_error( $result ) ) {
+ $this->send_response( 'error', $result->get_error_message(), array(), 409 );
+ }
+ $this->send_response( 'success', '', $result );
+ }
- $this->send_response('success', '', array('stored' => true));
+ protected function get_request_data() {
+ return json_decode( file_get_contents( 'php://input' ), true );
}
/**
- * Get SumUp active gateway.
+ * Get the registered SumUp gateway.
*
* @param string $gateway Gateway ID.
* @return WC_Gateway_SumUp
* @throws Exception When the gateway is unavailable.
*/
- protected function get_sumup_gateway($gateway)
- {
- $payment_gateways = WC()->payment_gateways()->get_available_payment_gateways();
+ protected function get_sumup_gateway( $gateway ) {
+ $payment_gateways = WC()->payment_gateways()->payment_gateways();
- if (isset($payment_gateways[$gateway])) {
- return $payment_gateways[$gateway];
+ if ( isset( $payment_gateways[ $gateway ] ) ) {
+ return $payment_gateways[ $gateway ];
}
- throw new Exception('SumUp payment method not found');
+ throw new Exception( 'SumUp payment method not found' );
}
}
--- a/sumup-payment-gateway-for-woocommerce/includes/api/handlers/class-sumup-validate-redirect.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/api/handlers/class-sumup-validate-redirect.php
@@ -1,25 +1,23 @@
<?php
-if (!defined('ABSPATH')) {
+if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Validate SumUp payment status after a redirect (for example 3DS).
*/
-class Sumup_API_Validate_Redirect_Handler extends Sumup_Api_Handler
-{
+class Sumup_API_Validate_Redirect_Handler extends Sumup_Api_Handler {
- public function __construct()
- {
- add_filter('sumup_api_handlers', array($this, 'add_handlers'));
+
+ public function __construct() {
+ add_filter( 'sumup_api_handlers', array( $this, 'add_handlers' ) );
}
- public function add_handlers($handlers)
- {
+ public function add_handlers( $handlers ) {
$handlers['validate_redirect'] = array(
- 'callback' => array($this, 'handle'),
- 'method' => 'POST',
+ 'callback' => array( $this, 'handle' ),
+ 'method' => 'POST',
);
return $handlers;
@@ -28,41 +26,41 @@
/**
* Handle the request.
*/
- public function handle()
- {
- $json_data = file_get_contents('php://input');
- $post_data = json_decode($json_data, true);
+ public function handle() {
+ $json_data = file_get_contents( 'php://input' );
+ $post_data = json_decode( $json_data, true );
- if (!is_array($post_data)) {
- $this->send_response('error', __('Invalid request payload.', 'sumup-payment-gateway-for-woocommerce'), array(), 400);
+ if ( ! is_array( $post_data ) ) {
+ $this->send_response( 'error', __( 'Invalid request payload.', 'sumup-payment-gateway-for-woocommerce' ), array(), 400 );
}
- $nonce = isset($post_data['nonce']) ? sanitize_text_field(wp_unslash($post_data['nonce'])) : '';
- if (!wp_verify_nonce($nonce, 'sumup-validate-redirect')) {
- $this->send_response('error', __('Invalid request nonce.', 'sumup-payment-gateway-for-woocommerce'), array(), 403);
+ $nonce = isset( $post_data['nonce'] ) ? sanitize_text_field( wp_unslash( $post_data['nonce'] ) ) : '';
+ if ( ! wp_verify_nonce( $nonce, 'sumup-validate-redirect' ) ) {
+ $this->send_response( 'error', __( 'Invalid request nonce.', 'sumup-payment-gateway-for-woocommerce' ), array(), 403 );
}
- $order_id = isset($post_data['order_id']) ? absint($post_data['order_id']) : 0;
- if (!$order_id) {
- $this->send_response('error', __('Invalid order ID.', 'sumup-payment-gateway-for-woocommerce'), array(), 400);
+ $order_id = isset( $post_data['order_id'] ) ? absint( $post_data['order_id'] ) : 0;
+ if ( ! $order_id ) {
+ $this->send_response( 'error', __( 'Invalid order ID.', 'sumup-payment-gateway-for-woocommerce' ), array(), 400 );
}
- $checkout_id = isset($post_data['checkout_id']) ? sanitize_text_field(wp_unslash($post_data['checkout_id'])) : '';
+ $checkout_id = isset( $post_data['checkout_id'] ) ? sanitize_text_field( wp_unslash( $post_data['checkout_id'] ) ) : '';
+ $order_key = isset( $post_data['order_key'] ) ? wc_clean( wp_unslash( $post_data['order_key'] ) ) : '';
$gateways = WC()->payment_gateways()->payment_gateways();
- $gateway = isset($gateways['sumup']) ? $gateways['sumup'] : null;
+ $gateway = isset( $gateways['sumup'] ) ? $gateways['sumup'] : null;
- if (!$gateway instanceof WC_Gateway_SumUp) {
- $this->send_response('error', __('SumUp gateway is unavailable.', 'sumup-payment-gateway-for-woocommerce'), array(), 503);
+ if ( ! $gateway instanceof WC_Gateway_SumUp ) {
+ $this->send_response( 'error', __( 'SumUp gateway is unavailable.', 'sumup-payment-gateway-for-woocommerce' ), array(), 503 );
}
- $result = $gateway->resolve_payment_redirect_validation($order_id, $checkout_id, false);
+ $result = $gateway->resolve_payment_redirect_validation( $order_id, $checkout_id, false, $order_key );
- if (!empty($result['error'])) {
- $this->send_response('error', $result['error'], array(), 400);
+ if ( ! empty( $result['error'] ) ) {
+ $this->send_response( 'error', $result['error'], array(), 400 );
}
- $this->send_response('success', $result['message'], $result);
+ $this->send_response( 'success', $result['message'], $result );
}
}
--- a/sumup-payment-gateway-for-woocommerce/includes/api/handlers/class-sumup-validation-website-handler.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/api/handlers/class-sumup-validation-website-handler.php
@@ -1,16 +1,15 @@
<?php
-if (!defined('ABSPATH')) {
+if ( ! defined( 'ABSPATH' ) ) {
exit;
}
//TODO - Adicionar timeout na requisição.
-class Sumup_API_Validation_Website_Handler extends Sumup_Api_Handler
-{
+class Sumup_API_Validation_Website_Handler extends Sumup_Api_Handler {
- public function __construct()
- {
- add_filter('sumup_api_handlers', array($this, 'add_handlers'));
+
+ public function __construct() {
+ add_filter( 'sumup_api_handlers', array( $this, 'add_handlers' ) );
}
/**
@@ -20,11 +19,10 @@
* @throws Exception
*/
- public function add_handlers($handlers)
- {
+ public function add_handlers( $handlers ) {
$handlers['validate_website'] = array(
- 'callback' => array($this, 'handle'),
- 'method' => 'GET',
+ 'callback' => array( $this, 'handle' ),
+ 'method' => 'GET',
);
return $handlers;
@@ -33,13 +31,10 @@
/**
* Handle the request.
*/
- public function handle()
- {
- WC_SUMUP_LOGGER::log( "Sending validate website handler");
- $this->send_response(array('status' => 'valid website'));
-
+ public function handle() {
+ WC_SUMUP_LOGGER::log( 'Sending validate website handler' );
+ $this->send_response( array( 'status' => 'valid website' ) );
}
-
}
new Sumup_API_Validation_Website_Handler();
--- a/sumup-payment-gateway-for-woocommerce/includes/class-wc-sumup-access-token.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/class-wc-sumup-access-token.php
@@ -22,9 +22,8 @@
* @param string $client_secret SumUp client secret.
* @return string
*/
- private static function get_transient_key($client_id = '', $client_secret = '')
- {
- return 'sumup_access_token_' . hash('sha256', $client_id . '|' . $client_secret);
+ private static function get_transient_key( $client_id = '', $client_secret = '' ) {
+ return 'sumup_access_token_' . hash( 'sha256', $client_id . '|' . $client_secret );
}
/**
@@ -33,13 +32,12 @@
* @param mixed $response Decoded SumUp token response.
* @return string
*/
- private static function get_response_error_message($response)
- {
- if (!is_array($response)) {
+ private static function get_response_error_message( $response ) {
+ if ( ! is_array( $response ) ) {
return '';
}
- return isset($response['message']) ? sanitize_text_field($response['message']) : '';
+ return isset( $response['message'] ) ? sanitize_text_field( $response['message'] ) : '';
}
/**
@@ -51,7 +49,7 @@
*
* @return array
*/
- public static function get( $client_id = '', $client_secret = '', $api_key = '', $force=false, $log_context = array() ) {
+ public static function get( $client_id = '', $client_secret = '', $api_key = '', $force = false, $log_context = array() ) {
$context = is_array( $log_context ) ? $log_context : array();
/**
@@ -64,51 +62,51 @@
);
}
- if (empty($client_id)) {
+ if ( empty( $client_id ) ) {
WC_SUMUP_LOGGER::log( 'Error on get access token. Missing client_id', $context, 'error' );
return array();
}
// Try to get the transient for access token.
- $transient_key = self::get_transient_key($client_id, $client_secret);
- $access_token = get_transient($transient_key);
- if($access_token && !$force){
- return array('access_token' => $access_token);
+ $transient_key = self::get_transient_key( $client_id, $client_secret );
+ $access_token = get_transient( $transient_key );
+ if ( $access_token && ! $force ) {
+ return array( 'access_token' => $access_token );
}
$http_response = wp_remote_post(
'https://api.sumup.com/token',
array(
- 'timeout' => self::REQUEST_TIMEOUT,
+ 'timeout' => self::REQUEST_TIMEOUT,
'redirection' => 0,
- 'headers' => sumup_get_api_request_headers(
+ 'headers' => sumup_get_api_request_headers(
array(
'Content-Type' => 'application/x-www-form-urlencoded',
)
),
- 'body' => array(
- 'grant_type' => 'client_credentials',
- 'client_id' => $client_id,
+ 'body' => array(
+ 'grant_type' => 'client_credentials',
+ 'client_id' => $client_id,
'client_secret' => $client_secret,
),
)
);
- if (is_wp_error($http_response)) {
- $context['error'] = sanitize_text_field($http_response->get_error_message());
- WC_SUMUP_LOGGER::log('Error on get access token. Request failed.', $context, 'error');
+ if ( is_wp_error( $http_response ) ) {
+ $context['error'] = sanitize_text_field( $http_response->get_error_message() );
+ WC_SUMUP_LOGGER::log( 'Error on get access token. Request failed.', $context, 'error' );
return array();
}
- $response_http_code = wp_remote_retrieve_response_code($http_response);
- $response_body = wp_remote_retrieve_body($http_response);
- $response = json_decode( $response_body, true );
- if (is_array($response)) {
- if ($response_http_code === 200 && !empty($response['access_token'])) {
+ $response_http_code = wp_remote_retrieve_response_code( $http_response );
+ $response_body = wp_remote_retrieve_body( $http_response );
+ $response = json_decode( $response_body, true );
+ if ( is_array( $response ) ) {
+ if ( $response_http_code === 200 && ! empty( $response['access_token'] ) ) {
set_transient(
$transient_key,
$response['access_token'],
- !empty($response['expires_in']) ? (int) $response['expires_in'] : HOUR_IN_SECONDS
+ ! empty( $response['expires_in'] ) ? (int) $response['expires_in'] : HOUR_IN_SECONDS
);
return $response;
@@ -120,14 +118,13 @@
$context,
array(
'http_code' => $response_http_code,
- 'error' => self::get_response_error_message($response),
+ 'error' => self::get_response_error_message( $response ),
)
),
'warning'
);
}
-
return array();
}
}
--- a/sumup-payment-gateway-for-woocommerce/includes/class-wc-sumup-apple-pay.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/class-wc-sumup-apple-pay.php
@@ -0,0 +1,309 @@
+<?php
+/**
+ * Apple Pay association and domain registration helpers.
+ *
+ * @package SumUp_WooCommerce
+ */
+
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+/**
+ * Apple Pay domain association and registration helpers.
+ */
+class Wc_Sumup_Apple_Pay {
+ /**
+ * Request timeout in seconds for SumUp domain registration requests.
+ */
+ private const REQUEST_TIMEOUT = 30;
+
+ /**
+ * Canonical association file name shipped with the plugin.
+ */
+ private const ASSOCIATION_FILE_NAME = 'apple-developer-merchantid-domain-association.txt';
+
+ /**
+ * Apple Pay domain registration endpoint.
+ */
+ private const REGISTRATION_ENDPOINT = 'https://api.sumup.com/v0.1/wallet-keys/merchants/%s/domains';
+
+ /**
+ * Initialise the request hook that serves the association file.
+ *
+ * @return void
+ */
+ public static function init() {
+ add_action( 'template_redirect', array( __CLASS__, 'maybe_serve_association_file' ), 0 );
+ }
+
+ /**
+ * Determine whether Apple Pay should be offered in the payment widget.
+ *
+ * @param array|null $settings Optional gateway settings.
+ * @return bool
+ */
+ public static function should_offer_apple_pay( $settings = null ) {
+ if ( ! is_array( $settings ) ) {
+ $settings = sumup_get_gateway_settings();
+ }
+
+ return isset( $settings['enable_apple_pay_domain_registration'] ) && 'yes' === $settings['enable_apple_pay_domain_registration'];
+ }
+
+ /**
+ * Get the bundled association file path.
+ *
+ * @return string
+ */
+ public static function get_association_file_path() {
+ $path_candidates = apply_filters(
+ 'sumup_apple_pay_association_file_paths',
+ array(
+ WC_SUMUP_PLUGIN_PATH . '/' . self::ASSOCIATION_FILE_NAME,
+ WC_SUMUP_PLUGIN_PATH . '/assets/' . self::ASSOCIATION_FILE_NAME,
+ )
+ );
+
+ foreach ( (array) $path_candidates as $candidate ) {
+ $candidate = is_string( $candidate ) ? $candidate : '';
+
+ if ( '' === $candidate ) {
+ continue;
+ }
+
+ if ( is_readable( $candidate ) && is_file( $candidate ) ) {
+ return $candidate;
+ }
+ }
+
+ return '';
+ }
+
+ /**
+ * Read the contents of the association file.
+ *
+ * @return string
+ */
+ public static function get_association_file_contents() {
+ $file_path = self::get_association_file_path();
+ if ( '' === $file_path ) {
+ return '';
+ }
+
+ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Read the bundled local file verbatim.
+ $contents = file_get_contents( $file_path );
+ if ( false === $contents ) {
+ return '';
+ }
+
+ return (string) $contents;
+ }
+
+ /**
+ * Check whether the current request targets the well-known association path.
+ *
+ * @param string|null $request_path Optional path override for tests.
+ * @return bool
+ */
+ public static function is_association_request( $request_path = null ) {
+ if ( null === $request_path ) {
+ // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- The URI is parsed and matched against fixed association-file paths.
+ $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '';
+ $request_path = wp_parse_url( $request_uri, PHP_URL_PATH );
+ }
+
+ $request_path = is_string( $request_path ) ? untrailingslashit( $request_path ) : '';
+
+ return in_array(
+ $request_path,
+ array(
+ '/.well-known/apple-developer-merchantid-domain-association',
+ '/.well-known/apple-developer-merchantid-domain-association.dms',
+ ),
+ true
+ );
+ }
+
+ /**
+ * Determine whether the current request should serve the association file.
+ *
+ * @param string|null $request_path Optional request path override.
+ * @return bool
+ */
+ public static function should_serve_association_file( $request_path = null ) {
+ return self::is_association_request( $request_path );
+ }
+
+ /**
+ * Serve the association file when the well-known path is requested.
+ *
+ * @return void
+ */
+ public static function maybe_serve_association_file() {
+ if ( ! self::should_serve_association_file() ) {
+ return;
+ }
+
+ $contents = self::get_association_file_contents();
+ if ( '' === $contents ) {
+ WC_SUMUP_LOGGER::log(
+ __( 'Apple Pay association file is missing or unreadable.', 'sumup-payment-gateway-for-woocommerce' ),
+ array(
+ 'flow' => 'apple_pay',
+ ),
+ 'warning'
+ );
+
+ status_header( 404 );
+ nocache_headers();
+ exit;
+ }
+
+ nocache_headers();
+ status_header( 200 );
+ header( 'Content-Type: text/plain; charset=us-ascii' );
+ header( 'Content-Length: ' . strlen( $contents ) );
+ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- The file must be served verbatim for Apple Pay verification.
+ echo $contents;
+ exit;
+ }
+
+ /**
+ * Register the current site domain with SumUp for Apple Pay.
+ *
+ * @param array $log_context Optional log context.
+ * @return bool
+ */
+ public static function register_domain( $log_context = array() ) {
+ if ( ! self::should_offer_apple_pay() ) {
+ return false;
+ }
+
+ $settings = sumup_maybe_migrate_merchant_code( sumup_get_gateway_settings() );
+ $merchant_code = sanitize_text_field( (string) ( $settings['merchant_id'] ?? '' ) );
+ if ( '' === $merchant_code ) {
+ WC_SUMUP_LOGGER::log(
+ __( 'Skipping Apple Pay domain registration because merchant code is missing.', 'sumup-payment-gateway-for-woocommerce' ),
+ array_merge(
+ is_array( $log_context ) ? $log_context : array(),
+ array(
+ 'flow' => 'apple_pay',
+ )
+ ),
+ 'warning'
+ );
+ return false;
+ }
+
+ $domain = sumup_get_site_hostname();
+ if ( '' === $domain || ! sumup_is_public_hostname( $domain ) ) {
+ WC_SUMUP_LOGGER::log(
+ __( 'Skipping Apple Pay domain registration because the store hostname is not public.', 'sumup-payment-gateway-for-woocommerce' ),
+ array_merge(
+ is_array( $log_context ) ? $log_context : array(),
+ array(
+ 'flow' => 'apple_pay',
+ 'domain' => $domain,
+ )
+ ),
+ 'warning'
+ );
+ return false;
+ }
+
+ if ( '' === self::get_association_file_contents() ) {
+ WC_SUMUP_LOGGER::log(
+ __( 'Skipping Apple Pay domain registration because the association file is missing.', 'sumup-payment-gateway-for-woocommerce' ),
+ array_merge(
+ is_array( $log_context ) ? $log_context : array(),
+ array(
+ 'flow' => 'apple_pay',
+ 'domain' => $domain,
+ )
+ ),
+ 'warning'
+ );
+ return false;
+ }
+
+ $access_token_data = Wc_Sumup_Access_Token::get(
+ $settings['client_id'] ?? '',
+ $settings['client_secret'] ?? '',
+ $settings['api_key'] ?? '',
+ true,
+ is_array( $log_context ) ? $log_context : array()
+ );
+ $access_token = isset( $access_token_data['access_token'] ) ? sanitize_text_field( (string) $access_token_data['access_token'] ) : '';
+ if ( '' === $access_token ) {
+ WC_SUMUP_LOGGER::log(
+ __( 'Skipping Apple Pay domain registration because the access token could not be resolved.', 'sumup-payment-gateway-for-woocommerce' ),
+ array_merge(
+ is_array( $log_context ) ? $log_context : array(),
+ array(
+ 'flow' => 'apple_pay',
+ 'domain' => $domain,
+ 'merchant_code' => $merchant_code,
+ )
+ ),
+ 'warning'
+ );
+ return false;
+ }
+
+ $request_body = wp_json_encode(
+ array(
+ 'domain' => $domain,
+ 'walletType' => 'APPLE_PAY',
+ )
+ );
+
+ $response = wp_remote_post(
+ sprintf( self::REGISTRATION_ENDPOINT, rawurlencode( $merchant_code ) ),
+ array(
+ 'timeout' => self::REQUEST_TIMEOUT,
+ 'redirection' => 0,
+ 'headers' => sumup_get_api_request_headers(
+ array(
+ 'Content-Type' => 'application/json',
+ 'Idempotency-Key' => wp_generate_uuid4(),
+ ),
+ $access_token
+ ),
+ 'body' => $request_body,
+ )
+ );
+
+ $context = array_merge(
+ is_array( $log_context ) ? $log_context : array(),
+ array(
+ 'flow' => 'apple_pay',
+ 'domain' => $domain,
+ 'merchant_code' => $merchant_code,
+ )
+ );
+
+ if ( is_wp_error( $response ) ) {
+ $context['error'] = sanitize_text_field( $response->get_error_message() );
+ WC_SUMUP_LOGGER::log( __( 'Apple Pay domain registration request failed.', 'sumup-payment-gateway-for-woocommerce' ), $context, 'warning' );
+ return false;
+ }
+
+ $response_code = wp_remote_retrieve_response_code( $response );
+ $response_body = wp_remote_retrieve_body( $response );
+ $response_data = json_decode( $response_body, true );
+
+ if ( $response_code >= 200 && $response_code < 300 ) {
+ WC_SUMUP_LOGGER::log( __( 'Apple Pay domain registration completed.', 'sumup-payment-gateway-for-woocommerce' ), $context, 'info' );
+ return true;
+ }
+
+ $context['http_code'] = $response_code;
+ $context['error'] = is_array( $response_data )
+ ? sanitize_text_field( (string) ( $response_data['message'] ?? $response_data['detail'] ?? '' ) )
+ : '';
+ WC_SUMUP_LOGGER::log( __( 'Apple Pay domain registration returned a non-success response.', 'sumup-payment-gateway-for-woocommerce' ), $context, 'warning' );
+
+ return false;
+ }
+}
--- a/sumup-payment-gateway-for-woocommerce/includes/class-wc-sumup-block-gateway.php
+++ b/sumup-payment-gateway-for-woocommerce/includes/class-wc-sumup-block-gateway.php
@@ -1,129 +1,31 @@
<?php
use AutomatticWooCommerceBlocksPaymentsIntegrationsAbstractPaymentMethodType;
-final class WC_Sumup_Blocks_Support extends AbstractPaymentMethodType
-{
+final class WC_Sumup_Blocks_Support extends AbstractPaymentMethodType {
+
private $gateway;
protected $name = 'sumup'; // payment gateway id
- public function initialize()
- {
+ public function initialize() {
// get payment gateway settings
- $this->settings = get_option("woocommerce_{$this->name}_settings", array());
+ $this->settings = get_option( "woocommerce_{$this->name}_settings", array() );
// you can also initialize your payment gateway here
- $gateways = WC()->payment_gateways->payment_gateways();
- $this->gateway = $gateways[$this->name];
-
-
+ $gateways = WC()->payment_gateways->payment_gateways();
+ $this->gateway = $gateways[ $this->name ];
}
- public function is_active()
- {
- return ! empty($this->settings['enabled'])
+ public function is_active() {
+ return ! empty( $this->settings['enabled'] )
&& 'yes' === $this->settings['enabled']
- && sumup_gateway_is_configured($this->settings);
+ && sumup_gateway_is_configured( $this->settings );
}
- public function get_payment_method_script_handles()
- {
-
- $shop_base_country = WC()->countries->get_base_country();
- $supported_countries = array(
- "AT",
- "AU",
- "BE",
- "BG",
- "BR",
- "CH",
- "CL",
- "CO",
- "CY",
- "CZ",
- "DE",
- "DK",
- "EE",
- "ES",
- "FI",
- "FR",
- "GB",
- "GR",
- "HR",
- "HU",
- "IE",
- "IT",
- "LT",
- "LU",
- "LV",
- "MT",
- "NL",
- "NO",
- "PL",
- "PT",
- "RO",
- "SE",
- "SI",
- "SK",
- "US",
- );
- $card_country = in_array($shop_base_country, $supported_countries) ? $shop_base_country : 'null';
-
- $show_zipcode = $card_country === 'US' ? 'true' : 'false';
-
- $card_locale = str_replace('_', '-', get_locale());
- $card_supported_locales = array(
- "bg-BG",
- "cs-CZ",
- "da-DK",
- "de-AT",
- "de-CH",
- "de-DE",
- "de-LU",
- "el-CY",
- "el-GR",
- "en-AU",
- "en-GB",
- "en-IE",
- "en-MT",
- "en-US",
- "es-CL",
- "es-ES",
- "et-EE",
- "fi-FI",
- "fr-BE",
- "fr-CH",
- "fr-FR",
- "fr-LU",
- "hu-HU",
- "it-CH",
- "it-IT",
- "lt-LT",
- "lv-LV",
- "nb-NO",
- "nl-BE",
- "nl-NL",
- "pt-BR",
- "pt-PT",
- "pl-PL",
- "sk-SK",
- "sl-SI",
- "sv-SE",
- );
- $card_locale = in_array($card_locale, $card_supported_locales) ? $card_locale : 'en-GB';
-
- /**
- * Translators: the following error messages are shown to the end user
- */
- $show_installments = 'false';
- $number_of_installments = null;
- if ($card_country === "BR") {
- $show_installments = isset($this->settings["enable_installments"]) && $this->settings["enable_installments"] === "yes" ? 'true' : 'false';
- $number_of_installments = isset($this->settings["number_of_installments"]) && $this->settings["number_of_installments"] !== false && $this->settings["number_of_installments"] !== 'select' ? $this->settings["number_of_installments"] : null;
- }
+ public function get_payment_method_script_handles() {
- if (is_cart() || is_checkout()) {
+ if ( is_cart() || is_checkout() ) {
$asset_data = sumup_get_build_asset_metadata(
'build/index.asset.php',
array(
@@ -139,71 +41,25 @@
* Use the SumUp's SDK for accepting card payments.
* Documentation can be found at https://developer.sumup.com/docs/widgets-card
*/
- wp_enqueue_script('sumup_gateway_card_sdk', 'https://gateway.sumup.com/gateway/ecom/card/v2/sdk.js', array(), WC_SUMUP_VERSION, false);
+ wp_enqueue_script( 'sumup_gateway_card_sdk', 'https://gateway.sumup.com/gateway/ecom/card/v2/sdk.js', array(), WC_SUMUP_VERSION, false );
wp_register_script(
'wc-sumup-blocks-integration',
- plugin_dir_url(__DIR__) . 'build/index.js',
+ plugin_dir_url( __DIR__ ) . 'build/index.js',
$asset_data['dependencies'],
$asset_data['version'],
true
);
- wp_register_style("wc_sumup_checkout", plugin_dir_url(__DIR__) . 'build/index.css', array( 'wp-components' ), $asset_data['version']);
- wp_enqueue_style('wp-components');
- wp_enqueue_style('wc_sumup_checkout');
+ wp_register_style( 'wc_sumup_checkout', plugin_dir_url( __DIR__ ) . 'build/index.css', array( 'wp-components' ), $asset_data['version'] );
+ wp_enqueue_style( 'wp-components' );
+ wp_enqueue_style( 'wc_sumup_checkout' );
}
- /**
- * Translators: the following error messages are shown to the end user
- */
- $error_general = __('Transaction was unsuccessful. Please check the minimum amount or use another valid card.', 'sumup-payment-gateway-for-woocommerce');
- $error_invalid_form = __('Fill in all required details.', 'sumup-payment-gateway-for-woocommerce');
- $error_instructions = __('Unable to save payment instructions. Please try again.', 'sumup-payment-gateway-for-woocommerce');
- $error_payment_failed = __('Payment failed, please try again.', 'sumup-payment-gateway-for-woocommerce');
- $error_payment_pending = __('We are still waiting for SumUp to confirm this payment. Please wait a moment and refresh the page if the order is not updated automatically.', 'sumup-payment-gateway-for-woocommerce');
- $error_payment_cancelled = __('Payment was cancelled.', 'sumup-payment-gateway-for-woocommerce');
- $error_checkout_init = __('Unable to initialize SumUp checkout.', 'sumup-payment-gateway-for-woocommerce');
-
- wp_localize_script('wc-sumup-blocks-integration', 'sumup_gateway_params', array(
-
- 'showInstallmen