Backend code to create Stripe customers and store their IDs in CreditCards. Page refresh not working

This commit is contained in:
stveep
2017-05-01 20:21:30 +01:00
committed by Rob Harrington
parent 1449169b16
commit c9c4680ef6
5 changed files with 101 additions and 16 deletions

View File

@@ -10,16 +10,5 @@ Darkswarm.controller "CreditCardsCtrl", ($scope, $timeout, CreditCard, savedCred
$scope.secrets = CreditCard.secrets
$scope.storeCard = =>
Loading.message = "Saving"
CreditCard.requestToken($scope.secrets)
# Need to call Spree::Gateway::StripeConnect#provider.store(creditcard)
# creditcard should be formatted as for a payment
# The token then needs to be associated with the Customer (in Stripe) - can be done in Ruby.

View File

@@ -1,16 +1,33 @@
Darkswarm.factory 'CreditCard', ($injector, $rootScope, StripeJS, Navigation, $http, RailsFlashLoader, Loading)->
new class CreditCard
errors: {}
secrets: {}
requestToken: (secrets) ->
#$scope.secrets.name = $scope.secrets.first_name + " " + $scope.secrets.last_name
secrets.name = @full_name(secrets)
StripeJS.requestToken(secrets, @submit)
StripeJS.requestToken(secrets, @submit, t("saving_credit_card"))
submit: =>
$rootScope.$apply ->
Loading.clear()
Navigation.go '/account'
params = @process_params()
$http.put('/credit_cards/new_from_token', params )
.success (data, status) ->
$rootScope.$apply ->
Loading.clear()
Navigation.go '/account'
.error (response, status) ->
if response.path
Navigation.go response.path
else
Loading.clear()
@errors = response.errors
RailsFlashLoader.loadFlash(response.flash)
full_name: (secrets) ->
secrets.first_name + " " + secrets.last_name
process_params: ->
{"exp_month": @secrets.card.exp_month,
"exp_year": @secrets.card.exp_year,
"last4": @secrets.card.last4,
"token": @secrets.token,
"cc_type": @secrets.card.brand}

View File

@@ -0,0 +1,47 @@
module Spree
class CreditCardsController < BaseController
def new_from_token
set_user
# At the moment a new Customer is created for every credit card (even via ActiveMerchant),
# so doing the same here (for now).
if @customer = create_customer(params[:token])
# Since it's a new customer, the default_source is the card that our original token represented
credit_card_params = format_credit_card_params(params)
.merge({gateway_payment_profile_id: @customer.default_source,
gateway_customer_profile_id: @customer.id,
cc_type: params[:cc_type]
})
@credit_card = Spree::CreditCard.new(credit_card_params)
# Can't mass assign these:
@credit_card.cc_type = credit_card_params[:cc_type]
@credit_card.last_digits = credit_card_params[:last_digits]
@credit_card.user_id = @user.id
if @credit_card.save
render json: @credit_card, status: :ok
else
render json: "error saving credit card", status: 500
end
else
render json: "error creating Stripe customer", status: 500
end
end
private
def create_customer(token)
Stripe::Customer.create(email: @user.email, source: token)
end
def format_credit_card_params(params_hash)
{ month: params_hash[:exp_month],
year: params_hash[:exp_year],
last_digits: params_hash[:last4]
}
end
def set_user
@user = spree_current_user
end
end
end

View File

@@ -234,6 +234,7 @@ Spree::Core::Engine.routes.prepend do
match '/admin/reports/xero_invoices' => 'admin/reports#xero_invoices', :as => "xero_invoices_admin_reports", :via => [:get, :post]
match '/admin', :to => 'admin/overview#index', :as => :admin
match '/admin/payment_methods/show_provider_preferences' => 'admin/payment_methods#show_provider_preferences', :via => :get
put 'credit_cards/new_from_token', to: 'credit_cards#new_from_token'
namespace :api, :defaults => { :format => 'json' } do

View File

@@ -0,0 +1,31 @@
require 'spec_helper'
require 'support/request/authentication_workflow'
describe Spree::CreditCardsController do
include AuthenticationWorkflow
let(:user) { create_enterprise_user }
it "Creates a credit card from token + params" do
controller.stub(:spree_current_user) { user }
controller.stub(:create_customer) {
sc = Stripe::Customer.new
sc.default_source = "card_1AEEbN2eZvKYlo2CMk6QwrN7"
sc.email = nil
sc.stub(:id) {"cus_AZNMJzuACN3Sgt"}
sc }
token = "tok_234bd2c22"
expect{ post :new_from_token, {
"exp_month" => 12,
"exp_year" => 2020,
"last4" => 4242,
"token" => token,
"cc_type" => "visa"
} }.to change(Spree::CreditCard, :count).by(1)
Spree::CreditCard.last.gateway_payment_profile_id.should eq "card_1AEEbN2eZvKYlo2CMk6QwrN7"
Spree::CreditCard.last.gateway_customer_profile_id.should eq "cus_AZNMJzuACN3Sgt"
Spree::CreditCard.last.user_id.should eq user.id
Spree::CreditCard.last.last_digits.should eq "4242"
end
end