Creating basic angular StripeJS wrapper service for requesting tokens

This commit is contained in:
Rob Harrington
2017-02-21 12:25:51 +11:00
parent 5ad88f992c
commit 5c16fefe41
6 changed files with 162 additions and 26 deletions

View File

@@ -22,6 +22,6 @@ Darkswarm.controller "CheckoutCtrl", ($scope, localStorageService, Checkout, Cur
event.preventDefault()
$scope.submitted = true
if form.$valid
$scope.Checkout.submit()
$scope.Checkout.purchase()
else
$scope.$broadcast 'purchaseFormInvalid', form

View File

@@ -1,10 +1,16 @@
Darkswarm.factory 'Checkout', (CurrentOrder, ShippingMethods, PaymentMethods, $http, Navigation, CurrentHub, RailsFlashLoader, Loading)->
Darkswarm.factory 'Checkout', ($injector, CurrentOrder, ShippingMethods, StripeJS, PaymentMethods, $http, Navigation, CurrentHub, RailsFlashLoader, Loading)->
new class Checkout
errors: {}
secrets: {}
order: CurrentOrder.order
submit: ->
purchase: ->
if @paymentMethod()?.method_type == 'stripe'
StripeJS.requestToken(@secrets, @submit)
else
@submit()
submit: =>
Loading.message = t 'submitting_order'
$http.put('/checkout', {order: @preprocess()}).success (data, status)=>
Navigation.go data.path
@@ -53,6 +59,16 @@ Darkswarm.factory 'Checkout', (CurrentOrder, ShippingMethods, PaymentMethods, $h
last_name: @order.bill_address.lastname
}
if @paymentMethod()?.method_type == 'stripe'
angular.extend munged_order.payments_attributes[0], {
source_attributes:
gateway_payment_profile_id: @secrets.token
cc_type: @secrets.cc_type
last_digits: @secrets.card.last4
month: @secrets.card.exp_month
year: @secrets.card.exp_year
}
munged_order
shippingMethod: ->

View File

@@ -0,0 +1,36 @@
Darkswarm.factory 'StripeJS', ($rootScope, Loading, RailsFlashLoader) ->
new class StripeJS
requestToken: (secrets, submit) ->
Loading.message = "Processing Payment..."
params =
number: secrets.card_number
cvc: secrets.card_verification_value
exp_month: secrets.card_month or 0
exp_year: secrets.card_year or 0
# This is the global Stripe object created by Stripe.js, included in the _stripe partial
Stripe.card.createToken params, (status, response) =>
if response.error
$rootScope.$apply ->
Loading.clear()
RailsFlashLoader.loadFlash({error: "Error: #{response.error.message}"})
else
secrets.token = response['id']
secrets.cc_type = @mapCC(response.card.brand)
secrets.card = response.card
submit()
mapCC: (ccType) ->
if ccType == 'MasterCard'
return 'mastercard'
else if ccType == 'Visa'
return 'visa'
else if ccType == 'American Express'
return 'amex'
else if ccType == 'Discover'
return 'discover'
else if ccType == 'Diners Club'
return 'dinersclub'
else if ccType == 'JCB'
return 'jcb'
return