321 lines
9.4 KiB
PHP
321 lines
9.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Functions for stripe payment integration
|
|
* @see https://github.com/ericthelast/drupal-stripe-form and https://www.webomelette.com/drupal-stripe-integration
|
|
*/
|
|
|
|
/**
|
|
* Get the current stripe api public key
|
|
*/
|
|
function _booking_get_stripe_public_key() {
|
|
if (variable_get('booking_stripe_testmode', 0) == 1) {
|
|
return variable_get('booking_stripe_test_public_key', '');
|
|
}
|
|
else {
|
|
return variable_get('booking_stripe_live_public_key', '');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the current stripe api private key
|
|
*/
|
|
function _booking_get_stripe_private_key() {
|
|
if (variable_get('booking_stripe_testmode', 0) == 1) {
|
|
return variable_get('booking_stripe_test_secret_key', '');
|
|
}
|
|
else {
|
|
return variable_get('booking_stripe_live_secret_key', '');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Helper function to generate paypal form for payments
|
|
*/
|
|
function _booking_stripe_form($person, $invoiceid, $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) {
|
|
global $event;
|
|
$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
|
|
|
|
// @todo set name and image in the admin page
|
|
$setting['booking_stripe'] = array(
|
|
'pubkey' => _booking_get_stripe_public_key(),
|
|
'form_selector' => str_replace('_', '-', __FUNCTION__),
|
|
'name' => 'Study Week',
|
|
'image' => '',
|
|
);
|
|
//attach settings and javascript to the form
|
|
$form['#attached'] = array(
|
|
'js' => array(
|
|
array('data' => $setting, 'type' => 'setting'),
|
|
),
|
|
'library' => array(
|
|
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,
|
|
'last_name' => $person->booking_lastname,
|
|
'first_name' => $person->booking_firstname,
|
|
'token_id' => '',
|
|
'token_email' => ''
|
|
);
|
|
|
|
/*
|
|
$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(
|
|
'#type' => 'hidden',
|
|
'#value' => $value,
|
|
);
|
|
};
|
|
|
|
$form['submit'] = array(
|
|
'#id' => 'stripe-submit',
|
|
'#type' => 'button',
|
|
'#value' => t($button_text),
|
|
);
|
|
$form['#after_build'][] = 'booking_stripe_add_final_validation';
|
|
|
|
//watchdog('booking', 'Booking Balance payment: @info', array ('@info' => var_export($form, TRUE)));
|
|
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.
|
|
*/
|
|
function booking_stripe_add_final_validation($form) {
|
|
$form['#validate'][] = 'booking_stripe_validate_form_payment';
|
|
return $form;
|
|
}
|
|
|
|
/**
|
|
* Form validation callback.
|
|
*/
|
|
function booking_stripe_checkout_form_validate($form, &$form_state) {
|
|
// Validate normal form elements as needed.
|
|
}
|
|
|
|
/**
|
|
* Processes the stripe payment.
|
|
*
|
|
* We do this here so that if the payment fails,
|
|
* we're still in a validation stage and can return
|
|
* early. If success, we'll pass the charge on
|
|
* to the submission callback.
|
|
*/
|
|
function booking_stripe_validate_form_payment($form, &$form_state) {
|
|
global $event;
|
|
if($errors = form_get_errors()) {
|
|
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;
|
|
$token = (isset($form_state['input']['token_id']) ? $form_state['input']['token_id'] : '');
|
|
$amount = (isset($form_state['input']['amount']) ? $form_state['input']['amount'] : '');
|
|
$invoice = (isset($form_state['input']['invoice']) ? $form_state['input']['invoice'] : '');
|
|
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
|
|
try {
|
|
$charge = \Stripe\Charge::create(array(
|
|
"amount" => $amount * 100,
|
|
"currency" => "aud",
|
|
"card" => $token,
|
|
"description" => $form_state['input']['description'],
|
|
"receipt_email" => $form_state['input']['email'],
|
|
"metadata" => array(
|
|
"invoice" => $invoice,
|
|
),
|
|
));
|
|
if ($charge && $charge->paid) {
|
|
watchdog('booking', 'Charge created successfully');
|
|
$form_state['stripeform_charge'] = $charge;
|
|
watchdog('booking_debug', "<pre>Stripe payment charge results:\n@info</pre>", array('@info' => print_r( $charge, true)));
|
|
drupal_goto('bookingfinal');
|
|
}
|
|
else {
|
|
drupal_set_message('Card does not seem to have been charged successfully. Please try again', 'error');
|
|
}
|
|
}
|
|
catch(\Stripe\Error\Card $e) {
|
|
$e_json = $e->getJsonBody();
|
|
$error = $e_json['error'];
|
|
watchdog('booking', $e->getMessage());
|
|
drupal_set_message($error['message'], 'error');
|
|
}
|
|
catch (\Stripe\Error\ApiConnection $e) {
|
|
watchdog('booking', $e->getMessage());
|
|
drupal_set_message($error['message'], 'error');
|
|
}
|
|
catch (\Stripe\Error\Api $e) {
|
|
watchdog('booking', $e->getMessage());
|
|
drupal_set_message($error['message'], 'error');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Form submission handler.
|
|
*/
|
|
function booking_stripeform_form_submit($form, &$form_state) {
|
|
drupal_set_message('Charge successful!');
|
|
}
|
|
|
|
/**
|
|
* FAPI #pre_render callback.
|
|
*
|
|
* Removes the name field form a form element.
|
|
*/
|
|
function booking_stripeform_remove_name($element) {
|
|
unset($element['#name']);
|
|
return $element;
|
|
} |