· 9 years ago · Oct 27, 2016, 12:58 PM
1add_action( 'wp_ajax_action_varaa_AJAX', 'varaa_AJAX' );
2add_action('wp_ajax_nopriv_varaa_AJAX', 'varaa_AJAX');
3
4function varaa_AJAX(){
5 global $chargeID;
6 global $bookingStatus;
7
8 $amount = $_POST['amount'];
9 $token = $_POST['stripeToken'];
10 $name = $_POST['name'];
11 $cardName = $_POST['cardName'];
12 $email = $_POST['email'];
13 $phone = $_POST['phone'];
14 $accept_terms = $_POST['accept'];
15 $insurance = $_POST['insurance'];
16 $extradates = $_POST['extradates'];
17 $id = $_POST['session_id'];
18 $form = array(
19 'customer_name' => $name,
20 'customer_email' => $email,
21 'customer_phone' => $phone,
22 'accept_terms' => $accept_terms
23 );
24
25 function make_payment($token, $name, $cardName, $email, $phone, $amount, $insurance, $extradates) {
26 require_once(get_template_directory() . 'vendor/stripe/stripe-php/init.php');
27 global $chargeID;
28
29 $stripe_settings = get_option('stripe_settings');
30
31 // Set your secret key: remember to change this to your live secret key in production
32 // See your keys here: https://dashboard.stripe.com/account/apikeys
33 if(isset($stripe_settings['test_mode']) && $stripe_settings['test_mode']) {
34 $secret_key = $stripe_settings['test_secret_key'];
35 } else {
36 $secret_key = $stripe_settings['live_secret_key'];
37 }
38
39 StripeStripe::setApiKey($secret_key);
40
41 // ANONYYMI VARAUS
42
43 // Create customer and assign charge with $token
44 $customer = StripeCustomer::create(array(
45 "source" => $token,
46 "email" => $email,
47 "description" => "Uusi anonyymi varaus.",
48 "metadata" => [
49 "nimi" => $name,
50 "kortissa oleva nimi" => $cardName,
51 "puhelin" => $phone,
52 "insurance" => $insurance,
53 "extradates" => $extradates
54 ]
55 ));
56
57 // Get customer id from create above
58 $stripeID = $customer->id;
59
60 // Charge customer
61 $charge = StripeCharge::create(array(
62 "amount" => $amount, // Amount in cents
63 "currency" => "eur",
64 "capture" => false,
65 "customer" => $stripeID
66 ));
67
68 // Get charge id from charge above
69 $chargeID = $charge->id;
70 }
71
72 // Create booking
73 function create_booking($id, $form){
74 require_once(get_template_directory() . 'lib/booking.php');
75 require_once(get_template_directory() . 'lib/form.php');
76 global $bookingStatus;
77 global $bookingId;
78
79 $Booking = new Booking();
80
81 $bookIt = $Booking->create(
82 array(
83 'session_id' => $id,
84 'form' => $form
85 )
86 );
87
88 $bookingStatus = $bookIt['request']['status'];
89
90 $bookingId = $bookIt['booking']['booking_id'];
91
92 echo $bookIt;
93
94 }
95
96// Run functions above
97
98 // Check WP nonce
99 if (
100 ! isset( $_POST['nonce'] )
101 || ! wp_verify_nonce( $_POST['nonce'], 'bookNow' )
102 ) {
103
104 print 'Sorry, your nonce did not verify.';
105 exit;
106
107 } else {
108 // Create a customer and charge card
109 try {
110 // Create charge
111 make_payment($token, $name, $cardName, $email, $phone, $amount, $insurance, $extradates);
112
113 // Create booking
114 create_booking($id, $form);
115
116 // Check if booking went through
117 if ( $bookingStatus = 'OK' ) {
118 // Capture charge
119 $ch = StripeCharge::retrieve( $chargeID );
120 $ch->capture();
121
122 // Booking success
123 $value = array('msg' => 'success', 'bookingStatus' => $bookingStatus, 'bookingID' => $bookingId );
124 echo json_encode($value);
125
126 } else {
127 // Refund charge
128 $re = StripeRefund::create(array(
129 "charge" => $chargeID
130 ));
131
132 $value = array('msg' => 'error' );
133 echo json_encode($value);
134 }
135
136 } catch(StripeErrorCard $e) {
137 // The card has been declined
138 $value = array('error' => true); // or $result = array('error' => false);
139 echo json_encode($value);
140 }
141 }
142 die();
143}
144
145var token = response.id,
146 amount = vm.total * 100,
147 name = vm.form.name,
148 cardName = vm.form.cardName,
149 phone = vm.form.phone,
150 email = vm.form.email,
151 terms = vm.form.terms,
152 insurance = vm.form.insurance,
153 action = 'varaa_AJAX',
154 id = vm.sessionId;
155
156 var nonce = $('#ilmtrail_nonce').val();
157 var extradates = Object.keys( vm.daysBetweenStartStop ).length * 25 - 25;
158
159 jQuery.ajax({
160 url: AJAXurl,
161 data: { action: action, nonce: nonce, stripeToken: token, insurance: insurance, extradates: extradates, name: name, phone: phone, email: email, cardName: cardName, amount: amount, accept: terms, session_id: id },
162 type: 'post',
163 success: function(value) {
164 if ( value.msg === 'success' ) {
165
166 console.log(value);
167 vm.form.sending = false;
168 vm.form.sent = true;
169 $('#receipt').openModal();
170 return true;
171
172 } else{
173
174 alert ('Jokin meni mönkään. Tarkista tiedot ja kokeile uudestaan, ja jos virhe toistuu ole yhteydessä meihin niin saadaan varaus läpi!');
175 console.log(value);
176 vm.form.sending = false;
177 $form.find('.submit').prop('disabled', false);
178 return false;
179
180 }
181
182 },
183 error: function(data) {
184 vm.form.sending = false;
185 $form.find('.submit').prop('disabled', false);
186 console.log(data);
187 alert ('Jokin meni mönkään. Tarkista tiedot ja kokeile uudestaan, ja jos virhe toistuu ole yhteydessä meihin niin saadaan varaus läpi!');
188 return false;
189 }
190 });