Files
openfoodnetwork/app/assets/javascripts/darkswarm/services/stripe_elements.js.coffee
Matt-Yorkley 4cdb892c75 Refactor formatting of credit card brand names and reformat automatically when saving
This little bit of "translation" between what we might receive as input and what ActiveMerchant requires is important, but currently the responsibility for this job is spread all over the code base. It can now just live in the CreditCard model (in one place) and doesn't need to be duplicated anywhere else (like several different places in frontend Javascript!)
2021-12-02 16:22:52 +00:00

65 lines
2.5 KiB
CoffeeScript

angular.module('Darkswarm').factory 'StripeElements', ($rootScope, Messages) ->
new class StripeElements
# These are both set from the StripeElements directive
stripe: null
card: null
# Create Token to be used with the Stripe Charges API
requestToken: (secrets, submit, loading_message = t("processing_payment")) ->
return unless @stripe? && @card?
Messages.loading loading_message
cardData = @makeCardData(secrets)
@stripe.createToken(@card, cardData).then (response) =>
if(response.error)
@reportError(response.error, t("error") + ": #{response.error.message}")
else
secrets.token = response.token.id
secrets.cc_type = response.token.card.brand
secrets.card = response.token.card
submit()
.catch (response) =>
# Stripe handles errors in the response above. This code may never be
# reached. But if we get here, we want to know.
@reportError(response, t("js.stripe_elements.unknown_error_from_stripe"))
throw response
# Create Payment Method to be used with the Stripe Payment Intents API
createPaymentMethod: (secrets, submit, loading_message = t("processing_payment")) ->
return unless @stripe? && @card?
Messages.loading loading_message
cardData = @makeCardData(secrets)
@stripe.createPaymentMethod({ type: 'card', card: @card }, @card, cardData).then (response) =>
if(response.error)
@reportError(response.error, t("error") + ": #{response.error.message}")
else
secrets.token = response.paymentMethod.id
secrets.cc_type = response.paymentMethod.card.brand
secrets.card = response.paymentMethod.card
submit()
.catch (response) =>
# Stripe handles errors in the response above. This code may never be
# reached. But if we get here, we want to know.
@reportError(response, t("js.stripe_elements.unknown_error_from_stripe"))
throw response
reportError: (error, messageForUser) ->
Messages.error(messageForUser)
@triggerAngularDigest()
console.error(error)
Bugsnag.notify(new Error(JSON.stringify(error)))
triggerAngularDigest: ->
# $evalAsync is improved way of triggering a digest without calling $apply
$rootScope.$evalAsync()
# It doesn't matter if any of these are nil, all are optional.
makeCardData: (secrets) ->
{'name': secrets.name,
'address1': secrets.address1,
'city': secrets.city,
'zipcode': secrets.zipcode}