1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
<?php
defined( 'ABSPATH' ) || exit();
class WC_Stripe_Controller_Gateway_Settings extends WC_Stripe_Rest_Controller {
protected $namespace = 'gateway-settings';
public function register_routes() {
register_rest_route(
$this->rest_uri(), 'apple-domain', array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'register_apple_domain' ),
'permission_callback' => array( $this, 'shop_manager_permission_check' )
)
);
register_rest_route( $this->rest_uri(), 'create-webhook',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_webhook' ),
'permission_callback' => array( $this, 'shop_manager_permission_check' )
)
);
register_rest_route( $this->rest_uri(), 'delete-webhook', array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'delete_webhook' ),
'permission_callback' => array( $this, 'shop_manager_permission_check' )
) );
register_rest_route( $this->rest_uri(), 'connection-test',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'connection_test' ),
'permission_callback' => array( $this, 'shop_manager_permission_check' )
)
);
}
public function register_apple_domain( $request ) {
$gateway = WC_Stripe_Gateway::load();
if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
$path = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . '.well-known';
$file = $path . DIRECTORY_SEPARATOR . 'apple-developer-merchantid-domain-association';
if ( ! file_exists( $file ) ) {
require_once( ABSPATH . '/wp-admin/includes/file.php' );
if ( function_exists( 'WP_Filesystem' ) && ( WP_Filesystem() ) ) {
global $wp_filesystem;
if ( ! $wp_filesystem->is_dir( $path ) ) {
$wp_filesystem->mkdir( $path );
}
$contents = $wp_filesystem->get_contents( WC_STRIPE_PLUGIN_FILE_PATH . 'apple-developer-merchantid-domain-association' );
$wp_filesystem->put_contents( $file, $contents, 0755 );
}
}
}
$server_name = ! empty( $request['hostname'] ) ? $request['hostname'] : $_SERVER['SERVER_NAME'];
$server_name = apply_filters( 'wc_stripe_apple_pay_domain', $server_name );
if ( strstr( $server_name, 'www.' ) ) {
$server_name_2 = str_replace( 'www.', '', $server_name );
} else {
$server_name_2 = 'www.' . $server_name;
}
$domains = array( $server_name, $server_name_2 );
try {
$api_key = wc_stripe_get_secret_key( 'live' );
if ( empty( $api_key ) ) {
throw new Exception( __( 'You cannot register your domain until you have completed the Connect process on the API Settings page. A registered domain is not required when test mode is enabled.',
'woo-stripe-payment' ) );
}
$registered_domains = $gateway->applePayDomains->mode( 'live' )->all( array( 'limit' => 50 ) );
if ( ! is_wp_error( $registered_domains ) && $registered_domains ) {
foreach ( $registered_domains->data as $domain ) {
if ( in_array( $domain->domain_name, $domains ) ) {
$gateway->applePayDomains->mode( 'live' )->delete( $domain->id );
}
}
}
$failures = 0;
foreach ( $domains as $domain ) {
$result = $gateway->applePayDomains->mode( 'live' )->create( array( 'domain_name' => $domain ) );
if ( is_wp_error( $result ) ) {
$failures ++;
if ( $failures > 1 ) {
throw new Exception( $result->get_error_message() );
}
}
}
}
catch ( Exception $e ) {
return new WP_Error( 'domain-error', $e->getMessage(), array( 'status' => 200 ) );
}
return rest_ensure_response(
array(
'message' => sprintf(
__(
'Domain registered successfully. You can confirm in your Stripe Dashboard at https://dashboard.stripe.com/account/apple_pay.',
'woo-stripe-payment'
)
),
)
);
}
public function create_webhook( $request ) {
$url = stripe_wc()->rest_api->webhook->rest_url( 'webhook' );
$api_settings = stripe_wc()->api_settings;
$env = $request->get_param( 'environment' );
$gateway = WC_Stripe_Gateway::load( $env );
$events = array();
$secret_key = wc_stripe_get_secret_key( $env );
if ( empty( $secret_key ) ) {
return new WP_Error( 'webhook-error', __( 'You must configure your secret key before creating webhooks.', 'woo-stripe-payment' ),
array(
'status' => 200,
)
);
}
$webhooks = $gateway->webhookEndpoints->all( array( 'limit' => 100 ) );
if ( ! is_wp_error( $webhooks ) ) {
foreach ( $webhooks->data as $webhook ) {
if ( $webhook->url === $url ) {
if ( ! $api_settings->get_option( "webhook_secret_{$env}", null ) ) {
$events = $webhook->enabled_events;
$gateway->webhookEndpoints->delete( $webhook->id );
$api_settings->delete_webhook_settings( $env );
} else {
return new WP_Error( 'webhook-error',
__( 'There is already a webhook configured for this site. If you want to delete the webhook, login to your Stripe Dashboard.', 'woo-stripe-payment' ),
array(
'status' => 200,
)
);
}
}
}
}
$webhook = $api_settings->create_webhook( $env, $events );
if ( is_wp_error( $webhook ) ) {
return new WP_Error( $webhook->get_error_code(), $webhook->get_error_message(), array( 'status' => 200 ) );
} else {
return rest_ensure_response(
array(
'message' => sprintf(
__( 'Webhook created in Stripe for %s environment. You can test your webhook by logging in to the Stripe dashboard',
'woo-stripe-payment' ),
'live' ==
$env ? __( 'Live', 'woo-stripe-payment' ) : __( 'Test', 'woo-stripe-payment' )
),
'secret' => $webhook['secret'],
)
);
}
}
public function delete_webhook( $request ) {
$api_settings = stripe_wc()->api_settings;
$mode = $request['mode'];
$webhook_id = $api_settings->get_webhook_id( $mode );
if ( $webhook_id ) {
$client = WC_Stripe_Gateway::load( $mode );
$result = $client->webhookEndpoints->delete( $webhook_id );
$api_settings->delete_webhook_settings( $mode );
if ( is_wp_error( $result ) ) {
return new WP_Error( $result->get_error_code(), $result->get_error_message(), array( 'status' => 200 ) );
}
}
return rest_ensure_response( array( 'success' => true ) );
}
public function connection_test( $request ) {
$mode = $request->get_param( 'mode' );
$settings = stripe_wc()->api_settings;
$api_keys = null;
ob_start();
try {
if ( $mode === 'test' ) {
$api_keys = array( $request->get_param( 'secret_key' ), $request->get_param( 'publishable_key' ) );
if ( in_array( null, $api_keys ) ) {
throw new Exception( sprintf( __( 'You must enter your API keys or connect the plugin before performing a connection test.',
'woo-stripe-payment' ) ) );
}
$settings->settings['publishable_key_test'] = $settings->validate_text_field( 'publishable_key_test',
$request->get_param( 'publishable_key' ) );
$settings->settings['secret_key_test'] = $settings->validate_password_field( 'secret_key_test',
$request->get_param( 'secret_key' ) );
}
$response = WC_Stripe_Gateway::load()->customers->mode( $mode )->all( array( 'limit' => 1 ) );
if ( is_wp_error( $response ) ) {
throw new Exception( sprintf( __( 'Mode: %s. Invalid secret key. Please check your entry.', 'woo-stripe-payment' ), $mode ) );
}
$response = wp_remote_post(
'https://api.stripe.com/v1/payment_methods',
array(
'headers' => array( 'Content-Type' => 'application/x-www-form-urlencoded' ),
'body' => array(
'key' => wc_stripe_get_publishable_key( $mode ),
'type' => 'card',
'card' => array(
'number' => '4242424242424242',
'exp_month' => 12,
'exp_year' => 2030,
'cvc' => 314
),
'metadata' => array(
'origin' => 'API Settings connection test'
)
),
)
);
if ( is_wp_error( $response ) ) {
throw new Exception( sprintf( __( 'Mode: %s. Invalid publishable key. Please check your entry.', 'woo-stripe-payment' ), $mode ) );
}
if ( $response['response']['code'] == 401 ) {
throw new Exception( sprintf( __( 'Mode: %s. Invalid publishable key. Please check your entry.', 'woo-stripe-payment' ), $mode ) );
}
if ( $api_keys ) {
update_option( $settings->get_option_key(), $settings->settings, 'yes' );
}
ob_get_clean();
}
catch ( Exception $e ) {
return new WP_Error( 'connection-failure', $e->getMessage(), array( 'status' => 200 ) );
}
return rest_ensure_response( array(
'message' => sprintf( __( 'Connection test to Stripe was successful. Mode: %s.', 'woo-stripe-payment' ), $mode )
) );
}
public function shop_manager_permission_check() {
if ( current_user_can( 'manage_woocommerce' ) ) {
return true;
}
return new WP_Error( 'permission-error', __( 'You do not have permissions to access this resource.', 'woo-stripe-payment' ),
array( 'status' => 403 ) );
}
}