Commit 011843bd authored by Joshua Walker's avatar Joshua Walker

Merge pull request #105 from progressivetech/github76a

Refactoring to properly load stripe on submission error.
Better loading of stripe details in stripe.php.
parents 7fd38b0f a976837a
......@@ -147,15 +147,15 @@ function stripe_civicrm_validateForm($formName, &$fields, &$files, &$form, &$err
* @param $form - reference to the form object
*/
function stripe_civicrm_buildForm($formName, &$form) {
if (isset($form->_paymentProcessor['payment_processor_type']) && $form->_paymentProcessor['payment_processor_type'] == 'Stripe') {
$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'));
$form->addElement('hidden', 'stripe_id', $form->_paymentProcessor['id'], array('id' => 'stripe-id'));
stripe_add_stripe_js($form);
}
stripe_add_stripe_js($stripe_key, $form);
}
else {
// This is a Confirm or Thank You (completed) form.
......@@ -167,8 +167,6 @@ function stripe_civicrm_buildForm($formName, &$form) {
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'));
// Stash stripe payment processor id
$form->addElement('hidden', 'stripe_id', $form->_paymentProcessor['id'], array('id' => 'stripe-id'));
}
}
}
......@@ -180,14 +178,14 @@ function stripe_civicrm_buildForm($formName, &$form) {
'CRM_Member_Form_Membership',
'CRM_Member_Form_MembershipRenewal'
);
if (in_array($formName, $backendForms) && !empty($form->_processors)) {
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($form);
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)) {
......@@ -197,49 +195,58 @@ function stripe_civicrm_buildForm($formName, &$form) {
}
/**
* Add publishable key and event bindings for Stripe.js.
* 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.
*/
function stripe_add_stripe_js($form) {
if (!empty($form->_paymentProcessor['password'])) {
$stripe_pub_key = $form->_paymentProcessor['password'];
function stripe_get_key($form) {
// Backend contribution pages have the password embedded in them.
if(isset($form->_paymentProcessor['password'])) {
return $form->_paymentProcessor['password'];
}
$is_test = 0;
if(isset($form->_mode)) {
$is_test = $form->_mode == 'live' ? 0 : 1;
}
else {
// Find Stripe's payproc ID in Civi.
$query_params = array(
1 => array('Stripe', 'String'),
);
$stripe_pp_id = CRM_Core_DAO::singleValueQuery("SELECT id
FROM civicrm_payment_processor_type
WHERE name = %1", $query_params);
// Find out if the form might use Stripe.
if (!empty($stripe_pp_id)) {
$mode = ($form->_mode == 'live' ? 0 : 1);
$query_params = array(
1 => array($stripe_pp_id, 'Integer'),
2 => array($mode, 'Integer'),
);
$stripe_procs_query = CRM_Core_DAO::executeQuery("SELECT name, password
FROM civicrm_payment_processor
WHERE payment_processor_type_id = %1 AND is_test = %2", $query_params);
// Loop through and see if Stripe is on this form.
while ($stripe_procs_query->fetch()) {
foreach ($form->_processors as $form_processor) {
if ($form_processor == $stripe_procs_query->name) {
$stripe_pub_key = $stripe_procs_query->password;
break;
}
if (!empty($stripe_pub_key)) {
break;
}
}
// _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') {
// We have a match.
return stripe_get_key_for_name($pp['name'], $is_test);
}
}
}
}
/**
* Given a payment processor name, return the pub key.
*/
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) {
$result = array_pop($results['values']);
return $result['password'];
}
}
catch (CiviCRM_API3_Exception $e) {
return NULL;
}
}
$form->addElement('hidden', 'stripe_pub_key', $stripe_pub_key, array('id' => 'stripe-pub-key'));
/**
* Add publishable key and event bindings for Stripe.js.
*/
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, 'billing-block', FALSE);
->addScriptFile('com.drastikbydesign.stripe', 'js/civicrm_stripe.js', 0);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment