Last active
August 24, 2020 15:30
-
-
Save kdctek/c00214cdc9ae499137f8a16cbfa5c7f8 to your computer and use it in GitHub Desktop.
WooCommerce Stripe Gateway - India FIX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooCommerce Stripe Gateway - India FIX | |
* Plugin URI: https://github.com/woocommerce/woocommerce-gateway-stripe/issues/1284 | |
* Description: Stripe India, requires to pass some addtional parameters. [Read Here](https://stripe.com/docs/india-exports) | |
* Author: KDC | |
* Author URI: https://kdc.in/ | |
* Version: 1.0.1 | |
* Requires at least: 4.4 | |
* Tested up to: 5.5 | |
* WC requires at least: 3.0 | |
* WC tested up to: 4.3 | |
* Text Domain: woocommerce-gateway-stripe-in | |
* | |
*/ | |
/** | |
* Avoid direct access to file | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
/** | |
* Check if 'WooCommerce Stripe Gateway' is installed & active | |
*/ | |
if ( ! is_plugin_active( 'woocommerce-gateway-stripe/woocommerce-gateway-stripe.php' ) ) { | |
echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'Stripe - India FIX requires WooCommerce Stripe Gateway to be installed and active. You can download %s here.', 'woocommerce-gateway-stripe-in' ), '<a href="https://wordpress.org/plugins/woocommerce-gateway-stripe/" target="_blank">WP Plugin</a>' ) . '</strong></p></div>'; | |
exit; | |
} | |
/** | |
* Enable support to Store Country as IN | |
* | |
* @since 1.0.0 | |
* @see https://gist.github.com/tommyshellberg/6e1d1ddae944f3cbdd4b0aad87844b13 | |
* @update https://wordpress.org/support/topic/after-the-latest-update-stripe-is-not-working-on-my-website/#post-13251503 | |
*/ | |
if( ! function_exists( 'kdc_wc_stripe_in_add_supported_country' ) ) { | |
function kdc_wc_stripe_in_add_supported_country( $countries ) { | |
if ( ! in_array( 'IN', $countries ) ) { | |
$countries[] = 'IN'; | |
} | |
return $countries; | |
} | |
add_filter( 'wc_stripe_supported_countries', 'kdc_wc_stripe_in_add_supported_country', 10, 2 ); | |
} | |
/** | |
* Add Post Data to Stripe's PaymentIntent | |
* | |
* @since 1.0.0 | |
*/ | |
if( ! function_exists( 'kdc_wc_stripe_in_add_post_data' ) ) { | |
function kdc_wc_stripe_in_add_post_data( $post_data, $order ) { | |
/** | |
* Check if the cart has only Virtual &/or Downloadable Items, | |
* Accordingly mark as `Export of Goods` | |
* | |
* @see https://stripe.com/docs/india-exports#export-of-goods | |
* @credit https://wordpress.org/support/topic/skip-shipping-for-virtual-variable-products/#post-10125403 | |
*/ | |
// By default, virtual/downloadable product only | |
$kdc_virtual_downloadable_products_only = true; | |
// Get all products in cart | |
$products = WC()->cart->get_cart(); | |
// Loop through cart products | |
foreach( $products as $product ) { | |
// is variation downloadable | |
$is_downloadable = $product['data']->downloadable; | |
// is variation virtual | |
$is_virtual = $product['data']->virtual ; | |
// Update $virtual_downloadable_products_only if product is not virtual or downloadable and exit loop | |
if( 'no' == $is_virtual && 'no' == $is_downloadable ) { | |
$kdc_virtual_downloadable_products_only = false; | |
break; | |
} | |
} | |
if( false === $kdc_virtual_downloadable_products_only ) { | |
/** | |
* Add Shipping Info | |
* | |
* @see https://github.com/woocommerce/woocommerce-gateway-stripe/issues/1266#issuecomment-667976567 | |
* @credit marcinbot | |
*/ | |
if ( ! empty( $order->get_shipping_postcode() ) ) { | |
$post_data['shipping'] = array( | |
'name' => $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), | |
'address' => array( | |
'line1' => $order->get_shipping_address_1(), | |
'line2' => $order->get_shipping_address_2(), | |
'city' => $order->get_shipping_city(), | |
'country' => $order->get_shipping_country(), | |
'postal_code' => $order->get_shipping_postcode(), | |
'state' => $order->get_shipping_state(), | |
) | |
); | |
} | |
} | |
return $post_data; | |
} | |
add_filter( 'wc_stripe_generate_create_intent_request', 'kdc_wc_stripe_in_add_post_data', 10, 2 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the Supported Country, Now it verifies if the country name does not exist, only then it adds India.
@see https://wordpress.org/support/topic/after-the-latest-update-stripe-is-not-working-on-my-website/#post-13251503