Skip to content
Snippets Groups Projects
Commit 4ee7e494 authored by drastik's avatar drastik
Browse files

Major changes to the buildForm and other Stripe-activation logic.

Fix in civicrm_stripe.js that was causing "Stripe.js Not passed" because token was empty.
Fix error message about accessing static variable.
Large cleanup to code structure in stripe.php.
parent 011843bd
Branches
Tags
No related merge requests found
......@@ -19,9 +19,8 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
* Mode of operation: live or test.
*
* @var object
* @static
*/
static protected $_mode = NULL;
protected $_mode = NULL;
/**
* Constructor
......@@ -32,31 +31,12 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
* @return void
*/
function __construct($mode, &$paymentProcessor) {
self::$_mode = $mode;
$this->_mode = $mode;
$this->_islive = ($mode == 'live' ? 1 : 0);
$this->_paymentProcessor = $paymentProcessor;
$this->_processorName = ts('Stripe');
}
/**
* Singleton function used to manage this object.
*
* @param string $mode the mode of operation: live or test
* @param object $paymentProcessor the details of the payment processor being invoked
* @param object $paymentForm reference to the form object if available
* @param boolean $force should we force a reload of this payment object
*
* @return object
* @static
*
*/
static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
$processorName = $paymentProcessor['name'];
if (self::$_singleton[$processorName] === NULL || $force) {
self::$_singleton[$processorName] = new self($mode, $paymentProcessor);
}
return self::$_singleton[$processorName];
}
/**
* This function checks to see if we have the right config values.
*
......@@ -256,16 +236,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
return $params;
}
// Get live/test mode.
switch ($this->_mode) {
case 'test':
$params['transaction_mode'] = $transaction_mode = 0;
break;
case 'live':
$params['transaction_mode'] = $transaction_mode = 1;
break;
}
// Get proper entry URL for returning on error.
if (!(array_key_exists('qfKey', $params))) {
// Probably not called from a civicrm form (e.g. webform) -
......@@ -344,7 +314,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$customer_query = CRM_Core_DAO::singleValueQuery("SELECT id
FROM civicrm_stripe_customers
WHERE email = %1 AND is_live = '$transaction_mode'", $query_params);
WHERE email = %1 AND is_live = '{$this->_islive}'", $query_params);
/****
* If for some reason you cannot use Stripe.js and you are aware of PCI Compliance issues,
......@@ -401,7 +371,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_customers
(email, id, is_live) VALUES (%1, %2, '$transaction_mode')", $query_params);
(email, id, is_live) VALUES (%1, %2, '{$this->_islive}')", $query_params);
}
else {
CRM_Core_Error::fatal(ts('There was an error saving new customer within Stripe. Is Stripe down?'));
......@@ -453,7 +423,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
1 => array($email, 'String'),
);
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_stripe_customers
WHERE email = %1 AND is_live = '$transaction_mode'", $query_params);
WHERE email = %1 AND is_live = '{$this->_islive}'", $query_params);
// Create new record for this customer.
$query_params = array(
......@@ -461,7 +431,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
2 => array($stripe_customer->id, 'String'),
);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_customers (email, id, is_live)
VALUES (%1, %2, '$transaction_mode')", $query_params);
VALUES (%1, %2, '{$this->_islive}')", $query_params);
}
else {
// Customer was found in civicrm_stripe database, but unable to be
......@@ -551,8 +521,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
* @public
*/
function doRecurPayment(&$params, $amount, $stripe_customer) {
$transaction_mode = $params['transaction_mode'];
// Get recurring contrib properties.
$frequency = $params['frequency_unit'];
$installments = $params['installments'];
......@@ -567,7 +535,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$stripe_plan_query = CRM_Core_DAO::singleValueQuery("SELECT plan_id
FROM civicrm_stripe_plans
WHERE plan_id = %1 AND is_live = '$transaction_mode'", $query_params);
WHERE plan_id = %1 AND is_live = '{$this->_islive}'", $query_params);
if (!isset($stripe_plan_query)) {
$formatted_amount = '$' . number_format(($amount / 100), 2);
......@@ -594,7 +562,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
1 => array($plan_id, 'String'),
);
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_plans (plan_id, is_live)
VALUES (%1, '$transaction_mode')", $query_params);
VALUES (%1, '{$this->_islive}')", $query_params);
}
// If a contact/customer has an existing active recurring
......@@ -623,7 +591,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$existing_subscription_query = CRM_Core_DAO::singleValueQuery("SELECT invoice_id
FROM civicrm_stripe_subscriptions
WHERE customer_id = %1 AND is_live = '$transaction_mode'", $query_params);
WHERE customer_id = %1 AND is_live = '{$this->_islive}'", $query_params);
if (!empty($existing_subscription_query)) {
// Cancel existing Recurring Contribution in CiviCRM.
......@@ -658,14 +626,14 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
if (empty($installments)) {
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions
(customer_id, invoice_id, is_live)
VALUES (%1, %2, '$transaction_mode')", $query_params);
VALUES (%1, %2, '{$this->_islive}')", $query_params);
}
else {
// Add the end time to the query params.
$query_params[3] = array($end_time, 'Integer');
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions
(customer_id, invoice_id, end_time, is_live)
VALUES (%1, %2, %3, '$transaction_mode')", $query_params);
VALUES (%1, %2, %3, '{$this->_islive}')", $query_params);
}
$params['trxn_id'] = $stripe_response->id;
......
......@@ -11,7 +11,7 @@
<author>Joshua Walker (drastik) - Drastik by Design</author>
<email>admin@drastikbydesign.com</email>
</maintainer>
<releaseDate>2015-09-09</releaseDate>
<releaseDate>2015-10-09</releaseDate>
<version>1.9.2</version>
<compatibility>
<ver>4.4</ver>
......
......@@ -50,7 +50,7 @@
}
else {
if (!($('.stripe-payment-form').length)) {
$('#crm-container>form').addClass('stripe-payment-form');
$('#crm-container > form').addClass('stripe-payment-form');
}
}
$form = $('form.stripe-payment-form');
......@@ -59,8 +59,8 @@
}
else {
$submit = $form.find('input[type="submit"][formnovalidate!="1"]');
// If CiviDiscount button or field is submitted, flag the form
// If CiviDiscount button or field is submitted, flag the form.
$form.data('cividiscount-dont-handle', '0');
$form.find('input[type="submit"][formnovalidate="1"]').click( function() {
$form.data('cividiscount-dont-handle', 1);
......@@ -73,6 +73,7 @@
$submit;
}
// For CiviCRM Webforms.
if (isWebform) {
if (!($('#action').length)) {
$form.append('<input type="hidden" name="op" id="action" />');
......@@ -92,7 +93,7 @@
var webformPrevious = $('input.webform-previous').first().val();
}
else {
// This is native civicrm form - check for existing token
// This is native civicrm form - check for existing token.
if ($form.find("input#stripe-token").val()) {
$('.credit_card_info-group').hide();
$('#billing-payment-block').append('<input type="button" value="Edit CC details" id="ccButton" />');
......@@ -110,8 +111,8 @@
// Intercept form submission.
$form.submit(function (event) {
// Don't handle submits generated by the CiviDiscount button
if ($form.data('cividiscount-dont-handle') == 1) {
// Don't handle submits generated by the CiviDiscount button.
if ($form.data('cividiscount-dont-handle') == 1) {
return true;
}
if (isWebform) {
......@@ -138,11 +139,12 @@
buttonText = $submit.attr('value');
$submit.prop('disabled', true).attr('value', 'Processing');
// Hide payment if total is 0 and no more participants
// Hide payment if total is 0 and no more participants.
if ($('#priceset').length) {
currentTotal = cj('#pricevalue').text().replace(/[^\/\d]/g,'');
additionalParticipants = cj("#additional_participants").val();
// The currentTotal is already being calculated in Form/Contribution/Main.tpl.
if (currentTotal == 0 && !additionalParticipants) {
// This is also hit when "Going back", but we already have stripe_token.
return true;
}
}
......@@ -161,7 +163,7 @@
}
}
// Handle pay later (option value '0' in payment_processor radio group)
// Handle pay later (option value '0' in payment_processor radio group).
if ($form.find('input[name="payment_processor"]:checked').length && !parseInt($form.find('input[name="payment_processor"]:checked').val())) {
return true;
}
......
......@@ -32,7 +32,7 @@ function stripe_civicrm_install() {
`email` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
`id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`is_live` tinyint(4) NOT NULL COMMENT 'Whether this is a live or test transaction',
UNIQUE KEY `email` (`email`)
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
......@@ -125,14 +125,17 @@ function stripe_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
* @param $errors - Reference to the errors array.
*/
function stripe_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ((isset($form->_paymentProcessor['payment_processor_type']) &&$form->_paymentProcessor['payment_processor_type'] == 'Stripe')
|| (!isset($form->_paymentProcessor['payment_processor_type']) && isset($form->_elementIndex['stripe_token']))) {
if($form->elementExists('credit_card_number')){
if (empty($form->_paymentProcessor['payment_processor_type'])) {
return;
}
// If Stripe is active here.
if (isset($form->_elementIndex['stripe_token'])) {
if ($form->elementExists('credit_card_number')) {
$cc_field = $form->getElement('credit_card_number');
$form->removeElement('credit_card_number', true);
$form->addElement($cc_field);
}
if($form->elementExists('cvv2')){
if ($form->elementExists('cvv2')) {
$cvv2_field = $form->getElement('cvv2');
$form->removeElement('cvv2', true);
$form->addElement($cvv2_field);
......@@ -148,79 +151,66 @@ function stripe_civicrm_validateForm($formName, &$fields, &$files, &$form, &$err
*/
function stripe_civicrm_buildForm($formName, &$form) {
$stripe_key = stripe_get_key($form);
if (!empty($stripe_key)) {
if (!stristr($formName, '_Confirm') && !stristr($formName, '_ThankYou')) {
// This is the 'Main', or first step of the form that collects CC data.
if (!$form->elementExists('stripe_token')) {
$form->setAttribute('class', $form->getAttribute('class') . ' stripe-payment-form');
$form->addElement('hidden', 'stripe_token', NULL, array('id' => 'stripe-token'));
}
stripe_add_stripe_js($stripe_key, $form);
}
else {
// This is a Confirm or Thank You (completed) form.
$params = $form->get('params');
// Contrib forms store this in $params, Event forms in $params[0].
if (!empty($params[0]['stripe_token'])) {
$params = $params[0];
}
if (!empty($params['stripe_token'])) {
// Stash the token (including its value) in Confirm, in case they go backwards.
$form->addElement('hidden', 'stripe_token', $params['stripe_token'], array('id' => 'stripe-token'));
}
}
// If this is not a form Stripe is involved in, do nothing.
if (empty($stripe_key)) {
return;
}
$params = $form->get('params');
// Contrib forms store this in $params, Event forms in $params[0].
if (!empty($params[0]['stripe_token'])) {
$params = $params[0];
}
$stripe_token = (empty($params['stripe_token']) ? NULL : $params['stripe_token']);
// For the 'Record Contribution' backend page.
$backendForms = array(
'CRM_Contribute_Form_Contribution',
'CRM_Event_Form_Participant',
'CRM_Member_Form_Membership',
'CRM_Member_Form_MembershipRenewal'
);
if (in_array($formName, $backendForms) && !empty($stripe_key)) {
if (!isset($form->_elementIndex['stripe_token'])) {
if (empty($form->_attributes['class'])) {
$form->_attributes['class'] = '';
}
$form->_attributes['class'] .= ' stripe-payment-form';
$form->addElement('hidden', 'stripe_token', NULL, array('id' => 'stripe-token'));
stripe_add_stripe_js($stripe_key, $form);
}
// Add email field as it would usually be found on donation forms.
if (!isset($form->_elementIndex['email']) && !empty($form->userEmail)) {
$form->addElement('hidden', 'email', $form->userEmail, array('id' => 'user-email'));
}
// Add some hidden fields for Stripe.
if (!$form->elementExists('stripe_token')) {
$form->setAttribute('class', $form->getAttribute('class') . ' stripe-payment-form');
$form->addElement('hidden', 'stripe_token', $stripe_token, array('id' => 'stripe-token'));
}
stripe_add_stripe_js($stripe_key, $form);
// Add email field as it would usually be found on donation forms.
if (!isset($form->_elementIndex['email']) && !empty($form->userEmail)) {
$form->addElement('hidden', 'email', $form->userEmail, array('id' => 'user-email'));
}
}
/**
* Return the stripe api public key (aka password)
* Return the stripe api public key (aka password)
*
* If this form could conceiveably now or at any time in the future
* contain a Stripe payment processor, return the api public key for
* that processor.
* that processor.
*/
function stripe_get_key($form) {
// Backend contribution pages have the password embedded in them.
if(isset($form->_paymentProcessor['password'])) {
return $form->_paymentProcessor['password'];
// Only return first value if Stripe is the only/default.
if ($form->_paymentProcessor['payment_processor_type'] == 'Stripe') {
if (isset($form->_paymentProcessor['password'])) {
return $form->_paymentProcessor['password'];
}
}
// Otherwise we need to look through all active payprocs and find Stripe.
$is_test = 0;
if(isset($form->_mode)) {
if (isset($form->_mode)) {
$is_test = $form->_mode == 'live' ? 0 : 1;
}
// _paymentProcessors array seems to be the most reliable way to find
// at least payment processor on the form that is a stripe pp.
if(isset($form->_paymentProcessors)) {
foreach($form->_paymentProcessors as $k => $pp) {
if($pp['payment_processor_type'] == 'Stripe') {
// The _paymentProcessors array seems to be the most reliable way to find
// if the form is using Stripe.
if (!empty($form->_paymentProcessors)) {
foreach ($form->_paymentProcessors as $pp) {
if ($pp['payment_processor_type'] == 'Stripe') {
if (!empty($pp['password'])) {
return $pp['password'];
}
// We have a match.
return stripe_get_key_for_name($pp['name'], $is_test);
}
}
}
// Return NULL if this is not a form with Stripe involved.
return NULL;
}
/**
......@@ -230,13 +220,13 @@ function stripe_get_key_for_name($name, $is_test) {
try {
$params = array('name' => $name, 'is_test' => $is_test);
$results = civicrm_api3('PaymentProcessor', 'get', $params);
if($results['count'] == 1) {
if ($results['count'] == 1) {
$result = array_pop($results['values']);
return $result['password'];
}
}
catch (CiviCRM_API3_Exception $e) {
return NULL;
return NULL;
}
}
......@@ -245,8 +235,7 @@ function stripe_get_key_for_name($name, $is_test) {
*/
function stripe_add_stripe_js($stripe_key, $form) {
$form->addElement('hidden', 'stripe_pub_key', $stripe_key, array('id' => 'stripe-pub-key'));
CRM_Core_Resources::singleton()
->addScriptFile('com.drastikbydesign.stripe', 'js/civicrm_stripe.js', 0);
CRM_Core_Resources::singleton()->addScriptFile('com.drastikbydesign.stripe', 'js/civicrm_stripe.js', 0);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment