diff --git a/booking.balance.inc b/booking.balance.inc index 4d6f687..a255c37 100644 --- a/booking.balance.inc +++ b/booking.balance.inc @@ -47,7 +47,7 @@ function booking_balance_page() { $tokens = booking_define_personspecific_tokens($node); // @todo - use admin variable booking_payment_processor to determine which of these to calculate $tokens['paypal-total-form'] = _booking_paypal_form($node, $invoiceid, $tokens['paypal-total-amount'], "Pay Balance"); - $tokens['stripe-total-form'] = _booking_stripe_form($node, $invoiceid, $tokens['paypal-total-amount'], "Pay Balance"); + $tokens['stripe-total-form'] = _booking_stripe_form($node, $invoiceid, $tokens['payment-required'], "Pay Balance"); //Calculate the amount outstanding //watchdog('booking', 'Booking Balance form calculating amount owing'); diff --git a/booking.stripe.inc b/booking.stripe.inc index 2b97eb8..03962db 100644 --- a/booking.stripe.inc +++ b/booking.stripe.inc @@ -34,12 +34,12 @@ function _booking_get_stripe_private_key() { /** * Helper function to generate paypal form for payments */ -function _booking_stripe_form($person, $invoiceid, $amount_owing, $button_text) { +function _booking_stripe_form($person, $invoiceid, $net_amount_owing, $button_text) { $payment_form = drupal_get_form('booking_stripe_form', $person, $invoiceid, $amount_owing, $button_text); return drupal_render($payment_form); } -function booking_stripe_form($node, &$form_state, $person, $invoiceid, $amount_owing, $button_text) { +function booking_stripe_form($node, &$form_state, $person, $invoiceid, $net_amount_owing, $button_text) { global $event; $settings = array(); $form = array(); @@ -51,7 +51,7 @@ function booking_stripe_form($node, &$form_state, $person, $invoiceid, $amount_o $setting['booking_stripe'] = array( 'pubkey' => _booking_get_stripe_public_key(), 'form_selector' => str_replace('_', '-', __FUNCTION__), - 'name' => 'Study Week', + 'name' => $event->booking_eventname, 'image' => '', ); //attach settings and javascript to the form @@ -63,38 +63,27 @@ function booking_stripe_form($node, &$form_state, $person, $invoiceid, $amount_o array('booking', 'booking-stripe'), ), ); - //paypal specific settings $vars = array( - 'module' => 'Booking System', - 'type' => $event->booking_eventname, 'nid' => $person->nid, 'email' => $person->booking_email, 'description' => $event->booking_eventname . ' ' . $person->booking_price_descrip, 'invoice' => $invoiceid, - 'amount' => $amount_owing, + 'amount' => $net_amount_owing, + 'gross_amount' => _booking_add_stripe_fees($net_amount_owing, $person->booking_country), + 'foreign_gross_amount' => _booking_add_stripe_fees($net_amount_owing, 'FakeCountry'), 'last_name' => $person->booking_lastname, 'first_name' => $person->booking_firstname, 'uuid' => $person->booking_tempid, 'token_id' => '', - 'token_email' => '' + 'token_email' => '', + 'token_client_ip' => '', + 'card_brand' => '', + 'card_cvc_check' => '', + 'card_address_zip_check' => '', ); - /* - $form['#id'] = 'booking_stripe_form'; - $form['#attached']['js'] = array( - array( - 'data' => 'https://checkout.stripe.com/checkout.js', - 'type' => 'external', - ), - array( - 'data' => drupal_get_path('module', 'booking') . '/booking.stripe.js', - 'type' => 'file', - ), - ); - */ - //turn the array into a form foreach($vars as $name => $value) { $form[$name] = array( @@ -139,19 +128,19 @@ function booking_stripe_checkout_form_validate($form, &$form_state) { */ function booking_stripe_validate_form_payment($form, &$form_state) { global $event; + if($errors = form_get_errors()) { + //@todo log an error via watchdog return; } $path = libraries_get_path('stripe'); require_once($path . '/init.php'); - \Stripe\Stripe::setApiKey(_booking_get_stripe_private_key()); - //$token = $form_state['values']['stripeToken']; - //$amount = $form_state['values']['amount'] * 100; + //get values from original form $token = (isset($form_state['input']['token_id']) ? $form_state['input']['token_id'] : ''); - $amount = (isset($form_state['input']['amount']) ? $form_state['input']['amount'] : ''); + $amount = (isset($form_state['input']['gross_amount']) ? $form_state['input']['gross_amount'] : ''); $invoice = (isset($form_state['input']['invoice']) ? $form_state['input']['invoice'] : ''); $nid = (isset($form_state['input']['nid']) ? $form_state['input']['nid'] : ''); $tempid= (isset($form_state['input']['uuid']) ? $form_state['input']['uuid'] : ''); @@ -159,6 +148,16 @@ function booking_stripe_validate_form_payment($form, &$form_state) { $first_name = (isset($form_state['input']['first_name']) ? $form_state['input']['first_name'] : ''); watchdog('booking_debug', "
Stripe token:\n@info
", array('@info' => print_r( $token, true))); + //if card type is american express then the rate charged should be different + $card_type = (isset($form_state['input']['card_brand']) ? $form_state['input']['card_brand'] : ''); + if ($card_type == 'American Express') { + //amount charged should be the international rate not the domestic rate + //@todo verify that nid is correct format + //@todo debug log to indicate price change + $person = node_load($nid); + $amount = (isset($form_state['input']['foreign_gross_amount']) ? $form_state['input']['foreign_gross_amount'] : $amount); + } + // Create the charge on Stripe's servers - this will charge the user's card try { $charge = \Stripe\Charge::create(array( diff --git a/booking.stripe.js b/booking.stripe.js index 5b35051..a7502b6 100644 --- a/booking.stripe.js +++ b/booking.stripe.js @@ -1,16 +1,19 @@ jQuery(document).ready(function($) { - var settings = Drupal.settings.booking_stripe; - var $stripeForm = $("#" + settings.form_selector); + var $settings = Drupal.settings.booking_stripe; + var $stripeForm = $("#" + $settings.form_selector); var handler = StripeCheckout.configure({ key: settings.pubkey, image: settings.image, locale: 'auto', token: function(token) { try { - // Use the token to create the charge with a server-side script. - // You can access the token ID with `token.id` + //store the returned token into hidden form elements $(':input[name="token_id"]', $stripeForm).val(token.id); $(':input[name="token_email"]', $stripeForm).val(token.email); + $(':input[name="token_client_ip"]', $stripeForm).val(token.client_ip); + $(':input[name="card_brand"]', $stripeForm).val(token.card.brand); + $(':input[name="card_cvc_check"]', $stripeForm).val(token.card.cvc_check); + $(':input[name="card_address_zip_check"]', $stripeForm).val(token.card.address_zip_check); $stripeForm.get(0).submit(); } catch(err) { @@ -24,8 +27,8 @@ jQuery(document).ready(function($) { //currentForm = $(this).closest('form'); //if (currentForm === undefined) // return; - var settings = Drupal.settings.booking_stripe; - var $stripeForm = $("#" + settings.form_selector); + var $settings = Drupal.settings.booking_stripe; + var $stripeForm = $("#" + $settings.form_selector); //$description = $(':input[name="description"]', $stripeForm).val(); //console.log([$description]); //description: currentForm.find('input[name="description"]').val(),