· 7 years ago · Jun 01, 2018, 06:36 AM
1<?php
2
3if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5}
6
7class WPNeo_Stripe_Connect extends WC_Payment_Gateway
8{
9
10 protected $stripe_checkout_image;
11 protected $client_id;
12 protected $secret_key;
13 protected $publishable_key;
14 protected $testmode;
15
16 /**
17 * WPNeo_Stripe_Connect constructor.
18 */
19 public function __construct(){
20 $this->id = 'wpneo_stripe_connect';
21 $this->has_fields = false;
22 $this->method_title = 'WPNeo Stripe connect';
23 $this->has_fields = true;
24 $this->init_form_fields();
25
26
27 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
28
29 $this->title = $this->get_option('title');
30 $this->description = $this->get_option('description');
31 $this->instructions = $this->get_option('instructions');
32 $this->stripe_checkout_image = $this->get_option( 'stripe_checkout_image', '' );
33
34 if ('yes' === $this->enabled){
35 add_action( 'wp_enqueue_scripts', array($this, 'wpneo_stripe_payment_script'), 10 ); //Do
36 }
37
38 if ($this->get_option('test_mode') === 'yes'){
39 $this->testmode = true;
40 $this->client_id = $this->get_option('test_client_id');
41 $this->secret_key = $this->get_option('test_secret_key');
42 $this->publishable_key = $this->get_option('test_publishable_key');
43 }else{
44 $this->testmode = false;
45 $this->client_id = $this->get_option('live_client_id');
46 $this->secret_key = $this->get_option('secret_key');
47 $this->publishable_key = $this->get_option('publishable_key');
48 }
49
50
51 if ( $this->testmode ) {
52 $this->description .= ' ' . sprintf( __( 'TEST MODE ENABLED. In test mode, you can use the card number 4242424242424242 with any CVC and a valid expiration date or check the documentation "<a href="%s">Testing Stripe</a>" for more card numbers.', 'woocommerce-gateway-stripe' ), 'https://stripe.com/docs/testing' );
53 $this->description = trim( $this->description );
54 }
55
56
57 }
58
59
60 /**
61 * Initialise Gateway Settings Form Fields.
62 */
63 public function init_form_fields() {
64
65 $this->form_fields = array(
66 'enabled' => array(
67 'title' => __( 'Enable Stripe Connect Payment', 'wp-crowdfunding' ),
68 'label' => __( 'Enable Stripe Connect Payment', 'wp-crowdfunding' ),
69 'type' => 'checkbox',
70 'description' => '',
71 'default' => 'no'
72 ),
73
74 'test_mode' => array(
75 'title' => __( 'Enable/Disable', 'wp-crowdfunding' ),
76 'type' => 'checkbox',
77 'label' => __( 'Enable Stripe Test Mode', 'wp-crowdfunding' ),
78 'default' => 'no'
79 ),
80
81
82 'receivers_percent' => array(
83 'title' => __( 'Receivers percent', 'wp-crowdfunding' ),
84 'type' => 'number',
85 'desc_tip' => true,
86 'description' => __( 'Campaign owner will get this percent, rest amount will credited stripe owner account as application fee', 'wp-crowdfunding' ),
87 'default' => ''
88 ),
89
90 'title' => array(
91 'title' => __( 'Title', 'wp-crowdfunding' ),
92 'type' => 'text',
93 'desc_tip' => true,
94 'description' => __( 'Title will see user during checkout as payment method', 'wp-crowdfunding' ),
95 'default' => ''
96 ),
97 'description' => array(
98 'title' => __( 'Description', 'wp-crowdfunding' ),
99 'type' => 'text',
100 'desc_tip' => true,
101 'description' => __( 'This description will see user during checkout', 'wp-crowdfunding' ),
102 'default' => ''
103 ),
104
105 'test_client_id' => array(
106 'title' => __( 'Stripe Connect Test Client ID', 'wp-crowdfunding' ),
107 'type' => 'text',
108 'description' => __( 'Get your client ID from stripe app settings', 'wp-crowdfunding' ),
109 'default' => '',
110 'desc_tip' => true,
111 ),
112 'live_client_id' => array(
113 'title' => __( 'Stripe Connect Live Client ID', 'wp-crowdfunding' ),
114 'type' => 'text',
115 'description' => __( 'Get your client ID from stripe app settings', 'wp-crowdfunding' ),
116 'default' => '',
117 'desc_tip' => true,
118 ),
119 'secret_key' => array(
120 'title' => __( 'Live Secret Key', 'wp-crowdfunding' ),
121 'type' => 'text',
122 'description' => __( 'Get your API keys from your stripe account.', 'wp-crowdfunding' ),
123 'default' => '',
124 'desc_tip' => true,
125 ),
126 'publishable_key' => array(
127 'title' => __( 'Live Publishable Key', 'wp-crowdfunding' ),
128 'type' => 'text',
129 'description' => __( 'Get your API keys from your stripe account.', 'wp-crowdfunding' ),
130 'default' => '',
131 'desc_tip' => true,
132 ),
133 'test_secret_key' => array(
134 'title' => __( 'Test Secret Key', 'wp-crowdfunding' ),
135 'type' => 'text',
136 'description' => __( 'Get your API keys from your stripe account.', 'wp-crowdfunding' ),
137 'default' => '',
138 'desc_tip' => true,
139 ),
140 'test_publishable_key' => array(
141 'title' => __( 'Test Publishable Key', 'wp-crowdfunding' ),
142 'type' => 'text',
143 'description' => __( 'Get your API keys from your stripe account.', 'wp-crowdfunding' ),
144 'default' => '',
145 'desc_tip' => true,
146 ),
147 'stripe_checkout_image' => array(
148 'title' => __( 'Stripe Checkout Image', 'wp-crowdfunding' ),
149 'description' => __( 'Optionally enter the URL to a 128x128px image of your brand or product. e.g. <code>https://yoursite.com/wp-content/uploads/2016/07/yourimage.jpg</code>', 'woocommerce-gateway-stripe' ),
150 'type' => 'text',
151 'default' => '',
152 'desc_tip' => true,
153 ),
154
155 );
156 }
157
158
159 /**
160 * Payment form on checkout page
161 */
162 public function payment_fields() {
163
164 echo '<fieldset class="stripe-legacy-payment-fields">';
165
166 echo $this->description;
167
168 $user = wp_get_current_user();
169
170 if ( $user ) {
171 $user_email = get_user_meta( $user->ID, 'billing_email', true );
172 $user_email = $user_email ? $user_email : $user->user_email;
173 } else {
174 $user_email = '';
175 }
176
177 echo '<div style="display:none;" id="stripe-payment-data"
178 data-description=""
179 data-email="' . esc_attr( $user_email ) . '"
180 data-amount="' . esc_attr( $this->get_stripe_amount( WC()->cart->total ) ) . '"
181 data-name="' . esc_attr( sprintf( __( '%s', 'woocommerce-gateway-stripe' ), get_bloginfo( 'name', 'display' ) ) ) . '"
182 data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '"
183 data-image="' . esc_attr( $this->stripe_checkout_image ) . '"
184 data-locale="en">';
185
186 echo '</div>';
187
188
189 echo '</fieldset>';
190 }
191
192 /**
193 * Get Stripe amount to pay
194 * @return float
195 */
196 public function get_stripe_amount( $total, $currency = '' ) {
197 if ( ! $currency ) {
198 $currency = get_woocommerce_currency();
199 }
200 switch ( strtoupper( $currency ) ) {
201 // Zero decimal currencies
202 case 'BIF' :
203 case 'CLP' :
204 case 'DJF' :
205 case 'GNF' :
206 case 'JPY' :
207 case 'KMF' :
208 case 'KRW' :
209 case 'MGA' :
210 case 'PYG' :
211 case 'RWF' :
212 case 'VND' :
213 case 'VUV' :
214 case 'XAF' :
215 case 'XOF' :
216 case 'XPF' :
217 $total = absint( $total );
218 break;
219 default :
220 $total = round( $total, 2 ) * 100; // In cents
221 break;
222 }
223 return $total;
224 }
225
226 /**
227 * stripe payment script
228 */
229
230 public function wpneo_stripe_payment_script(){
231
232 wp_enqueue_script( 'wpcf_stripe_checkout', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true );
233 wp_enqueue_script( 'wpneo_crowdfunding_stripe', plugin_dir_url( __DIR__).'assets/js/stripe_checkout.js', array( 'wpcf_stripe_checkout' ), WPNEO_CROWDFUNDING_VERSION, true );
234
235 $stripe_params = array(
236 'key' => $this->publishable_key,
237 'i18n_terms' => __( 'Please accept the terms and conditions first', 'woocommerce-gateway-stripe' ),
238 'i18n_required_fields' => __( 'Please fill in required checkout fields first', 'woocommerce-gateway-stripe' ),
239 );
240
241 // If we're on the pay page we need to pass stripe.js the address of the order.
242 if ( is_checkout_pay_page() && isset( $_GET['order'] ) && isset( $_GET['order_id'] ) ) {
243 $order_key = urldecode( $_GET['order'] );
244 $order_id = absint( $_GET['order_id'] );
245 $order = wc_get_order( $order_id );
246
247 if ( $order->get_id() === $order_id && $order->order_key === $order_key ) {
248 $stripe_params['billing_first_name'] = $order->billing_first_name;
249 $stripe_params['billing_last_name'] = $order->billing_last_name;
250 $stripe_params['billing_address_1'] = $order->billing_address_1;
251 $stripe_params['billing_address_2'] = $order->billing_address_2;
252 $stripe_params['billing_state'] = $order->billing_state;
253 $stripe_params['billing_city'] = $order->billing_city;
254 $stripe_params['billing_postcode'] = $order->billing_postcode;
255 $stripe_params['billing_country'] = $order->billing_country;
256 }
257 }
258
259 wp_localize_script( 'wpneo_crowdfunding_stripe', 'wpneo_stripe_params', apply_filters( 'wpneo_stripe_params', $stripe_params ) );
260 }
261
262
263
264 /**
265 * Process the payment
266 */
267 public function process_payment( $order_id, $retry = true, $force_customer = false ) {
268 try {
269 $order = wc_get_order( $order_id );
270
271 // Handle payment
272 if ( $order->get_total() > 0 ) {
273 // die(print_r($_POST));
274
275 $campaign_owner_stripe_account = '';
276
277 //Get total ordered amount from order item
278
279 $stripe_receiver_account = null;
280 foreach ($order->get_items() as $item) {
281 $product = wc_get_product($item['product_id']);
282 if($product->get_type() == 'crowdfunding'){
283 $campaign_author = $product->post->post_author;
284 $stripe_receiver_account = get_user_meta($campaign_author, 'stripe_user_id',true);
285 }
286 }
287
288 $receivers_percent = $this->get_option('receivers_percent');
289 $receivers_percent = absint($receivers_percent);
290 $total_amount = $this->get_stripe_amount($order->get_total());
291
292 $reciever_amount = ($total_amount * $receivers_percent) / 100;
293 $application_fee = $total_amount - $reciever_amount;
294 $currency = get_woocommerce_currency();
295
296 $actual_application_fee = $application_fee / 100;
297
298 if ( ! empty($_REQUEST['stripe_token']) && $stripe_receiver_account){
299
300 require_once 'stripe-php/init.php';
301 try{
302 $stripeToken = empty($_REQUEST['stripe_token']) ? '' : $_REQUEST['stripe_token'];
303 $stripeToken = wc_clean($stripeToken);
304
305 \Stripe\Stripe::setApiKey($this->secret_key);
306 \Stripe\Charge::create(array(
307 'amount' => $total_amount,
308 'currency' => $currency,
309 'source' => $stripeToken,
310 'application_fee' => $application_fee,
311 //'destination' => $stripe_receiver_account,
312
313 "description" => "Charged applicatin fee {$currency}{$actual_application_fee}",
314 ), array('stripe_account' => $stripe_receiver_account));
315
316
317 $order->payment_complete();
318 }catch (\Exception $e){
319 //die(print_r($e->getMessage()));
320 die(json_encode( array( 'result' => 'failure', 'messages' => '<div class="woocommerce-error">'.$e->getMessage().'</div>' ) ));
321 //$order->update_status( 'failed', $e->getMessage() );
322 }
323 }
324
325
326 } else {
327 $order->payment_complete();
328 }
329
330 // Remove cart
331 WC()->cart->empty_cart();
332
333 // Return thank you page redirect
334 return array(
335 'result' => 'success',
336 'redirect' => $this->get_return_url( $order )
337 );
338
339 } catch ( Exception $e ) {
340 wc_add_notice( $e->getMessage(), 'error' );
341 WC()->session->set( 'refresh_totals', true );
342 return;
343 }
344 }
345
346
347}
348
349//Paypal_Adaptive_Payment::instance();
350
351function add_wpneo_stripe_connect( $methods ) {
352 $methods[] = 'WPNeo_Stripe_Connect';
353 return $methods;
354}
355add_filter( 'woocommerce_payment_gateways', 'add_wpneo_stripe_connect' );