· 6 years ago · Oct 07, 2019, 10:20 AM
1if($charge_admin_fee && $pay_booking_amount_to == 'admin' && (($admin_fee_type == 'fixed' && $admin_fee_fixed > 0) || ($admin_fee_type == 'percentage' && $admin_fee_percentage > 0)) && $charge_admin_fee_from == 'customer'){
2
3 if($admin_fee_type == 'fixed'){
4
5 $adminfee = $admin_fee_fixed;
6
7 }elseif($admin_fee_type == 'percentage'){
8
9 $adminfee = $totalcost * ($admin_fee_percentage/100);
10
11 }
12
13
14
15 $destinationamount = $totalcost;
16
17
18
19 $totalcost = $totalcost + $adminfee;
20
21 }elseif($charge_admin_fee && $pay_booking_amount_to == 'admin' && (($admin_fee_type == 'fixed' && $admin_fee_fixed > 0) || ($admin_fee_type == 'percentage' && $admin_fee_percentage > 0)) && $charge_admin_fee_from == 'provider'){
22
23
24// I'm charging 4%
25 $adminfee = $totalcost * ($admin_fee_percentage/100);
26 $destinationamount = $totalcost - $adminfee;
27
28 if($pay_booking_amount_to == 'admin'){
29 // THIS IS THE OPTION FOR SPLIT PAYMENTS FOR CONNECT -- use my secret key
30 $stripetype = (!empty($service_finder_options['stripe-type'])) ? esc_html($service_finder_options['stripe-type']) : '';
31 $secret_key = (!empty($service_finder_options['stripe-live-secret-key'])) ? esc_html($service_finder_options['stripe-live-secret-key']) : '';
32
33 }elseif($pay_booking_amount_to == 'provider'){
34 $secret_key = esc_html($settings['stripesecretkey']);
35
36 }
37
38 \Stripe\Stripe::setApiKey($secret_key);
39
40 // BUILD ARRAY TO CHARGE CARD. (works)
41
42 try {
43 $customer = \Stripe\Customer::create(array(
44 'card' => $token,
45 'email' => $_POST['email'],
46 'description' => "Provider booked by ".$_POST['firstname']." ".$_POST['lastname']
47 )
48 );
49
50
51// GET ACCOUNT ID OF PROVIDER ON WHOSE BEHALF WE'RE CHARGING
52 $acct_id = get_user_meta($providerid,'stripe_connect_custom_account_id',true);
53 // we should have the account ID at this point, so this should follow.
54
55 if($acct_id != "" && $destinationamount > 0){
56 $destinationamount = $destinationamount * 100;
57 $charge = \Stripe\Charge::create(array(
58 "amount" => $totalcost,
59 "currency" => strtolower(service_finder_currencycode()),
60 "customer" => $customer->id, // obtained with Stripe.js
61 "description" => "Charge for Booking",
62 "destination" => array(
63 "amount" => $destinationamount,
64 "account" => $acct_id,
65 )
66 ));
67
68 }else{ // SOMEHOW THERE'S NO PROVIDER ACCT_ID.
69 $charge = \Stripe\Charge::create(array(
70 "amount" => $totalcost,
71 "currency" => strtolower(service_finder_currencycode()),
72 "customer" => $customer->id, // obtained with Stripe.js
73 "description" => "Charge for Booking",
74 ));
75
76 }
77
78
79// This is the rest of what happens AFTER the charge succeeds....
80 if ($charge->paid == true && $charge->status == "succeeded") {
81 $saveBooking = new SERVICE_FINDER_BookNow();
82 $saveBooking->service_finder_SaveBooking($_POST,$customer->id,$charge->balance_transaction,$adminfee);
83 $senMail = new SERVICE_FINDER_BookNow();
84 $bookingdata = $wpdb->get_row($wpdb->prepare('SELECT * FROM '.$service_finder_Tables->bookings.' WHERE `stripe_customer_id` = "%s"',$customer->id),ARRAY_A);
85 $senMail->service_finder_SendBookingMailToProvider($bookingdata,'',$adminfee);
86 $senMail->service_finder_SendBookingMailToCustomer($bookingdata,'',$adminfee);
87 $senMail->service_finder_SendBookingMailToAdmin($bookingdata,'',$adminfee);
88 $redirectOption = $service_finder_options['redirect-option'];
89 $redirectURL = (!empty($service_finder_options['thankyou-page-url'])) ? $service_finder_options['thankyou-page-url'] : '';
90 if($redirectOption == 'thankyou-page'){
91 if($redirectURL != ""){
92 $url = $redirectURL.'?bookingcompleted=success';
93 }else{
94 $url = service_finder_get_url_by_shortcode('[service_finder_thank_you]').'?bookingcompleted=success';
95 }
96 }else{
97 $url = '';
98 }