Advertisement
syednazrulhassan

Stripe Woo Addon Customers charge

Feb 29th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 26.43 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Plugin Name: Stripe WooCommerce Addon
  4.  * Plugin URI: https://wordpress.org/plugins/stripe-woocommerce-addon/
  5.  * Description: This plugin adds a payment option in WooCommerce for customers to pay with their Credit Cards Via Stripe.
  6.  * Version: 1.0.6
  7.  * Author: Syed Nazrul Hassan
  8.  * Author URI: https://nazrulhassan.wordpress.com/
  9.  * Author Email : nazrulhassanmca@gmail.com
  10.  *
  11.  * License: GNU General Public License v3.0
  12.  * License URI: http://www.gnu.org/licenses/gpl-3.0.html
  13.  *
  14.  * PCI Compliance by: Conner Imrie (https://github.com/cimrie) and Stephen Zuniga (https://github.com/stezu/)
  15.  */
  16.  
  17. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  18. function stripe_init()
  19. {
  20.  
  21.     if(!class_exists('Stripe'))
  22.     {
  23.         include(plugin_dir_path( __FILE__ )."lib/Stripe.php");
  24.     }
  25.     function add_stripe_gateway_class( $methods )
  26.     {
  27.         $methods[] = 'WC_Stripe_Gateway';
  28.         return $methods;
  29.     }
  30.     add_filter( 'woocommerce_payment_gateways', 'add_stripe_gateway_class' );
  31.  
  32.     if(class_exists('WC_Payment_Gateway'))
  33.     {
  34.         class WC_Stripe_Gateway extends WC_Payment_Gateway
  35.         {
  36.  
  37.             public function __construct()
  38.             {
  39.  
  40.                 $this->id               = 'stripe';
  41.                 $this->icon             = plugins_url( 'images/stripe.png' , __FILE__ ) ;
  42.                 $this->has_fields       = true;
  43.                 $this->method_title     = 'Stripe Cards Settings';
  44.                 $this->init_form_fields();
  45.                 $this->init_settings();
  46.  
  47.                 $this->supports                 = array( 'default_credit_card_form','products','refunds');
  48.  
  49.                 $this->title                    = $this->get_option( 'stripe_title' );
  50.                 $this->stripe_description       = $this->get_option( 'stripe_description');
  51.                 $this->stripe_testpublickey     = $this->get_option( 'stripe_testpublickey' );
  52.                 $this->stripe_testsecretkey     = $this->get_option( 'stripe_testsecretkey' );
  53.                 $this->stripe_livepublickey     = $this->get_option( 'stripe_livepublickey' );
  54.                 $this->stripe_livesecretkey     = $this->get_option( 'stripe_livesecretkey' );
  55.                 $this->stripe_storecurrency     = $this->get_option( 'stripe_storecurrency' );
  56.                 $this->stripe_sandbox           = $this->get_option( 'stripe_sandbox' );
  57.                 $this->stripe_authorize_only    = $this->get_option( 'stripe_authorize_only' );
  58.                 $this->stripe_cardtypes         = $this->get_option( 'stripe_cardtypes');
  59.                 $this->stripe_enable_for_methods= $this->get_option( 'stripe_enable_for_methods', array() );
  60.                 $this->stripe_meta_cartspan     = $this->get_option( 'stripe_meta_cartspan');
  61.  
  62.                 $this->stripe_zerodecimalcurrency      = array("BIF","CLP","DJF","GNF","JPY","KMF","KRW","MGA","PYG","RWF","VND","VUV","XAF","XOF","XPF");
  63.  
  64.  
  65.                 if(!defined("STRIPE_TRANSACTION_MODE"))
  66.                     { define("STRIPE_TRANSACTION_MODE"  , ($this->stripe_authorize_only =='yes'? false : true)); }
  67.  
  68.                 add_action( 'wp_enqueue_scripts', array( $this, 'load_stripe_scripts' ) );
  69.  
  70.  
  71.                 if('yes'  == $this->stripe_sandbox  )
  72.                     { Stripe::setApiKey($this->stripe_testsecretkey);  }
  73.                 else
  74.                     { Stripe::setApiKey($this->stripe_livesecretkey);  }
  75.  
  76.                 if (is_admin())
  77.                 {
  78.                     add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
  79.                 }
  80.  
  81.                 add_action('admin_notices' , array($this, 'do_ssl_check'    ));
  82.             }
  83.  
  84.            
  85.  
  86.             public function do_ssl_check()
  87.             {
  88.                 if( 'yes'  != $this->stripe_sandbox && "no" == get_option( 'woocommerce_force_ssl_checkout' )  && "yes" == $this->enabled ) {
  89.                     echo "<div class=\"error\"><p>". sprintf( __( "<strong>%s</strong> is enabled and WooCommerce is not forcing the SSL on your checkout page. Please ensure that you have a valid SSL certificate and that you are <a href=\"%s\">forcing the checkout pages to be secured.</a>" ), $this->method_title, admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ) ."</p></div>"; 
  90.                 }
  91.             }
  92.  
  93.  
  94.             public function load_stripe_scripts() {
  95.  
  96.                 wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', false, '2.0', true );
  97.  
  98.                 wp_enqueue_script( 'stripewoojs', plugins_url( 'assets/js/stripewoo.js',  __FILE__  ), array( 'stripe', 'wc-credit-card-form' ), '', true );
  99.  
  100.                 $stripe_array = array(
  101.                     'stripe_publishablekey'    => $this->stripe_sandbox == 'yes' ? $this->stripe_testpublickey : $this->stripe_livepublickey);
  102.  
  103.  
  104.                 if ( is_checkout_pay_page() ) {
  105.                     $order_key = urldecode( $_GET['key'] );
  106.                     $order_id  = absint( get_query_var( 'order-pay' ) );
  107.                     $order     = new WC_Order( $order_id );
  108.  
  109.                     if ( $order->id == $order_id && $order->order_key == $order_key ) {
  110.                         $stripe_array['billing_name']      = $order->billing_first_name.' '.$order->billing_last_name;
  111.                         $stripe_array['billing_address_1'] = $order->billing_address_1;
  112.                         $stripe_array['billing_address_2'] = $order->billing_address_2;
  113.                         $stripe_array['billing_city']      = $order->billing_city;
  114.                         $stripe_array['billing_state']     = $order->billing_state;
  115.                         $stripe_array['billing_postcode']  = $order->billing_postcode;
  116.                         $stripe_array['billing_country']   = $order->billing_country;
  117.                     }
  118.                 }
  119.  
  120.  
  121.                 wp_localize_script( 'stripewoojs', 'stripe_array', $stripe_array );
  122.  
  123.             }
  124.  
  125.  
  126.             public function admin_options()
  127.             {
  128.                 ?>
  129.                 <h3><?php _e( 'Stripe addon for Woocommerce', 'woocommerce' ); ?></h3>
  130.                 <p><?php  _e( 'Stripe is a company that provides a way for individuals and businesses to accept payments over the Internet.', 'woocommerce' ); ?></p>
  131.                 <table class="form-table">
  132.                     <?php $this->generate_settings_html(); ?>
  133.                 </table>
  134.                 <?php
  135.             }
  136.  
  137.  
  138.  
  139.             public function init_form_fields()
  140.             {
  141.  
  142.                 $this->form_fields = array(
  143.                     'enabled' => array(
  144.                         'title' => __( 'Enable/Disable', 'woocommerce' ),
  145.                         'type' => 'checkbox',
  146.                         'label' => __( 'Enable Stripe', 'woocommerce' ),
  147.                         'default' => 'yes'
  148.                         ),
  149.                     'stripe_title' => array(
  150.                         'title' => __( 'Title', 'woocommerce' ),
  151.                         'type' => 'text',
  152.                         'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
  153.                         'default' => __( 'Stripe', 'woocommerce' ),
  154.                         'desc_tip'      => true,
  155.                         ),
  156.                     'stripe_description' => array(
  157.                         'title' => __( 'Description', 'woocommerce' ),
  158.                         'type' => 'textarea',
  159.                         'description' => __( 'This controls the description which the user sees during checkout.', 'woocommerce' ),
  160.                         'default' => __( 'Note: This form processes payments securely via &copy; <a href="https://stripe.com" target="_blank">Stripe</a>. Your card details <strong>never</strong> hit our server', 'woocommerce' ),
  161.                         'desc_tip'      => true,
  162.                         ),
  163.                     'stripe_testsecretkey' => array(
  164.                         'title' => __( 'Test Secret Key', 'woocommerce' ),
  165.                         'type' => 'text',
  166.                         'description' => __( 'This is the Secret Key found in API Keys in Account Dashboard.', 'woocommerce' ),
  167.                         'default' => '',
  168.                         'desc_tip'      => true,
  169.                         'placeholder' => 'Stripe Test Secret Key'
  170.                         ),
  171.  
  172.                     'stripe_testpublickey' => array(
  173.                         'title' => __( 'Test Publishable Key', 'woocommerce' ),
  174.                         'type' => 'text',
  175.                         'description' => __( 'This is the Publishable Key found in API Keys in Account Dashboard.', 'woocommerce' ),
  176.                         'default' => '',
  177.                         'desc_tip'      => true,
  178.                         'placeholder' => 'Stripe Test Publishable Key'
  179.                         ),
  180.                    
  181.                     'stripe_livesecretkey' => array(
  182.                         'title' => __( 'Live Secret Key', 'woocommerce' ),
  183.                         'type' => 'text',
  184.                         'description' => __( 'This is the Secret Key found in API Keys in Account Dashboard.', 'woocommerce' ),
  185.                         'default' => '',
  186.                         'desc_tip'      => true,
  187.                         'placeholder' => 'Stripe Live Secret Key'
  188.                         ),
  189.  
  190.                     'stripe_livepublickey' => array(
  191.                         'title' => __( 'Live Publishable Key', 'woocommerce' ),
  192.                         'type' => 'text',
  193.                         'description' => __( 'This is the Publishable Key found in API Keys in Account Dashboard.', 'woocommerce' ),
  194.                         'default' => '',
  195.                         'desc_tip'      => true,
  196.                         'placeholder' => 'Stripe Live Publishable Key'
  197.                         ),
  198.                    
  199.  
  200.                     'stripe_storecurrency'    => array(
  201.                         'title'        => __('Fund Receiving Currency'),
  202.                         'type'         => 'select',
  203.                         'class'        => 'select',
  204.                         'css'          => 'width: 350px;',
  205.                         'desc_tip'     => __( 'Select the currency in which you like to receive payment the currency that has (*) is unsupported on  American Express Cards.This is independent of store base currency so please update your cart price accordingly.', 'woocommerce' ),
  206.                         'options'      => array( 'USD'=>' United States Dollar','AED'=>'United Arab Emirates Dirham','AFN'=>' Afghan Afghani*','ALL'=>' Albanian Lek','AMD'=>' Armenian Dram','ANG'=>' Netherlands Antillean Gulden','AOA'=>' Angolan Kwanza*','ARS'=>' Argentine Peso*','AUD'=>' Australian Dollar','AWG'=>' Aruban Florin','AZN'=>' Azerbaijani Manat','BAM'=>' Bosnia & Herzegovina Convertible Mark','BBD'=>' Barbadian Dollar','BDT'=>' Bangladeshi Taka','BGN'=>' Bulgarian Lev','BIF'=>' Burundian Franc','BMD'=>' Bermudian Dollar','BND'=>' Brunei Dollar','BOB'=>' Bolivian Boliviano*','BRL'=>' Brazilian Real*','BSD'=>' Bahamian Dollar','BWP'=>' Botswana Pula','BZD'=>' Belize Dollar','CAD'=>' Canadian Dollar','CDF'=>' Congolese Franc','CHF'=>' Swiss Franc','CLP'=>' Chilean Peso*','CNY'=>' Chinese Renminbi Yuan','COP'=>' Colombian Peso*','CRC'=>' Costa Rican Colón*','CVE'=>' Cape Verdean Escudo*','CZK'=>' Czech Koruna*','DJF'=>' Djiboutian Franc*','DKK'=>' Danish Krone','DOP'=>' Dominican Peso','DZD'=>' Algerian Dinar','EEK'=>' Estonian Kroon*','EGP'=>' Egyptian Pound','ETB'=>' Ethiopian Birr','EUR'=>' Euro','FJD'=>' Fijian Dollar','FKP'=>' Falkland Islands Pound*','GBP'=>' British Pound','GEL'=>' Georgian Lari','GIP'=>' Gibraltar Pound','GMD'=>' Gambian Dalasi','GNF'=>' Guinean Franc*','GTQ'=>' Guatemalan Quetzal*','GYD'=>' Guyanese Dollar','HKD'=>' Hong Kong Dollar','HNL'=>' Honduran Lempira*','HRK'=>' Croatian Kuna','HTG'=>' Haitian Gourde','HUF'=>' Hungarian Forint*','IDR'=>' Indonesian Rupiah','ILS'=>' Israeli New Sheqel','INR'=>' Indian Rupee*','ISK'=>' Icelandic Króna','JMD'=>' Jamaican Dollar','JPY'=>' Japanese Yen','KES'=>' Kenyan Shilling','KGS'=>' Kyrgyzstani Som','KHR'=>' Cambodian Riel','KMF'=>' Comorian Franc','KRW'=>' South Korean Won','KYD'=>' Cayman Islands Dollar','KZT'=>' Kazakhstani Tenge','LAK'=>' Lao Kip*','LBP'=>' Lebanese Pound','LKR'=>' Sri Lankan Rupee','LRD'=>' Liberian Dollar','LSL'=>' Lesotho Loti','LTL'=>' Lithuanian Litas','LVL'=>' Latvian Lats','MAD'=>' Moroccan Dirham','MDL'=>' Moldovan Leu','MGA'=>' Malagasy Ariary','MKD'=>' Macedonian Denar','MNT'=>' Mongolian Tögrög','MOP'=>' Macanese Pataca','MRO'=>' Mauritanian Ouguiya','MUR'=>' Mauritian Rupee*','MVR'=>' Maldivian Rufiyaa','MWK'=>' Malawian Kwacha','MXN'=>' Mexican Peso*','MYR'=>' Malaysian Ringgit','MZN'=>' Mozambican Metical','NAD'=>' Namibian Dollar','NGN'=>' Nigerian Naira','NIO'=>' Nicaraguan Córdoba*','NOK'=>' Norwegian Krone','NPR'=>' Nepalese Rupee','NZD'=>' New Zealand Dollar','PAB'=>' Panamanian Balboa*','PEN'=>' Peruvian Nuevo Sol*','PGK'=>' Papua New Guinean Kina','PHP'=>' Philippine Peso','PKR'=>' Pakistani Rupee','PLN'=>' Polish Złoty','PYG'=>' Paraguayan Guaraní*','QAR'=>' Qatari Riyal','RON'=>' Romanian Leu','RSD'=>' Serbian Dinar','RUB'=>' Russian Ruble','RWF'=>' Rwandan Franc','SAR'=>' Saudi Riyal','SBD'=>' Solomon Islands Dollar','SCR'=>' Seychellois Rupee','SEK'=>' Swedish Krona','SGD'=>' Singapore Dollar','SHP'=>' Saint Helenian Pound*','SLL'=>' Sierra Leonean Leone','SOS'=>' Somali Shilling','SRD'=>' Surinamese Dollar*','STD'=>' São Tomé and Príncipe Dobra','SVC'=>' Salvadoran Colón*','SZL'=>' Swazi Lilangeni','THB'=>' Thai Baht','TJS'=>' Tajikistani Somoni','TOP'=>' Tongan Paʻanga','TRY'=>' Turkish Lira','TTD'=>' Trinidad and Tobago Dollar','TWD'=>' New Taiwan Dollar','TZS'=>' Tanzanian Shilling','UAH'=>' Ukrainian Hryvnia','UGX'=>' Ugandan Shilling','UYU'=>' Uruguayan Peso*','UZS'=>' Uzbekistani Som','VND'=>' Vietnamese Đồng','VUV'=>' Vanuatu Vatu','WST'=>' Samoan Tala','XAF'=>' Central African Cfa Franc','XCD'=>' East Caribbean Dollar','XOF'=>' West African Cfa Franc*','XPF'=>' Cfp Franc*','YER'=>' Yemeni Rial','ZAR'=>' South African Rand','ZMW'=>' Zambian Kwacha'),
  207.                         'description'  => "<span style='color:red;'>Select the currency in which you like to receive payment the currency that has (*) is unsupported on  American Express Cards.This is independent of store base currency so please update your cart price accordingly.</span>",
  208.  
  209.                         'default' => 'USD',
  210.                         ),
  211.  
  212.                     'stripe_sandbox' => array(
  213.                         'title'       => __( 'Stripe Sandbox', 'woocommerce' ),
  214.                         'type'        => 'checkbox',
  215.                         'label'       => __( 'Enable stripe sandbox (Live Mode if Unchecked)', 'woocommerce' ),
  216.                         'description' => __( 'If checked its in sanbox mode and if unchecked its in live mode', 'woocommerce' ),
  217.                         'desc_tip'      => true,
  218.                         'default'     => 'no',
  219.                         ),
  220.  
  221.                     'stripe_authorize_only' => array(
  222.                         'title'       => __( 'Authorize Only', 'woocommerce' ),
  223.                         'type'        => 'checkbox',
  224.                         'label'       => __( 'Enable Authorize Only Mode (Authorize & Capture If Unchecked)', 'woocommerce' ),
  225.                         'description' => __( 'If checked will only authorize the credit card only upon checkout.', 'woocommerce' ),
  226.                         'desc_tip'      => true,
  227.                         'default'     => 'no',
  228.                         ),
  229.  
  230.                     'stripe_cardtypes' => array(
  231.                         'title'    => __( 'Accepted Cards', 'woocommerce' ),
  232.                         'type'     => 'multiselect',
  233.                         'class'    => 'chosen_select',
  234.                         'css'      => 'width: 350px;',
  235.                         'desc_tip' => __( 'Select the card types to accept.', 'woocommerce' ),
  236.                         'options'  => array(
  237.                             'mastercard'       => 'MasterCard',
  238.                             'visa'             => 'Visa',
  239.                             'discover'         => 'Discover',
  240.                             'amex'             => 'American Express',
  241.                             'jcb'              => 'JCB',
  242.                             'dinersclub'       => 'Dinners Club',
  243.                             ),
  244.                         'default' => array( 'mastercard', 'visa', 'discover', 'amex' ),
  245.                         ),
  246.  
  247.  
  248.                     'stripe_meta_cartspan' => array(
  249.                         'title'       => __( 'Enable CartSpan', 'woocommerce' ),
  250.                         'type'        => 'checkbox',
  251.                         'label'       => __( 'Enable <a href="http://www.cartspan.com/">CartSpan</a> to Stores Last4 & Brand of Card (Active If Checked)', 'woocommerce' ),
  252.                         'description' => __( 'If checked will store last4 and card brand in local db from charge object.', 'woocommerce' ),
  253.                         'desc_tip'      => true,
  254.                         'default'     => 'no',
  255.                         )
  256. );
  257. }
  258.  
  259.  
  260.  
  261.         //Function to check IP
  262.         function get_client_ip()
  263.         {
  264.             $ipaddress = '';
  265.             if (getenv('HTTP_CLIENT_IP'))
  266.                 $ipaddress = getenv('HTTP_CLIENT_IP');
  267.             else if(getenv('HTTP_X_FORWARDED_FOR'))
  268.                 $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
  269.             else if(getenv('HTTP_X_FORWARDED'))
  270.                 $ipaddress = getenv('HTTP_X_FORWARDED');
  271.             else if(getenv('HTTP_FORWARDED_FOR'))
  272.                 $ipaddress = getenv('HTTP_FORWARDED_FOR');
  273.             else if(getenv('HTTP_FORWARDED'))
  274.                 $ipaddress = getenv('HTTP_FORWARDED');
  275.             else if(getenv('REMOTE_ADDR'))
  276.                 $ipaddress = getenv('REMOTE_ADDR');
  277.             else
  278.                 $ipaddress = '0.0.0.0';
  279.             return $ipaddress;
  280.         }
  281.  
  282.         //End of function to check IP
  283.  
  284.         public function get_description() {
  285.             return apply_filters( 'woocommerce_gateway_description',$this->stripe_description, $this->id );
  286.         }
  287.  
  288.  
  289.         /*Is Avalaible*/
  290.         public function is_available() {
  291.             if(empty($this->stripe_testpublickey) || empty($this->stripe_testsecretkey) || empty($this->stripe_livepublickey) || empty($this->stripe_livesecretkey) ) {return false;
  292.             }
  293.  
  294.             return true;
  295.         }
  296.         /*end is availaible*/
  297.  
  298.  
  299.  
  300.         /*Get Icon*/
  301.         public function get_icon() {
  302.             $icon = '';
  303.             if(is_array($this->stripe_cardtypes))
  304.             {
  305.                 foreach ( $this->stripe_cardtypes as $card_type ) {
  306.  
  307.                     if ( $url = $this->stripe_get_active_card_logo_url( $card_type ) ) {
  308.  
  309.                         $icon .= '<img width="45" src="'.esc_url( $url ).'" alt="'.esc_attr( strtolower( $card_type ) ).'" />';
  310.                     }
  311.                 }
  312.             }
  313.             else
  314.             {
  315.                 $icon .= '<img  src="'.esc_url( plugins_url( 'images/stripe.png' , __FILE__ ) ).'" alt="Stripe Gateway" />';
  316.             }
  317.  
  318.             return apply_filters( 'woocommerce_stripe_icon', $icon, $this->id );
  319.         }
  320.  
  321.         public function stripe_get_active_card_logo_url( $type ) {
  322.  
  323.             $image_type = strtolower( $type );
  324.             return  WC_HTTPS::force_https_url( plugins_url( 'images/' . $image_type . '.png' , __FILE__ ) );
  325.         }
  326.  
  327.        
  328.  
  329.  
  330.         /*Process Payment*/
  331.  
  332.         public function process_payment( $order_id )
  333.         {
  334.             global $error;
  335.             global $woocommerce;
  336.             $wc_order       = wc_get_order( $order_id );
  337.             $grand_total    = $wc_order->order_total;
  338.  
  339.             if(in_array($this->stripe_storecurrency ,$this->stripe_zerodecimalcurrency ))
  340.             {
  341.                 $amount          = number_format($grand_total,0,".","") ;
  342.             }
  343.             else
  344.             {
  345.                 $amount          = $grand_total * 100 ;
  346.             }
  347.  
  348.             try
  349.             {
  350.  
  351.                 // create token for customer/buyer credit card
  352.                 $token_id = sanitize_text_field($_POST['stripe_token']);
  353.  
  354.                 $customer = Stripe_Customer::create(array(
  355.                                 'source'        => $token_id,
  356.                                 'description'   => $wc_order->billing_email.' - '.$wc_order->get_order_number()));
  357.  
  358.                 $charge = Stripe_Charge::create(array(
  359.                     'amount'   => $amount,
  360.                     'currency' => $this->stripe_storecurrency,
  361.                     'customer' => $customer->id,
  362.                     'capture'  => false,
  363.                     'statement_descriptor'  => 'Online Shopping',
  364.                     'metadata' => array(
  365.                                         'Order #'           => $wc_order->get_order_number(),
  366.                                         'Total Tax'         => $wc_order->get_total_tax(),
  367.                                         'Total Shipping'    => $wc_order->get_total_shipping(),
  368.                                         'Customer IP'       => $this->get_client_ip(),
  369.                                         'WP customer #'     => $wc_order->user_id,
  370.                                         'Billing Email'     => $wc_order->billing_email,
  371.                                        ) ,
  372.                     'receipt_email'         => $wc_order->billing_email,
  373.                     'description'  => get_bloginfo('blogname').' Order #'.$wc_order->get_order_number(),
  374.                     'shipping' => array(
  375.                                         'address' => array(
  376.                                             'line1'         => $wc_order->shipping_address_1,
  377.                                             'line2'         => $wc_order->shipping_address_2,
  378.                                             'city'          => $wc_order->shipping_city,
  379.                                             'state'         => $wc_order->shipping_state,
  380.                                             'country'       => $wc_order->shipping_country,
  381.                                             'postal_code'   => $wc_order->shipping_postcode    
  382.                                             ),
  383.                                         'name' => $wc_order->shipping_first_name.' '.$wc_order->shipping_last_name,
  384.                                         'phone'=> $wc_order->billing_phone
  385.                                     )
  386.  
  387.                 ));
  388.  
  389.  
  390.                 /* working code here */
  391.                 /*$charge = Stripe_Charge::create(array(
  392.                     'amount'                => $amount,
  393.                     'currency'              => $this->stripe_storecurrency,
  394.                     'customer'              => $$customer->id,
  395.                     'capture'               => STRIPE_TRANSACTION_MODE,
  396.                     'statement_descriptor'  => 'Online Shopping',
  397.                     'metadata'              => array(
  398.                         'Order #'           => $wc_order->get_order_number(),
  399.                         'Total Tax'         => $wc_order->get_total_tax(),
  400.                         'Total Shipping'    => $wc_order->get_total_shipping(),
  401.                         'Customer IP'       => $this->get_client_ip(),
  402.                         'WP customer #'     => $wc_order->user_id,
  403.                         'Billing Email'     => $wc_order->billing_email,
  404.                         ) ,
  405.                     'receipt_email'         => $wc_order->billing_email,
  406.                     'description'           => get_bloginfo('blogname').' Order #'.$wc_order->get_order_number(),
  407.                     'shipping'              => array(
  408.                         'address' => array(
  409.                             'line1'         => $wc_order->shipping_address_1,
  410.                             'line2'         => $wc_order->shipping_address_2,
  411.                             'city'          => $wc_order->shipping_city,
  412.                             'state'         => $wc_order->shipping_state,
  413.                             'country'       => $wc_order->shipping_country,
  414.                             'postal_code'   => $wc_order->shipping_postcode
  415.                             ),
  416.                         'name' => $wc_order->shipping_first_name.' '.$wc_order->shipping_last_name,
  417.                         'phone'=> $wc_order->billing_phone
  418.                         )
  419.  
  420.                     )
  421.                 );*/
  422.  
  423.                 if($token_id !='')
  424.                 {
  425.                     if ($charge->paid == true)
  426.                     {
  427.  
  428.                         $epoch     = $charge->created;
  429.                         $dt        = new DateTime("@$epoch");
  430.                         $timestamp = $dt->format('Y-m-d H:i:s e');
  431.                         $chargeid  = $charge->id ;
  432.  
  433.                         $wc_order->add_order_note(__( 'Payment completed at-'.$timestamp.',Charge ID='.$charge->id.',Card='.$charge->source->brand.' : '.$charge->source->last4.' : '.$charge->source->exp_month.'/'.$charge->source->exp_year,'woocommerce'));
  434.  
  435.                         $wc_order->payment_complete($chargeid);
  436.                         WC()->cart->empty_cart();
  437.  
  438.                         if('yes' == $this->stripe_meta_cartspan)
  439.                         {
  440.                             $stripe_metas_for_cartspan = array(
  441.                                 'cc_type'           => $charge->source->brand,
  442.                                 'cc_last4'          => $charge->source->last4,
  443.                                 'cc_trans_id'       => $charge->id,
  444.                                 );
  445.                             add_post_meta( $order_id, '_stripe_metas_for_cartspan', $stripe_metas_for_cartspan);
  446.                         }
  447.  
  448.  
  449.                     if(true == $charge->captured && true == $charge->paid)
  450.                     {
  451.                         add_post_meta( $order_id, '_stripe_charge_status', 'charge_auth_captured');
  452.                     }
  453.  
  454.                     if(false == $charge->captured && true == $charge->paid)
  455.                     {
  456.                         add_post_meta( $order_id, '_stripe_charge_status', 'charge_auth_only');
  457.                     }
  458.  
  459.  
  460.                         return array (
  461.                             'result'   => 'success',
  462.                             'redirect' => $this->get_return_url( $wc_order ),
  463.                             );
  464.                     }
  465.                     else
  466.                     {
  467.                         $wc_order->add_order_note( __( 'Stripe payment failed.'.$error, 'woocommerce' ) );
  468.                         wc_add_notice($error, $notice_type = 'error' );
  469.  
  470.                     }
  471.  
  472.                 }
  473.             }
  474.  
  475.             catch (Exception $e)
  476.             {
  477.  
  478.                 $body         = $e->getJsonBody();
  479.                 $error        = $body['error']['message'];
  480.                 $wc_order->add_order_note( __( 'Stripe payment failed due to.'.$error, 'woocommerce' ) );
  481.                 wc_add_notice($error,  $notice_type = 'error' );
  482.             }
  483.  
  484.  
  485.  
  486.  
  487.         } // end of function process_payment()
  488.  
  489.  
  490.         /*process refund function*/
  491.         public function process_refund($order_id, $amount = NULL, $reason = '' ) {
  492.  
  493.  
  494.             if($amount > 0 )
  495.             {
  496.                 $CHARGE_ID  = get_post_meta( $order_id , '_transaction_id', true );
  497.                 $charge         = Stripe_Charge::retrieve($CHARGE_ID);
  498.                 $refund         = $charge->refunds->create(
  499.                     array(
  500.                         'amount'        => $amount * 100,
  501.                         'metadata'  => array('Order #'      => $order_id,
  502.                             'Refund reason' => $reason
  503.                             ),
  504.                         )
  505.                     );
  506.                 if($refund)
  507.                 {
  508.  
  509.                     $repoch      = $refund->created;
  510.                     $rdt         = new DateTime("@$repoch");
  511.                     $rtimestamp  = $rdt->format('Y-m-d H:i:s e');
  512.                     $refundid    = $refund->id;
  513.                     $wc_order    = new WC_Order( $order_id );
  514.                     $wc_order->add_order_note( __( 'Stripe Refund completed at. '.$rtimestamp.' with Refund ID = '.$refundid , 'woocommerce' ) );
  515.                     return true;
  516.                 }
  517.                 else
  518.                 {
  519.                     return false;
  520.                 }
  521.  
  522.  
  523.             }
  524.             else
  525.             {
  526.                 return false;
  527.             }
  528.  
  529.  
  530.  
  531.         }// end of  process_refund()
  532.  
  533.  
  534.  
  535.     }  // end of class WC_Stripe_Gateway
  536.  
  537. } // end of if class exist WC_Gateway
  538.  
  539. }
  540.  
  541. /*Activation hook*/
  542. add_action( 'plugins_loaded', 'stripe_init' );
  543.  
  544. function stripe_woocommerce_addon_activate() {
  545.  
  546.     if(!function_exists('curl_exec'))
  547.     {
  548.         wp_die( '<pre>This plugin requires PHP CURL library installled in order to be activated </pre>' );
  549.     }
  550. }
  551. register_activation_hook( __FILE__, 'stripe_woocommerce_addon_activate' );
  552. /*Activation hook*/
  553.  
  554. /*Plugin Settings Link*/
  555.  
  556.  
  557. function stripe_woocommerce_addon_settings_link( $links ) {
  558.     $settings_link = '<a href="admin.php?page=wc-settings&tab=checkout&section=wc_stripe_gateway">' . __( 'Settings' ) . '</a>';
  559.     array_push( $links, $settings_link );
  560.     return $links;
  561. }
  562. $plugin = plugin_basename( __FILE__ );
  563. add_filter( "plugin_action_links_$plugin", 'stripe_woocommerce_addon_settings_link' );
  564. /*Plugin Settings Link*/
  565.  
  566. /*Capture Charge*/
  567.  
  568. function stripe_capture_meta_box() {
  569.     global $post;
  570.     $chargestatus = get_post_meta( $post->ID, '_stripe_charge_status', true );
  571.     if($chargestatus == 'charge_auth_only')
  572.     {
  573.             add_meta_box(
  574.                 'stripe_capture_chargeid',
  575.                 __( 'Capture Charge', 'woocommerce' ),
  576.                 'stripe_capture_meta_box_callback',
  577.                 'shop_order',
  578.                 'side',
  579.                 'default'
  580.             );
  581.     }
  582. }
  583. add_action( 'add_meta_boxes', 'stripe_capture_meta_box' );
  584.  
  585.  
  586. function stripe_capture_meta_box_callback( $post ) {
  587.  
  588.     //charge_auth_only, charge_auth_captured, charge_auth_captured_later
  589.     echo '<input type="checkbox" name="_stripe_capture_charge" value="1"/>&nbsp;Check & Save Order to Capture';
  590. }
  591.  
  592.  
  593. /*Execute charge on order save*/
  594. function stripe_capture_meta_box_action($order_id, $items )
  595. {
  596.     if(isset($items['_stripe_capture_charge']) && (1 ==$items['_stripe_capture_charge']) )
  597.     {
  598.         global $post;
  599.         $chargeid = get_post_meta( $post->ID, '_transaction_id', true );
  600.         if(class_exists('WC_Stripe_Gateway'))
  601.         {
  602.             $stripepg = new WC_Stripe_Gateway();
  603.  
  604.             if('yes'  == $stripepg->stripe_sandbox  )
  605.             { Stripe::setApiKey($stripepg->stripe_testsecretkey);  }
  606.             else
  607.             { Stripe::setApiKey($stripepg->stripe_livesecretkey);  }
  608.  
  609.         }
  610.  
  611.  
  612.         $capturecharge   = Stripe_Charge::retrieve($chargeid);
  613.         $captureresponse = $capturecharge->capture();
  614.  
  615.        
  616.         if(true == $captureresponse->captured && true == $captureresponse->paid)
  617.         {
  618.             $epoch     = $captureresponse->created;
  619.             $dt        = new DateTime("@$epoch");
  620.             $timestamp = $dt->format('Y-m-d H:i:s e');
  621.  
  622.             $wc_order = new WC_Order($order_id);
  623.             update_post_meta( $order_id, '_stripe_charge_status', 'charge_auth_captured_later');
  624.            
  625.             $wc_order->add_order_note(__( 'Stripe charge captured at-'.$timestamp.',Charge ID='.$captureresponse->id.',Card='.$captureresponse->source->brand.' : '.$captureresponse->source->last4.' : '.$captureresponse->source->exp_month.'/'.$captureresponse->source->exp_year,'woocommerce'));
  626.             unset($wc_order);
  627.         }
  628.  
  629.     }  
  630.  
  631. }
  632. add_action ("woocommerce_saved_order_items", "stripe_capture_meta_box_action", 10,2);
  633. /*Execute charge on order save*/
  634.  
  635. add_action('admin_notices', 'stripe_admin_notice');
  636.  
  637. function stripe_admin_notice() {
  638.     global $current_user ;
  639.         $user_id = $current_user->ID;
  640.         /* Check that the user hasn't already clicked to ignore the message */
  641.     if ( ! get_user_meta($user_id, 'stripe_ignore_notice') ) {
  642.         echo '<div class="wc_plugin_upgrade_notice">';
  643.         printf(__('Stripe WooCommerce Addon 1.0.6 is a major release of this plugin and requires you to <a href="%2$s">Re Configure Plugin Settings</a> for PCI Compliance | <a href="%1$s">Hide Notice</a>'), '?stripe_warning_ignore=0','admin.php?page=wc-settings&tab=checkout&section=wc_stripe_gateway');
  644.         echo "</div>";
  645.     }
  646. }
  647.  
  648. add_action('admin_init', 'stripe_warning_ignore');
  649.  
  650. function stripe_warning_ignore() {
  651.     global $current_user;
  652.         $user_id = $current_user->ID;
  653.         if ( isset($_GET['stripe_warning_ignore']) && '0' == $_GET['stripe_warning_ignore'] ) {
  654.              add_user_meta($user_id, 'stripe_ignore_notice', 'true', true);
  655.     }
  656. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement