You are here

function stripe_payment_payment_method_load in Stripe 7

Implements hook_ENTITY_TYPE_ACTION().

File

stripe_payment/stripe_payment.module, line 211
Stripe Payment module.

Code

function stripe_payment_payment_method_load($payment_methods) {

  // Discard non-Stripe payment methods.
  $payment_methods = array_filter($payment_methods, function (PaymentMethod $payment_method) {
    return $payment_method->controller->name === 'StripePaymentMethodController';
  });
  if ($payment_methods) {

    // Collect pmids.
    $pmids = array_map(function (PaymentMethod $payment_method) {
      return $payment_method->pmid;
    }, $payment_methods);

    // Load keys (all payment methods at once).
    $keys = db_select('stripe_payment_keys', 'spk')
      ->fields('spk', array(
      'pmid',
      'secret',
      'publishable',
    ))
      ->condition('pmid', $pmids)
      ->execute()
      ->fetchAllAssoc('pmid', PDO::FETCH_ASSOC);

    // Set controller data for all payment methods.
    foreach ($payment_methods as $payment_method) {
      $payment_method->controller_data += $payment_method->controller->controller_data_defaults;
      if (isset($keys[$payment_method->pmid])) {

        // Keys found for this payment method.
        $payment_method->controller_data['keys']['mode'] = 1;
        $payment_method->controller_data['keys']['secret'] = $keys[$payment_method->pmid]['secret'];
        $payment_method->controller_data['keys']['publishable'] = $keys[$payment_method->pmid]['publishable'];
      }
    }
  }
}