further implementation work for stripe integration

This commit is contained in:
Nathan Coad
2016-07-22 09:01:39 +10:00
parent 6172866bf4
commit c0d2e11172
6 changed files with 188 additions and 221 deletions

View File

@@ -4,6 +4,7 @@
* @file
* Functions for stripe payment integration
* @see https://github.com/ericthelast/drupal-stripe-form and https://www.webomelette.com/drupal-stripe-integration
* Australian test number is 4000000360000006
*/
/**
@@ -43,9 +44,6 @@ function booking_stripe_form($node, &$form_state, $person, $invoiceid, $amount_o
$settings = array();
$form = array();
//get our current path so we can send the user back here if they cancel
//$path = isset($_GET['q']) ? $_GET['q'] : '<front>';
//set some values for our internal stripe library to help process the form
//these will be used by the attached js from booking-strip library to identify which parts of the form to process
@@ -116,116 +114,6 @@ function booking_stripe_form($node, &$form_state, $person, $invoiceid, $amount_o
return $form;
}
/*
function booking_stripe_form($form, &$form_state, $person, $invoiceid, $amount_owing, $button_text) {
global $event;
$setting = array();
$form = array();
// Try to load the library and check if that worked.
if (($library = libraries_load('stripe')) && !empty($library['loaded'])) {
// Do something with the library here.
}
else {
//library can be downloaded from https://stripe.com/docs/libraries
form_set_error('form', t('The required stripe library is not installed. Please contact your site adaministrator'));
}
//set some values for our internal stripe library to help process the form
//these will be used by the attached js from booking-strip library to identify which parts of the form to process
$setting['booking_stripeform'] = array(
'pubkey' => _booking_get_stripe_public_key(),
'form_selector' => str_replace('_', '-', __FUNCTION__),
);
$form['#attached'] = array(
'js' => array(
array('data' => $setting, 'type' => 'setting'),
),
'library' => array(
array('booking', 'booking-stripe'),
),
);
$form['stripeToken'] = array(
'#type' => 'hidden',
'#value' => !empty($form_state['input']['stripeToken']) ? $form_state['input']['stripeToken'] : NULL,
);
$form['amount'] = array(
'#type' => 'hidden',
'#value' => $amount_owing,
);
$form['credit_card'] = array(
'#type' => 'fieldset',
'#title' => t('Credit Card Information'),
'#description' => t('<p>This credit card information is securely processed via <a href="https://stripe.com">stripe.com</a>. No credit card information is stored or processed on our server.</p>');
);
$cc = &$form['credit_card'];
$cc['card_number'] = array(
'#type' => 'textfield',
'#title' => t('Credit Card Number'),
'#pre_render' => array('booking_stripeform_remove_name'),
'#attributes' => array(
'size' => 20,
'data-stripe' => 'number',
),
);
$cc['exp_month'] = array(
'#type' => 'select',
'#title' => t('Expiration Month'),
'#options' => drupal_map_assoc(array(1,2,3,4,5,6,7,8,9,10,11,12)),
'#pre_render' => array('booking_stripeform_remove_name'),
'#attributes' => array(
'data-stripe' => 'exp-month',
),
'#empty_option' => t('- Select -'),
);
$cc['exp_year'] = array(
'#type' => 'select',
'#title' => t('Expiration Year'),
'#options' => array(),
'#pre_render' => array('booking_stripeform_remove_name'),
'#attributes' => array(
'data-stripe' => 'exp-year',
),
'#empty_option' => t('- Select -'),
);
$year = date('Y');
for($i = $year; $i <= ($year + 10); $i++) {
$cc['exp_year']['#options'][$i] = $i;
}
$cc['cvc'] = array(
'#type' => 'textfield',
'#title' => t('CVC Number'),
'#pre_render' => array('booking_stripeform_remove_name'),
'#attributes' => array(
'size' => 4,
'data-stripe' => 'cvc',
),
);
$cc['submit'] = array(
'#type' => 'submit',
'#value' => t('Charge It'),
'#attributes' => array(
'class' => array('btn', 'btn-large', 'btn-primary'),
),
);
// Adds our validation at the end of the build process.
$form['#after_build'][] = 'booking_stripe_add_final_validation';
return $form;
}
*/
/**
* Tries to add final validation after all else has been added through alters.
*/
@@ -267,6 +155,8 @@ function booking_stripe_validate_form_payment($form, &$form_state) {
$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'] : '');
$last_name = (isset($form_state['input']['last_name']) ? $form_state['input']['last_name'] : '');
$first_name = (isset($form_state['input']['first_name']) ? $form_state['input']['first_name'] : '');
watchdog('booking_debug', "<pre>Stripe payment form :\n@info</pre>", array('@info' => print_r( $form_state, true)));
// Create the charge on Stripe's servers - this will charge the user's card
@@ -282,12 +172,15 @@ function booking_stripe_validate_form_payment($form, &$form_state) {
"metadata" => array(
"invoice" => $invoice,
"nid" => $nid,
"last_name" => $last_name,
"first_name" => $first_name,
),
));
watchdog('booking_debug', "<pre>Stripe payment charge results:\n@info</pre>", array('@info' => print_r( $charge, true)));
if ($charge && $charge->paid) {
watchdog('booking', 'Charge created successfully');
$form_state['stripeform_charge'] = $charge;
_booking_process_stripe_payment($charge);
//$form_state['stripeform_charge'] = $charge;
// @todo call _booking_process_stripe_payment to store payment
drupal_goto('bookingfinal/' . $tempid);
}
@@ -327,4 +220,89 @@ function booking_stripeform_form_submit($form, &$form_state) {
function booking_stripeform_remove_name($element) {
unset($element['#name']);
return $element;
}
function _booking_process_stripe_payment($charge) {
global $event;
$balance_payment = false;
$amount_owing = 0;
//$invoice = $data->metadata;
//verify the status of the charge
if (empty($charge->status) || ($charge->status != 'succeeded')) {
$successful = FALSE;
}
else {
$successful = TRUE;
}
//extract the person node id from the invoice
$pos = strpos($charge->metadata->invoice, "_");
if (($pos === false) || ($pos == 0)) {
watchdog('booking', 'Unable to process payment with invalid invoice information: !id', array('!id' => $charge->metadata->invoice), WATCHDOG_ERROR);
return;
}
//get the part of the invoice up to the first underscore
$nid = substr($charge->metadata->invoice, 0, $pos);
//get the data between the first and second underscore
$eid = substr($charge->metadata->invoice, $pos + 1, strrpos($charge->metadata->invoice, "_") - $pos - 1);
if (substr($eid,0,3) == "bal") {
$balance_payment = true;
watchdog('booking_debug', 'Balance payment via stripe for user with node id: !id and status !status.',
array('!id' => $nid, '!status' => $charge->status));
}
else {
watchdog('booking_debug', 'Initial payment via stripe for user with node id: !id and status !status.',
array('!id' => $nid, '!status' => $charge->status));
}
//this shouldn't ever happen, since stripe is sending this notification synchronously
//but just in case, check for an existing transaction that matches this one
$duplicate_check = db_query("SELECT payid, booking_person_nid FROM {booking_payment} where booking_ipn_track_id = :ipn_id ",
array(':ipn_id' => $charge->id))->fetchObject();
if ($duplicate_check) {
watchdog('booking', 'Detected duplicate stripe transaction notifications for transaction id !id, registration id !nid',
array('!id' => $charge->id, '!nid' => $nid), WATCHDOG_ERROR);
return;
}
$gross_amount = $charge->amount / 100;
$result = db_insert('booking_payment')
->fields(array(
'booking_person_nid' => $nid,
'booking_eventid' => $event->eid,
'booking_mc_gross' => $gross_amount,
'booking_mc_currency' => $charge->balance_transaction->currency,
'booking_mc_fee' => $charge->balance_transaction->fee / 100,
'booking_invoice' => $charge->metadata->invoice,
'booking_payer_id' => $charge->source->id,
'booking_payment_date' => $charge->created,
'booking_payment_status' => $charge->status,
'booking_first_name' => $charge->metadata->first_name,
'booking_last_name' => $charge->metadata->last_name,
'booking_buyer_email' => $charge->receipt_email,
//'booking_payer_status' => $data['payer_status'],
'booking_item_name' => $data->description,
'booking_ipn_track_id' => $data->id,
))
->execute();
//Get the person's info so we can update their total amount paid and booking status
$person = node_load($nid);
//check if we found a person matching this payment
if ($person) {
watchdog('booking', 'Found matching user with node id: !id; event id: !eid; existing payment !payment',
array('!id' => $nid, '!eid' => $eid, '!payment' => $person->booking_amount_paid));
_booking_process_payment($person, $gross_amount, $balance_payment);
}
else {
//couldn't find a matching nid for this invoice
watchdog('booking', "Unable to process payment for user with node id: '!id'", array('!id' => $nid), WATCHDOG_ERROR);
//db_query("UPDATE {booking_person} SET booking_tempid='' WHERE nid = %d", $nid);
}
}