A bunch of the comments in stripe.admin.inc module expressed a desire to abstract out some of the necessary elements of the Stripe process. This patch makes a number of changes:

It adds two new functions in stripe.module that take care of the necessary additions to $form:

/**
 * Returns the stripe pubkey.
 */
function stripe_get_pubkey()
/**
 * Returns an array to add to $form['#attached']['js'].
 *
 * @param $pubkey
 *   A Stripe pubkey returned from stripe_get_pubkey().
 * @param $selector
 *   A jQuery (xpath) selector to the <form> tag.
 */
function stripe_attached_js($pubkey, $selector = '.stripe-form')

I made a number of changes to stripe.js so that it works with any generic form that matches $selector (rather than just working for #stripe-admin-test).

It also adds a generic function to process transactions with some basic error logic:

/**
 * Process a stripe transaction.
 *
 * This takes into account all submissions to this webform. The output of this
 * function will be displayed under the "Results" tab then "Analysis".
 *
 * @param $amount
 *   The transaction amount, in dollars (if the currency is usd).
 * @param $token
 *   The Stripe token from the stripe.js call.
 * @param $description
 *   The transaction description for Stripe.
 * @param $error_messages
 *   BOOL.  Should we print error messages"
 * @return
 *   The stripe response, or FALSE if there was an error.
 */
function stripe_transaction($amount, $token, $currency = 'usd', $description = NULL, $error_messages = TRUE)

I used the changes from this patch to build the Stripe Webform module. You can see in work with the changes to stripe.admin.inc in this patch.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jlyon’s picture

FileSize
9.12 KB

Here's the patch.

jlyon’s picture

FileSize
9.12 KB

Forgot to add a * 100 to stripe.js to convert dollars into cents. This patch fixes it.

pbuyle’s picture

Version: » 7.x-1.x-dev

I dont see the value of the stripe_transaction() function or, more generally, of an additional non-standard functional layer on top of the Stripe API. The Stripe API is already simple and complete. To ease key management, the module already provide (now) the stripe_initialize() function, which could be replaced by a post-load callback if the module use the Libraries module (see #1967094: Integration with the Libraries API).

Instead of stripe_attached_js(), the Stripe module could implement hook_library with an rewriten JS Behavior that does not require a JS Setting to detect the form to be processed (as done in my suggested patch for #1971058: Provide payment form element through hook_element_info()).

stripe_get_pubkey() is useful and could be generalized to allow retrieving of both keys from both set (ie. status). Something like

/**
 * Retrieve the given key(s) from the given keys set.
 *
 * @param string $type
 *   The type of the retrieve, either 'secret' or 'publishable'. Or NULL to retrieve both keys.
 * @param string $set
 *   The keyset to retrieve the key(s) from, either 'test' or 'live'. Defautls to the currently active key sets (as set in the 'stripe_key_status' variable).
 */
stripe_get_key($type = NULL, $set = NULL) {
  // ...
}

mmilano’s picture

Status: Needs review » Closed (won't fix)
FileSize
26.91 KB

Mongolito404's sandbox included the libraries integration and stripe form element support. The libraries integration has been committed, and this is a patch of the extracted form element work that will be going in.

mmilano’s picture

Status: Closed (won't fix) » Fixed
mmilano’s picture

Status: Fixed » Closed (fixed)

Sorry, had 3 similar issues open.

mmilano’s picture

Issue summary: View changes

fix php code