forked from stezu/stripe-for-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripe-for-woocommerce.php
179 lines (143 loc) · 6.39 KB
/
stripe-for-woocommerce.php
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
<?php
/*
* Plugin Name: Stripe for WooCommerce
* Plugin URI: https://wordpress.org/plugins/stripe-for-woocommerce
* Description: Use Stripe for collecting credit card payments on WooCommerce.
* Version: 1.30
* Author: Stephen Zuniga
* Author URI: http://stephenzuniga.com
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* Foundation built by: Sean Voss // https://github.com/seanvoss/striper
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class S4WC {
public function __construct() {
global $wpdb;
// Include Stripe Methods
include_once( 'classes/class-s4wc_api.php' );
// Include Database Manipulation Methods
include_once( 'classes/class-s4wc_db.php' );
// Include Customer Profile Methods
include_once( 'classes/class-s4wc_customer.php' );
// Transition to new namespace
if ( ! get_option( 'woocommerce_s4wc_settings' ) ) {
// Update settings
update_option( 'woocommerce_s4wc_settings', get_option( 'woocommerce_wc_stripe_settings', array() ) );
delete_option( 'woocommerce_wc_stripe_settings' );
// Update saved payment methods
$wpdb->query(
"
UPDATE $wpdb->postmeta
SET `meta_value` = 's4wc'
WHERE `meta_value` = 'wc_stripe'
"
);
}
// Update transaction id's to new version so we don't break capturing
if ( ! get_option( 'woocommerce_s4wc_update_transaction_id' ) ) {
$wpdb->query(
"
UPDATE $wpdb->postmeta
SET `meta_key` = '_transaction_id'
WHERE `meta_key` = 'transaction_id'
"
);
// Make sure we only do this once
update_option( 'woocommerce_s4wc_update_transaction_id', true );
}
// Grab settings
$this->settings = get_option( 'woocommerce_s4wc_settings', array() );
// Add default values for fresh installs
$this->settings['testmode'] = isset( $this->settings['testmode'] ) ? $this->settings['testmode'] : 'yes';
$this->settings['test_publishable_key'] = isset( $this->settings['test_publishable_key'] ) ? $this->settings['test_publishable_key'] : '';
$this->settings['test_secret_key'] = isset( $this->settings['test_secret_key'] ) ? $this->settings['test_secret_key'] : '';
$this->settings['live_publishable_key'] = isset( $this->settings['live_publishable_key'] ) ? $this->settings['live_publishable_key'] : '';
$this->settings['live_secret_key'] = isset( $this->settings['live_secret_key'] ) ? $this->settings['live_secret_key'] : '';
$this->settings['saved_cards'] = isset( $this->settings['saved_cards'] ) ? $this->settings['saved_cards'] : 'yes';
// API Info
$this->settings['publishable_key'] = $this->settings['testmode'] == 'yes' ? $this->settings['test_publishable_key'] : $this->settings['live_publishable_key'];
$this->settings['secret_key'] = $this->settings['testmode'] == 'yes' ? $this->settings['test_secret_key'] : $this->settings['live_secret_key'];
// Database info location
$this->settings['stripe_db_location'] = $this->settings['testmode'] == 'yes' ? '_stripe_test_customer_info' : '_stripe_live_customer_info';
// Hooks
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_stripe_gateway' ) );
add_action( 'woocommerce_order_status_processing_to_completed', array( $this, 'order_status_completed' ) );
// Localization
load_plugin_textdomain( 'stripe-for-woocommerce', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Add Stripe Gateway to WooCommerces list of Gateways
*
* @access public
* @param array $methods
* @return array
*/
public function add_stripe_gateway( $methods ) {
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
return;
}
// Include payment gateway
include_once( 'classes/class-s4wc_gateway.php' );
if ( class_exists( 'WC_Subscriptions_Order' ) ) {
include_once( 'classes/class-s4wc_subscriptions_gateway.php' );
$methods[] = 'S4WC_Subscriptions_Gateway';
} else {
$methods[] = 'S4WC_Gateway';
}
return $methods;
}
/**
* Process the captured payment when changing order status to completed
*
* @access public
* @param int $order_id
* @return mixed
*/
public function order_status_completed( $order_id = null ) {
if ( ! $order_id ) {
$order_id = $_POST['order_id'];
}
if ( get_post_meta( $order_id, 'capture', true ) ) {
$params = array();
if ( isset( $_POST['amount'] ) ) {
$params['amount'] = round( $_POST['amount'] );
}
$transaction_id = get_post_meta( $order_id, '_transaction_id', true );
$charge = S4WC_API::capture_charge( $transaction_id, $params );
return $charge;
}
}
}
$GLOBALS['s4wc'] = new S4WC();
/**
* Wrapper of wc_get_template to relate directly to s4wc
*
* @param string $template_name
* @param array $args
* @return string
*/
function s4wc_get_template( $template_name, $args = array() ) {
$template_path = WC()->template_path() . '/s4wc/';
$default_path = plugin_dir_path( __FILE__ ) . '/templates/';
return wc_get_template( $template_name, $args, $template_path, $default_path );
}
/**
* Helper function to find the key of a nested value
*
* @param string $needle
* @param array $haystack
* @return mixed
*/
if ( ! function_exists( 'recursive_array_search' ) ) {
function recursive_array_search( $needle, $haystack ) {
foreach ( $haystack as $key => $value ) {
if ( $needle === $value || ( is_array( $value ) && recursive_array_search( $needle, $value ) !== false ) ) {
return $key;
}
}
return false;
}
}