Currently this module only supports Stripe fields on node entities. It would be useful to support other entity types.

I haven't looked everywhere in the code to find where 'node' entities are hard coded, but here is an example:

src/Controller/StripeChargeController.php (59-60)

      // Load the price from the node that was purchased.
      $node = $this->entityTypeManager()->getStorage('node')->load($entity_id);

Is there any reason this couldn't be replaced with something like:

  $entity_type = $request->get('entity_type');
  // then we could change the previous lines to:
  // Load the price from the "entity" that was purchased.
      $entity = $this->entityTypeManager()->getStorage($entity_type)->load($entity_id);

This change would require that the $request contain the entity_type, I can't think of any really slick ways to get it.

Maybe entity_type could be populated from src/Plugin/Field/FieldFormatter/StripeCheckoutFormatter.php ( ln 69-103 )

      $elements[$delta] = [
        '#theme' => 'stripe_checkout',
        '#data' => [
          // Price is specified in cents.
          'amount' => $price * 100,
          // @todo Make configurable.
          'name' => 'Company name',
          // @todo Make configurable.
          'description' => 'Widget',
          'key' => $pub_key,
          // @todo Make configurable.
          'zip_code' => 'true',
          'locale' => 'auto',
          // @todo Make configurable.
          'image' => 'https://stripe.com/img/documentation/checkout/marketplace.png',
          'email' => \Drupal::currentUser()->getEmail(),
          'label' => $this->t('Purchase'),
        ],
        '#price' => $price,
        '#entity_id' => $item->getEntity()->id(),
        '#entity_type' => $item->getEntity()->getEntityTypeId(), // <- Is this where the $request is generated from?
        '#field_name' => $item->getFieldDefinition()->getName(),
        '#logged_in' => \Drupal::currentUser()->isAuthenticated(),
        "#anon_url" => Url::fromRoute('user.register', [], [
          'query' => [
            'destination' => $current_path,
            'stripe_checkout_click' => TRUE,
          ],
        ]),
        '#action' => Url::fromRoute('stripe_checkout.stripe_charge_controller_charge'),
        '#attached' => [
          'library' => [
            'stripe_checkout/checkout',
          ],
        ],
      ];

I am not sure I did all that correctly, but the idea is, if we can get the $entity_type into the controller's $request, then we could use payment buttons on any fieldable entity. Or am I missing something?

A part of this would need to be a decision about how to handle the redirect in src/Controller/StripeChargeController.php ( ln 94 )

return $this->redirect('entity.node.canonical', array('node' => $entity_id));

Maybe this could be configurable in the field settings?

Comments

scottsawyer created an issue. See original summary.

scottsawyer’s picture

StatusFileSize
new4.68 KB

And here is a patch. So far I have only tested that you can create a payment with Stripe's test credit cards, and only with entities that have a link template, but I would imagine that would be a 90% use case.

In cases where there is no valid link template, it grabs Url::fromRoute('') and attempts to redirect to whatever the current route is. I haven't tested this part at all because all of my custom entities have link templates.

I would really like some testers.

scottsawyer’s picture

@grasmash

The module page reads 'Provides a "Stripe Checkout" field type that can be used to set the prices of a fielded entity. ' However, the 8x-2.x branch currently only supports node entities. What would it take to get this patch committed?

grasmash’s picture

Status: Active » Needs review