1

I am attempting to create a form where the user can update their subscription card details (with Stripe Elements).

From looking at the Charge plugin's source code, I believe the endpoint I need is charge/updateSubscription.

As with checkout I am creating an opts hidden input in my twig template like so:

{{
  craft.charge.setPaymentOptions({
    planAmount: currentSubscription.planAmount,
    planInterval: currentSubscription.planInterval,
    planIntervalCount: currentSubscription.planIntervalCount
  })
}}

And then making a POST request with Javascript to the endpoint:

getStripeToken(cardToken => {
  post({
    params: {
      action: 'charge/updateSubscription',
      opts,
      cardToken,
      subscriptionId
    },
    then: response => console.log(response)
    url: '/'
  })
})

Quite simply, this doesn't work. I receive a {success: true} response, but the Stripe card has not been updated.

Does anybody what I'm doing wrong, or even if this is possible?

4

In the end I worked out the card is related to a customer, not a subscription.

I couldn't see any public endpoint to update a customer's card, so I added this method in charge/controllers/ChargeController.php (not ideal to update the plugin directly I know):

public function actionUpdateCard()
{
  $this->requirePostRequest();
  $customer = craft()->charge_customer->find();
  $cardToken = craft()->request->getRequiredPost('cardToken');

  if ($customer->addCard($cardToken)) {
    $this->returnJson(['success' => true]);
  } else {
    $this->returnJson(['success' => false]);
  }
}

and then can make requests to the charge/updateCard endpoint:

getStripeToken(cardToken => {
  post({
    params: {
      action: 'charge/updateCard',
      cardToken
    },
    then: ({ success }) => console.log(`Card updated: ${success}`)
    url: '/'
  })
})
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.