-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstripe-connect-for-woocommerce.php
1183 lines (954 loc) · 38.4 KB
/
stripe-connect-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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Stripe Connect for WooCommerce
* Plugin URI: https://zao.is
* Description: A modern Stripe Connect gateway for WooCommerce
* Version: 0.1.0
* Author: Zao
* Author URI: https://zao.is
* Donate link: https://zao.is
* License: MIT
* Text Domain: stripe-connect-for-woocommerce
* Domain Path: /languages
*
* @link https://zao.is
*
* @package Stripe_Connect_For_WooCommerce
* @version 0.1.0
*
* Built using generator-plugin-wp (https://github.com/WebDevStudios/generator-plugin-wp)
*/
/**
* Copyright (c) 2018 Zao (email : [email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 or, at
* your discretion, any later version, as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
define( 'STRIPE_CONNECT_WC_VERSION', '0.1.0' . ( defined( 'SCRIPT_DEBUG' ) ? time() : '' ) );
define( 'STRIPE_CONNECT_WC_BASENAME', plugin_basename( __FILE__ ) );
define( 'STRIPE_CONNECT_WC_URL', plugin_dir_url( __FILE__ ) );
define( 'STRIPE_CONNECT_WC_PATH', dirname( __FILE__ ) . '/' );
define( 'STRIPE_CONNECT_WC_INC', STRIPE_CONNECT_WC_PATH . 'includes/' );
// Use composer autoload.
require 'vendor/autoload.php';
/**
* Main initiation class.
*
* @since 0.1.0
*/
final class Stripe_Connect_For_WooCommerce {
/**
* Current version.
*
* @var string
* @since 0.1.0
*/
const VERSION = '0.1.0';
/**
* URL of plugin directory.
*
* @var string
* @since 0.1.0
*/
protected $url = '';
/**
* Path of plugin directory.
*
* @var string
* @since 0.1.0
*/
protected $path = '';
/**
* Plugin basename.
*
* @var string
* @since 0.1.0
*/
protected $basename = '';
/**
* Detailed activation error messages.
*
* @var array
* @since 0.1.0
*/
protected $activation_errors = array();
/**
* Seller Statements Object.
*
* @var Seller_Statements
* @since 0.1.0
*/
protected $statements = null;
/**
* Singleton instance of plugin.
*
* @var Stripe_Connect_For_WooCommerce
* @since 0.1.0
*/
protected static $single_instance = null;
/**
* Creates or returns an instance of this class.
*
* @since 0.1.0
* @return Stripe_Connect_For_WooCommerce A single instance of this class.
*/
public static function get_instance() {
if ( null === self::$single_instance ) {
self::$single_instance = new self();
}
return self::$single_instance;
}
/**
* Sets up our plugin.
*
* @since 0.1.0
*/
protected function __construct() {
$this->basename = plugin_basename( __FILE__ );
$this->url = plugin_dir_url( __FILE__ );
$this->path = plugin_dir_path( __FILE__ );
}
/**
* Attach other plugin classes to the base plugin class.
*
* @since 0.1.0
*/
public function plugin_classes() {
include_once $this->path . 'includes/settings.php';
include_once $this->path . 'includes/helpers.php';
include_once $this->path . 'includes/class-seller-statements.php';
} // END OF PLUGIN CLASSES FUNCTION
/**
* Add hooks and filters.
* Priority needs to be
* < 10 for CPT_Core,
* < 5 for Taxonomy_Core,
* and 0 for Widgets because widgets_init runs at init priority 1.
*
* @since 0.1.0
*/
public function hooks() {
add_action( 'init' , [ $this, 'init' ], 0 );
add_action( 'template_redirect' , [ $this, 'maybe_check_oauth' ] );
add_action( 'woocommerce_before_template_part' , [ $this, 'maybe_show_stripe_button' ] );
add_filter( 'wc_stripe_settings' , [ $this, 'add_connect_settings' ] );
add_action( 'edit_user_profile' , [ $this, 'add_stripe_connect_fields_to_profile' ] );
add_action( 'show_user_profile' , [ $this, 'add_stripe_connect_fields_to_profile' ] );
add_action( 'personal_options_update' , [ $this, 'add_stripe_connect_fields_to_usermeta' ] );
add_action( 'edit_user_profile_update' , [ $this, 'add_stripe_connect_fields_to_usermeta' ] );
add_filter( 'wc_stripe_generate_payment_request', [ $this, 'add_transfer_group_to_stripe_charge' ], 10, 3 );
add_action( 'wc_gateway_stripe_process_response', [ $this, 'create_payouts_to_each_seller' ] , 10, 2 );
add_filter( 'wcv_commission_rate_percent' , [ $this, 'filter_wcv_commission' ] , 10, 2 );
add_filter( 'woocommerce_shipping_packages' , [ $this, 'add_shipping_package_meta' ] );
add_filter( 'wcv_vendor_dues' , [ $this, 'add_shipping_tax_to_commissions' ], 10, 3 );
add_filter( 'wcv_order_export_csv_rows' , [ $this, 'adjust_exported_order_values' ] );
add_filter( 'wcv_order_export_csv_headers' , [ $this, 'unset_totals_column_from_order_export' ] );
add_filter( 'woocommerce_order_get_items' , [ $this, 'hack_wcv_vendor_email' ], 99, 2 );
add_action( 'stripe_connect_create_seller_payout', [ $this, 'record_seller_statements' ], 10, 7 );
if ( isset( $_GET['role'] ) && 'vendor' === $_GET['role'] ) {
add_filter( 'manage_users_columns' , [ $this, 'add_stripe_id_column' ] );
add_filter( 'manage_users_custom_column' , [ $this, 'render_stripe_id_column_data' ], 10, 3 );
}
add_filter( 'wcv_orders_table_rows', [ $this, 'filter_seller_dashboard_commissions' ] );
// TODO: get this working so that the commissions match the actual payouts.
// RELATED TODO: Ensure that Stripe webhook works properly, allowing for payouts received into vendor accounts to mark commissions as paid. payout.paid
// transfer.created - need to have a way to connect payouts and transfers for webhooks - pending support reply from Stripe.
// add_filter( 'wcv_vendor_dues' , [ $this, 'maybe_modify_totals' ] , 20, 3 );
// add_action( 'init' , 'scfwc_maybe_charge_monthly_fee' );
}
public function unset_totals_column_from_order_export( $cols ) {
unset( $cols['total'] );
return $cols;
}
public function hack_wcv_vendor_email( $items, $order ) {
$vendor = 0;
foreach ( $items as $key => $product ) {
if ( ! $vendor ) {
$vendor = $product->get_meta( 'vendor_id' );
}
}
$seller_statements = new Seller_Statements( $vendor );
$_totals = $seller_statements->get_totals( array( 'order_id' => $order->get_id(), 'date' => false ) );
$totals = max( $_totals['pending']['totals'], $_totals['paid']['totals'] );
add_filter( 'stripe_connect_commissions_email_render', function( $total ) use ( $items, $_totals, $vendor, $totals ) {
return $totals;
} );
return $items;
}
public function adjust_exported_order_values( $rows ) {
foreach ( $rows as $index => $row ) {
unset( $rows[ $index ][' total'] );
}
return $rows;
}
private function get_order_range_dates() {
$pv_options = get_option( 'wc_prd_vendor_options' );
$orders_sales_range = ( isset( $pv_options[ 'orders_sales_range' ] ) ) ? $pv_options[ 'orders_sales_range' ] : 'monthly';
$default_start = '';
switch ( $orders_sales_range ) {
case 'annually':
$default_start = '-1 year';
break;
case 'quarterly':
$default_start = '-3 month';
break;
case 'monthly':
$default_start = '-1 month';
break;
case 'weekly':
$default_start = '-1 week';
break;
case 'custom':
$default_start = '-1 year';
break;
default:
break;
}
$start = ( !empty( $_SESSION[ 'PV_Session' ][ '_wcv_order_start_date_input' ] ) ) ? $_SESSION[ 'PV_Session' ][ '_wcv_order_start_date_input' ] : strtotime( apply_filters( 'wcv_order_start_date', $default_start ) );
$end = ( !empty( $_SESSION[ 'PV_Session' ][ '_wcv_order_end_date_input' ] ) ) ? $_SESSION[ 'PV_Session' ][ '_wcv_order_end_date_input' ] : strtotime( apply_filters( 'wcv_order_end_date', 'now' ) );
return [ 'start' => $start, 'end' => $end ];
}
public function filter_seller_dashboard_commissions( $rows ) {
$dates = $this->get_order_range_dates();
$seller_statements = new Seller_Statements( get_current_user_id() );
$seller_statements->set_start( strtotime( '-1 day', $dates['start'] ) )->set_end( strtotime( '+1 day', $dates['end'] ) );
foreach ( $rows as $index => $row ) {
$order_id = $row->ID;
$total = $row->total;
$totals = $seller_statements->get_totals( array( 'order_id' => $order_id ) );
$product = max( $totals['pending']['product'], $totals['paid']['product'] );
$shipping = max( $totals['pending']['shipping'], $totals['paid']['shipping'] );
$tax = max( $totals['pending']['tax'], $totals['paid']['tax'] );
$totals = max( $totals['pending']['totals'], $totals['paid']['totals'] );
if ( ! $product ) {
continue;
}
$total_text = '<span class="wcv-tooltip" data-tip-text="'. sprintf( '%s %s %s %s %s %s', __( 'Product: ' , 'wcvendors-pro'), strip_tags( wc_price( $product ) ), __( 'Shipping: ' , 'wcvendors-pro'), strip_tags( wc_price( $shipping ) ), __( 'Tax: ' , 'wcvendors-pro'), strip_tags( wc_price( $tax ) ) ).'">'.wc_price( $totals ).'</span>';
$rows[ $index ]->total = $total_text;
}
return $rows;
}
/**
* Adds taxes generated from TaxJar for shipping rates to commissions prior to insertion.
*
* @param Array $receiver Array of commission receivers
* @param WC_Order $order WooCommerce Order Object.
* @param Bool $group Whether or not to group vendor products into one array or several.
* @return void
*/
public function add_shipping_tax_to_commissions( $receiver, $order, $group ) {
$shipping = $order->get_items( 'shipping' );
$rates = [];
foreach ( $shipping as $items ) {
$rates[ $items->get_meta( 'vendor_id' ) ] = $items->get_total_tax();
}
foreach ( $receiver as $vendor_id => $data ) {
if ( ! isset( $rates[ $vendor_id ] ) || ! $rates[ $vendor_id ] ) {
continue;
}
// We've now know we have taxes on shipping - now we just need to add to either the grouped tax rate, or the first of the non-grouped tax rates.
if ( $group ) {
$receiver[ $vendor_id ]['tax'] += $rates[ $vendor_id ];
continue;
} else {
foreach ( $data as $product_id => $contents ) {
$receiver[ $vendor_id ][ $product_id ]['tax'] += $rates[ $vendor_id ];
continue 2;
}
}
}
return $receiver;
}
/**
* Adds taxes generated from TaxJar for shipping rates to commissions prior to insertion.
*
* @param Array $receiver Array of commission receivers
* @param WC_Order $order WooCommerce Order Object.
* @param Bool $group Whether or not to group vendor products into one array or several.
* @return void
*/
public function maybe_modify_totals( $receiver, $order, $group ) {
$is_running_completed = doing_action( 'woocommerce_order_status_completed' );
$is_running_processing = doing_action( 'woocommerce_order_status_processing' );
if ( isset( $receiver[1] ) ) {
unset( $receiver[1] );
}
foreach ( $receiver as $vendor_id => $data ) {
$commission = $this->prepare_commission( $vendor_id, $order, $data, false );
$receiver[ $vendor_id ]['commission'] = $commission['total'];
}
return $receiver;
}
/**
* Returns Vendor Commission
*
* The commission should be their percentage of the NET Stripe payout on their items, plus taxes + shipping, minus monthly fees.
*
* @param [type] $vendor_id
* @param [type] $order
* @return void
*/
protected function prepare_commission( $vendor_id, $order, $commission, $log = true ) {
$vendor_name = get_user_by( 'id', $vendor_id )->display_name;
$totals = [];
// For now, we'll override the commission calculation, which for whatever reason, is not taking our custom rates into account
// TODO: Determine why that is - possibly hooking in too late.
$commission['commission'] = $this->calculate_base_total( $vendor_id, $order );
$log = [
'Base seller payout for ' . $vendor_name . ' for this order was ' . $commission['commission'] . ' based on a commission of ' . scfwc_get_seller_commission( $vendor_id ) . '%'
];
$totals['base'] = $commission['commission'];
$log[] = 'Subtotal = Base plus tax & shipping is ' . round( $commission['commission'] + $commission['tax'] + $commission['shipping'], 2 );
$total = round( $commission['commission'] + $commission['tax'] + $commission['shipping'], 2 );
$totals['subtotal'] = $total;
$stripe_fee = $this->get_stripe_fee_portion( $vendor_id, $order, $commission );
$total -= $stripe_fee;
$log[] = 'Total = subtotal of ' . $total . ' less Stripe fee portion of ' . $stripe_fee .' is ' . ( $total - $stripe_fee );
$payout_fee = $this->get_payout_fee( $vendor_id, $order, $commission );
$total -= $payout_fee;
$log[] = 'Payout fee of 0.25% of the total products, shipping, and taxes for ' . $vendor_name . ' is ' . $payout_fee;
$totals['transactional_fee'] = $payout_fee;
$totals['stripe_fee'] = $stripe_fee;
$monthly_fee = $this->maybe_process_monthly_fee( $vendor_id, $commission['total'] );
if ( $monthly_fee ) {
$log[] = 'Monthly fee of ' . $monthly_fee . ' was due for seller, resulting in a total transfer of ' . round( $total - $monthly_fee, 2 );
$totals['monthly_fee'] = $monthly_fee;
$total -= $monthly_fee;
} else {
$log[] = 'No monthly fee was due for seller, resulting in a total transfer of ' . $total;
}
if ( $log ) {
$order->add_order_note( implode( '<br />', $log ) );
}
$totals['total'] = round( $total, 2 );
return $totals;
}
/**
* Returns vendor's portion of Stripe fee to pay.
*
* Not every vendor will have equal amounts of the order - imagine one vendor selling $50 of a $2,000 order.
* If that order has one other vendor with $1,950 in the order, but they split the $58.30 fee evenly, that's not fair.
*
* @param [type] $vendor_id
* @param [type] $order
* @param [type] $commission
* @return void
*/
protected function get_stripe_fee_portion( $vendor_id, $order, $commission ) {
$total_stripe_fee = WC_Stripe_Helper::get_stripe_fee( $order );
$order_total = $order->get_total();
$base = $commission['tax'] + $commission['shipping']; // The Commission object already did the hard work of getting per-vendor tax/shipping.
$vendor_totals = 0;
foreach ( $order->get_items() as $item ) {
$vendor = WCV_Vendors::get_vendor_from_product( $item->get_product()->get_id() );
if ( $vendor == $vendor_id ) {
$vendor_totals += $item->get_total();
}
}
$vendor_totals += $base;
$portion = round( $total_stripe_fee * ( $vendor_totals / $order_total ), 2 );
return apply_filters( 'get_stripe_fee_portion', $portion, $vendor_id, $order, $commission );
}
/**
* Returns proper base vendor commission.
*
* Not every vendor will have equal amounts of the order - imagine one vendor selling $50 of a $2,000 order.
* If that order has one other vendor with $1,950 in the order, but they split the $58.30 fee evenly, that's not fair.
*
* @param [type] $vendor_id
* @param [type] $order
* @return void
*/
protected function calculate_base_total( $vendor_id, $order ) {
$commission = scfwc_get_seller_commission( $vendor_id ) / 100;
$order_total = $order->get_total();
$vendor_totals = 0;
foreach ( $order->get_items() as $item ) {
$vendor = WCV_Vendors::get_vendor_from_product( $item->get_product()->get_id() );
if ( $vendor == $vendor_id ) {
$vendor_totals += $item->get_total();
}
}
$portion = round( ( $commission * $vendor_totals ), 2 );
return apply_filters( 'calculate_base_seller_commission_total', $portion, $vendor_id, $order );
}
/**
* Returns vendor's portion of Stripe payout fee to pay.
*
* This amounts to 0.25% of a vendor's payout, based on the subtotal prior to Chamfr's fee, not after.
* This fee is also based on the inclusion of taxes and shipping..
*
* @param [type] $vendor_id
* @param [type] $order
* @param [type] $commission
* @return void
*/
protected function get_payout_fee( $vendor_id, $order, $commission ) {
$payout_fee = apply_filters( 'default_payout_fee_percentage', 0.0025, $vendor_id, $order, $commission );
$order_total = $order->get_total();
$base = $commission['tax'] + $commission['shipping']; // The Commission object already did the hard work of getting per-vendor tax/shipping.
$vendor_totals = 0;
foreach ( $order->get_items() as $item ) {
$vendor = WCV_Vendors::get_vendor_from_product( $item->get_product()->get_id() );
if ( $vendor == $vendor_id ) {
$vendor_totals += $item->get_total();
}
}
$vendor_totals += $base;
$total = $payout_fee * $vendor_totals;
$portion = ( floor( 100 * $total ) + floor( 100 * $total - floor( 100 * $total ) ) ) / 100;
return apply_filters( 'get_payout_fee', $portion, $vendor_id, $order, $commission );
}
protected function get_items_list( $contents ) {
$items_list = [];
foreach ( $contents as $item ) {
$items_list[] = $item['data']->get_name() . ' × ' . $item['quantity'];
}
return implode( ', ', $items_list );
}
public function split_shipping( $items, $total ){
$last_item_id = '';
$total_remaining = 0;
$new_shipping_cost = ( $total == 0 ) ? 0 : $total / count( $items );
foreach ( $items as $item_id => $details ) {
$items[ $item_id ][ 'shipping_cost' ] = number_format( $new_shipping_cost, 2 );
$last_item_id = $item_id;
$total -= number_format( $new_shipping_cost, 2 );
}
// Make sure any uneven splits are still stored correctly for commissions
$items[ $last_item_id ][ 'shipping_cost' ] += number_format( $total, 2 );
$items[ $last_item_id ][ 'shipping_cost' ] = number_format( $items[ $last_item_id ][ 'shipping_cost' ], 2 );
return apply_filters( 'wcv_split_shipping_items', $items );
}
public function prepare_items( $items, $total ) {
$items = array_map( function( $item ) {
return array(
'product_id' => $item['product_id'],
);
}, $items );
return $this->split_shipping( $items, $total ) ;
}
public function add_shipping_package_meta( $packages ) {
foreach ( $packages as $index => $package ) {
foreach ( $package['rates'] as $rate_id => $rate ) {
if ( false !== stristr( $rate_id, 'ups' ) || false !== stristr( $rate_id, 'fedex' ) ) {
$total_shipping = $rate->get_cost();
$items = $this->prepare_items( $package['contents'], $total_shipping );
$rate->add_meta_data( 'Items', $this->get_items_list( $package['contents'] ) );
$rate->add_meta_data(
'vendor_costs',
array(
'total_shipping' => $total_shipping,
'total_cost' => $package['contents_cost'],
'items' => $items
)
);
$rate->add_meta_data( 'vendor_id', $package['vendor_id'] );
}
}
}
return $packages;
}
public function filter_wcv_commission( $commission, $product_id ) {
return scfwc_get_seller_commission( WCV_Vendors::get_vendor_from_product( $product_id ) );
}
public function add_transfer_group_to_stripe_charge( $post_data, $order, $prepared_source ) {
$post_data['transfer_group'] = self::get_order_transfer_number( $order );
return $post_data;
}
public function create_payouts_to_each_seller( $response, $order ) {
$data = [
'currency' => strtoupper( get_woocommerce_currency() ),
'transfer_group' => self::get_order_transfer_number( $order ),
'source_transaction' => $response->id
];
$commissions = WCV_Vendors::get_vendor_dues_from_order( $order );
// By default, WCV assumes the admin payout to the the 1 key in this method. We employ a different model.
if ( isset( $commissions[1] ) ) {
unset( $commissions[1] );
}
// Loop through each vendor, add 'destination' of Stripe account, amount of tax + shipping + (subtotal * commission)
foreach ( $commissions as $vendor_id => $commission ) {
$acct = get_user_meta( $vendor_id, 'stripe_account_id', true );
$totals = $this->prepare_commission( $vendor_id, $order, $commission, 'processing' === $order->get_status() );
$total = $totals['total'];
if ( empty( $acct ) ) {
$order->add_order_note( sprintf( __( 'Attempted to pay out %s to %s, but they do not have their Stripe account connected.' ), $total, $vendor_name ) );
continue;
}
$vendor_name = get_user_by( 'id', $vendor_id )->display_name;
$args = array_merge( $data, [
'destination' => $acct,
'amount' => WC_Stripe_Helper::get_stripe_amount( $total ),
'description' => 'Transfer to ' . $vendor_name . ' from Order #' . $order->get_id()
]
);
$request = apply_filters( 'stripe_connect_transfer_args', $args, $response, $order );
$_response = WC_Stripe_API::request( $request, 'transfers' );
$monthly_fee = $this->maybe_process_monthly_fee( $vendor_id, $commission['total'] );
do_action( 'stripe_connect_create_seller_payout', $_response, $request, $response, $order, $vendor_id, $commission, $totals );
// TODO: Determine if we have a better way of determining success here.
if ( $_response->id ) {
if ( $monthly_fee ) {
$order->add_order_note( sprintf( __( 'Paid monthly fee of %s out of the commission (%s) to %s.' ), $monthly_fee, $total, $vendor_name ) );
update_user_meta( $vendor_id, date( 'm-Y' ) . '-chamfr-fee', array( 'transfer_id' => $_response->id, 'fee' => $monthly_fee ) );
}
$order->add_order_note( sprintf( __( 'Successfully transferred (%s) to %s. Transfer ID#: %s' ), $total, $vendor_name, $_response->id ) );
} else {
$order->add_order_note(
sprintf(
__( 'We were unable to transfer the total of %s to %s. The Stripe API returned the following error: %s. Stripe provides some documentation on this error at <a href="%s">this link</a>. You may want to check into the sellers Stripe account to confirm they have set it up properly: %s. As a precautionary measure, this error has been sent to your development team to review as well.' ),
$total,
$vendor_name,
$_response->error->message,
$_response->error->doc_url,
self::get_stripe_account_profile_url( $vendor_id )
)
);
wp_mail( '[email protected], [email protected], [email protected], [email protected]', __( 'Stripe has returned the following error. Please log in to review.' ), '<pre>' . print_r( $_response, 1 ) . '</pre>' );
}
}
}
public function record_seller_statements( $response, $request, $stripe_response, $order, $vendor_id, $commission, $totals ) {
$statement = new Seller_Statements( $vendor_id );
$order_id = $order->get_id();
if ( $totals['monthly_fee'] ) {
$args = [
'order_id' => $order_id,
'amount' => $totals['monthly_fee'],
'type' => 'monthly_fee',
'description' => __( 'Monthly Chamfr Fee' ),
'status' => 'paid',
];
$statement->add_fee( $args );
}
if ( ! empty( $commission ) ) {
if ( ! empty( $commission['tax'] ) && $commission['tax'] > 0.00 ) {
$args = [
'order_id' => $order_id,
'amount' => $commission['tax'],
'type' => 'tax',
'status' => 'pending',
];
$statement->add_fee( $args );
}
if ( ! empty( $commission['shipping'] && $commission['shipping'] > 0.00 ) ) {
$args = [
'order_id' => $order_id,
'amount' => $commission['shipping'],
'type' => 'shipping',
'status' => 'pending',
];
$statement->add_fee( $args );
}
}
if ( $totals['transactional_fee'] ) {
$args = [
'order_id' => $order_id,
'amount' => $totals['transactional_fee'] ,
'type' => 'transactional_fee',
'description' => __( 'Transactional Fee' ),
'status' => 'paid',
];
$statement->add_fee( $args );
}
if ( $totals['stripe_fee'] ) {
$args = [
'order_id' => $order_id,
'amount' => $totals['stripe_fee'] ,
'type' => 'stripe_fee',
'description' => __( 'Stripe Fee' ),
'status' => 'paid',
];
$statement->add_fee( $args );
}
if ( $totals['base'] ) {
$args = [
'order_id' => $order_id,
'amount' => $totals['base'] ,
'type' => 'commission',
'description' => __( 'Base Commission' ),
'status' => 'pending',
];
$statement->add_fee( $args );
}
}
/**
* If a vendor has not yet had their monthly membership fee
*
* @param [type] $vendor_id
* @return void
*/
public function maybe_process_monthly_fee( $vendor_id, $total ) {
$monthly_fee = scfwc_user_monthly_fee( $vendor_id );
if ( $monthly_fee >= $total ) {
return false;
}
$month_key = date( 'm-Y' ) . '-chamfr-fee';
$has_processed_monthly_fee = get_user_meta( $vendor_id, $month_key, true );
if ( ! empty( $has_processed_monthly_fee ) ) {
return false;
}
return $monthly_fee;
}
public static function get_order_transfer_number( $order ) {
return apply_filters( 'stripe_connect_transfer_group', sprintf( __( 'Order #%s' ), $order->get_id() ) );
}
public function add_connect_settings( $settings = array() ) {
$settings['connect_header'] = array(
'title' => __( 'Stripe Connect Settings', 'woocommerce-gateway-stripe' ),
'type' => 'title',
'description' => __( 'The following defaults will be applied to all seller accounts, and can be overriden at a seller level by admins in the User section.' ),
);
$settings['connect_payout_schedule_interval'] = array(
'title' => __( 'Payout Schedule', 'woocommerce-gateway-stripe' ),
'label' => __( 'Interval', 'woocommerce-gateway-stripe' ),
'type' => 'select',
'description' => __( 'Select the payout schedule you would like for sellers by default. This is how often the seller will receive eligble payouts into their account.', 'woocommerce-gateway-stripe' ),
'default' => 'monthly',
'desc_tip' => true,
'options' => array(
'daily' => __( 'Daily', 'woocommerce-gateway-stripe' ),
'weekly' => __( 'Weekly', 'woocommerce-gateway-stripe' ),
'monthly' => __( 'Monthly', 'woocommerce-gateway-stripe' ),
),
);
$settings['connect_payout_schedule_delay_days'] = array(
'title' => __( 'Payout Schedule (Delay Days)', 'woocommerce-gateway-stripe' ),
'label' => __( 'Delay Days', 'woocommerce-gateway-stripe' ),
'type' => 'text',
'description' => __( 'Daily payouts will be delayed by this many days.', 'woocommerce-gateway-stripe' ),
'default' => '30',
'desc_tip' => true,
);
$settings['connect_payout_schedule_weekly_anchor'] = array(
'title' => __( 'Payout Schedule (Weekly Anchor)', 'woocommerce-gateway-stripe' ),
'label' => __( 'Weekly Anchor', 'woocommerce-gateway-stripe' ),
'type' => 'select',
'description' => __( 'Weekly Payouts will be made on this day.', 'woocommerce-gateway-stripe' ),
'default' => 'monday',
'desc_tip' => true,
'options' => array(
'monday' => __( 'Monday', 'woocommerce-gateway-stripe' ),
'tuesday' => __( 'Tuesday', 'woocommerce-gateway-stripe' ),
'wednesday' => __( 'Wednesday', 'woocommerce-gateway-stripe' ),
'thursday' => __( 'Thursday', 'woocommerce-gateway-stripe' ),
'friday' => __( 'Friday', 'woocommerce-gateway-stripe' ),
),
);
$settings['connect_payout_schedule_monthly_anchor'] = array(
'title' => __( 'Payout Schedule (Monthly Anchor)', 'woocommerce-gateway-stripe' ),
'label' => __( 'Monthly Anchor', 'woocommerce-gateway-stripe' ),
'type' => 'text',
'description' => __( 'Monthly payouts will be made on this day of the month.', 'woocommerce-gateway-stripe' ),
'default' => '5',
'desc_tip' => true,
);
$settings['connect_default_commission'] = array(
'title' => __( 'Payout Schedule (Default Commission)', 'woocommerce-gateway-stripe' ),
'label' => __( 'Default Commission', 'woocommerce-gateway-stripe' ),
'type' => 'text',
'description' => __( 'This is the default amount sellers will receive, plus taxes and shipping.', 'woocommerce-gateway-stripe' ),
'default' => '88',
'desc_tip' => true,
);
$settings['monthly_fee'] = array(
'title' => __( 'Monthly Fee (Active)', 'woocommerce-gateway-stripe' ),
'label' => __( 'Default Monthly Fee', 'woocommerce-gateway-stripe' ),
'type' => 'text',
'description' => __( 'This is the default monthly fee that sellers are charged in any month they have a payout.', 'woocommerce-gateway-stripe' ),
'default' => '4.25',
'desc_tip' => true,
);
/*
$settings['passive_monthly_fee'] = array(
'title' => __( 'Monthly Fee (Global)', 'woocommerce-gateway-stripe' ),
'label' => __( 'Default Monthly Fee', 'woocommerce-gateway-stripe' ),
'type' => 'text',
'description' => __( 'This is the default monthly fee that sellers are charged regardless of any payout.', 'woocommerce-gateway-stripe' ),
'default' => '99.00',
'desc_tip' => true,
);
$settings['passive_monthly_fee_date'] = array(
'title' => __( 'Monthly Fee Date (Global)', 'woocommerce-gateway-stripe' ),
'label' => __( 'Default Monthly Fee Date', 'woocommerce-gateway-stripe' ),
'type' => 'text',
'description' => __( 'This is the default day of the month on which the monthly fee will be debited.', 'woocommerce-gateway-stripe' ),
'default' => '15',
'desc_tip' => true,
);
*/
return $settings;
}
public function maybe_show_stripe_button( $template ) {
if ( 'report.php' !== $template ) {
return;
}
include STRIPE_CONNECT_WC_INC . 'dashboard-stripe.php';
}
public function add_stripe_id_column( $column ) {
$column['stripe_account_id'] = __( 'Stripe Account ID' );
return $column;
}
public static function get_stripe_account_profile_url( $user_id ) {
$options = get_option( 'woocommerce_stripe_settings' );
$testmode = ( isset( $options['testmode'] ) && 'yes' === $options['testmode'] ) ? true : false;
$test = $testmode ? 'test/' : '';
$acct = get_user_meta( $user_id, 'stripe_account_id', true );
$link = "https://dashboard.stripe.com/{$test}applications/users/$acct";
return '<a target ="_blank" href="' . esc_url( $link ) . '">' . esc_html( $acct ) . '</a>';
}
public function render_stripe_id_column_data( $val, $column_name, $user_id ) {
switch ( $column_name ) {
case 'stripe_account_id' :
return self::get_stripe_account_profile_url( $user_id );
break;
default:
}
return $val;
}
public function add_stripe_connect_fields_to_profile( $user ) {
// Iterate through add_connect_settings()
?>
<h3><?php _e( 'Stripe Connect Settings' ); ?></h3>
<table class="form-table">
<?php
foreach ( $this->add_connect_settings() as $key => $data ) :
if ( ! in_array( $data['type'], array( 'text', 'select' ), true ) ) {
continue;
}
?>
<tr>
<th>
<label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $data['title'] ); ?></label>
</th>
<td>
<?php $this->generate_input( $key, $data, $user ); ?>
</td>
</tr>
<?php endforeach; ?>
<tr>
<th>
<label for="stripe_account_id"><?php echo esc_html( 'Stripe Account ID' ); ?></label>
</th>
<td>
<input type="text" class="regular-text ltr" id="stripe_account_id" name="stripe_account_id" value="<?php esc_attr_e( $user->stripe_account_id ); ?>"
title="<?php esc_attr_e( $data['title'] ); ?>">
</td>
</tr>
</table>
<?php
}
protected function generate_input( $key, $data, $user ) {
$wc_stripe_settings = get_option( 'woocommerce_stripe_settings', array() );
$default = empty( $wc_stripe_settings->{$key} ) ? $data['default'] : $wc_stripe_settings->{$key};
$value = empty( $user->{$key} ) ? $default : $user->{$key};
if ( 'text' === $data['type'] ) { ?>
<input type="text" class="regular-text ltr" id="<?php esc_attr_e( $key ); ?>" name="<?php esc_attr_e( $key ); ?>" value="<?php esc_attr_e( $value ); ?>"
title="<?php esc_attr_e( $data['title'] ); ?>">
<?php if ( ! empty( $data['description'] ) ) : ?>
<p class="description"><?php echo esc_html( $data['description'] ); ?></p>
<?php endif; ?>
<?php
}
if ( 'select' === $data['type'] ) { ?>
<select id="<?php esc_attr_e( $key ); ?>" name="<?php esc_attr_e( $key ); ?>" title="<?php esc_attr_e( $data['title'] ); ?>">
<?php foreach ( $data['options'] as $name => $val ) : ?>
<option value="<?php esc_attr_e( $name ); ?>" <?php selected( $name, $value, true ); ?>><?php echo esc_html( $val ); ?></option>
<?php endforeach; ?>
</select>
<?php if ( ! empty( $data['description'] ) ) : ?>
<p class="description"><?php echo esc_html( $data['description'] ); ?></p>
<?php endif; ?>
<?php
}
}
/**
* The save action.
*
* @param $user_id int the ID of the current user.
* @return bool Meta ID if the key didn't exist, true on successful update, false on failure.
*/
public function add_stripe_connect_fields_to_usermeta( $user_id ) {
// check that the current user have the capability to edit the $user_id
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
// Prepend stripe_account_id to the normal connect settings in order to save it effectively.
foreach ( array_merge( array( 'stripe_account_id' => array() ), $this->add_connect_settings() ) as $key => $data ) {
if ( isset( $_POST[ $key ] ) ) {
// create/update user meta for the $user_id
update_user_meta( $user_id, $key, sanitize_text_field( $_POST[ $key ] ) );
} else {
delete_user_meta( $user_id, $key );
}
}
$account_id = get_user_meta( $user_id, 'stripe_account_id', true );
if ( WCV_Vendors::is_vendor( $user_id ) && ! empty( $account_id ) ) {
scfwc_update_user_payout_schedule( $user_id );
}
}