woocommerce / woocommerce-gateway-paypal-express-checkout

58 stars 65 forks source link

Allow setting the paypal environment via a filter. #766

Closed justlevine closed 4 years ago

justlevine commented 4 years ago

Very helpful if you have a staging environment (i.e. the entire point of a sandbox feature), and dont want to keep manually changing the environment via wp-admin every time you do a database merge.

Example

add_filter( 'woocommerce_paypal_express_checkout_set_express_checkout_environment', function ( $environment ) {
     $environment = defined( 'WP_ENV') && 'production === 'WP_ENV' ? 'live' : 'sandbox;
    return $environment;
}

Note

I explicitly didn't add a filter for $this->_credential, as someone more qualified than myself should consider the security implications (e.g. a malicious plugin being able to hijack the gateway).

james-allan commented 4 years ago

Hey @justlevine, thanks for submitting the PR.

After reviewing the proposed changes I don't think that filter will work for what you want. This specific set_environment() function is only used in 2 cases to verify the credentials are valid. It's not used, as far as I can tell, to set the environment for processing transactions etc.

If you would like to filter the environment across the whole site, following the logic from the snippet above, I'd suggest using something like this:

add_filter( 'option_woocommerce_ppec_paypal_settings', function( $settings ) {
    $settings['environment'] = defined( 'WP_ENV') && 'production' === WP_ENV ? 'live' : 'sandbox';
    return $settings;
} );