-
Notifications
You must be signed in to change notification settings - Fork 2
/
chargeio-for-woocommerce.php
216 lines (179 loc) · 8.22 KB
/
chargeio-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
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
<?php
/*
* Plugin Name: AffiniPay WooCommerce
* Description: Use the AffiniPay gateway for collecting credit card payments on WooCommerce.
* Version: 1.5.1
* Author: AffiniPay, LLC
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class CIO4WC {
public function __construct() {
global $wpdb;
// Include ChargeIO Methods
include_once( 'classes/class-cio4wc_api.php' );
// Include Database Manipulation Methods
include_once( 'classes/class-cio4wc_db.php' );
// Include Customer Profile Methods
include_once( 'classes/class-cio4wc_customer.php' );
// Grab settings
$this->settings = get_option( 'woocommerce_cio4wc_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'];
$this->settings['account'] = $this->settings['testmode'] == 'yes' ? $this->settings['test_account'] : $this->settings['live_account'];
// Database info location
$this->settings['chargeio_db_location'] = $this->settings['testmode'] == 'yes' ? '_chargeio_test_customer_info' : '_chargeio_live_customer_info';
// Hooks
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_chargeio_gateway' ) );
add_action( 'woocommerce_order_status_processing_to_completed', array( $this, 'order_status_completed' ) );
// Localization
load_plugin_textdomain( 'chargeio-for-woocommerce', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Add ChargeIO Gateway to WooCommerces list of Gateways
*
* @access public
* @param array $methods
* @return array
*/
public function add_chargeio_gateway( $methods ) {
if ( ! class_exists( 'WC_Payment_Gateway_CC' ) ) {
return;
}
// Include payment gateway
include_once( 'classes/class-cio4wc_gateway.php' );
if ( class_exists( 'WC_Subscriptions_Order' ) ) {
include_once( 'classes/class-cio4wc_subscriptions_gateway.php' );
$methods[] = 'CIO4WC_Subscriptions_Gateway';
} else {
$methods[] = 'CIO4WC_Gateway';
}
return $methods;
}
/**
* Localize ChargeIO error messages
*
* @access protected
* @param Exception $e
* @return string
*/
public function get_error_message( $e ) {
switch ( $e->getMessage() ) {
// Messages from ChargeIO API
case 'card_number_incorrect':
$message = __( 'Your card number is incorrect.', 'chargeio-for-woocommerce' );
break;
case 'card_number_invalid':
$message = __( 'Your card number is not a valid credit card number.', 'chargeio-for-woocommerce' );
break;
case 'card_cvv_incorrect':
$message = __( 'Your card\'s security code is incorrect.', 'chargeio-for-woocommerce' );
break;
case 'card_expired':
$message = __( 'Your card has expired.', 'chargeio-for-woocommerce' );
break;
case 'card_declined':
case 'card_declined_insufficient_funds':
case 'card_declined_limit_exceeded':
$message = __( 'Your card was declined.', 'chargeio-for-woocommerce' );
break;
case 'card_declined_insufficient_funds':
// Messages from CIO4WC
case 'cio4wc_problem_connecting':
case 'cio4wc_empty_response':
case 'cio4wc_invalid_response':
$message = __( 'There was a problem connecting to the payment gateway.', 'chargeio-for-woocommerce' );
break;
// Generic failed order
default:
$message = __( 'Failed to process the order, please try again later.', 'chargeio-for-woocommerce' );
}
return $message;
}
/**
* 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'];
}
// Manual capture is not supported at this time
/*
if ( get_post_meta( $order_id, '_cio4wc_capture', true ) || get_post_meta( $order_id, 'capture', true ) ) {
$order = new WC_Order( $order_id );
$params = array(
'amount' => isset( $_POST['amount'] ) ? $_POST['amount'] : $order->order_total * 100,
);
try {
$charge = CIO4WC_API::capture_charge( $order->transaction_id, $params );
if ( $charge ) {
$order->add_order_note(
sprintf(
__( '%s payment captured.', 'chargeio-for-woocommerce' ),
get_class( $this )
)
);
// Save ChargeIO fee
if ( isset( $charge->balance_transaction ) && isset( $charge->balance_transaction->fee ) ) {
$chargeio_fee = number_format( $charge->balance_transaction->fee / 100, 2, '.', '' );
update_post_meta( $order_id, 'ChargeIO Fee', $chargeio_fee );
}
}
} catch ( Exception $e ) {
$order->add_order_note(
sprintf(
__( '%s payment failed to capture. %s', 'chargeio-for-woocommerce' ),
get_class( $this ),
$this->get_error_message( $e )
)
);
}
}
*/
}
}
$GLOBALS['cio4wc'] = new CIO4WC();
/**
* Wrapper of wc_get_template to relate directly to cio4wc
*
* @param string $template_name
* @param array $args
* @return string
*/
function cio4wc_get_template( $template_name, $args = array() ) {
$template_path = WC()->template_path() . '/cio4wc/';
$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;
}
}