forked from openfoodfoundation/openfoodnetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openfoodfoundation#2420 from stveep/stripe-admin-2
Stripe admin 2
- Loading branch information
Showing
14 changed files
with
174 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
app/assets/javascripts/admin/payments/controllers/payment.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
angular.module("admin.payments").controller "PaymentCtrl", ($scope, Payment, StatusMessage) -> | ||
$scope.form_data = Payment.form_data | ||
$scope.submitted = false | ||
$scope.StatusMessage = StatusMessage | ||
|
||
$scope.submitPayment = () -> | ||
return false if $scope.submitted | ||
$scope.submitted = true | ||
StatusMessage.display 'progress', t("spree.admin.payments.source_forms.stripe.submitting_payment") | ||
Payment.purchase() |
36 changes: 36 additions & 0 deletions
36
app/assets/javascripts/admin/payments/directives/stripe_elements.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
angular.module('admin.payments').directive "stripeElements", ($injector, AdminStripeElements) -> | ||
restrict: 'E' | ||
template: "<label for='card-element'>\ | ||
<div id='card-element'></div>\ | ||
<div id='card-errors' class='error'></div>\ | ||
</label>" | ||
|
||
link: (scope, elem, attr)-> | ||
if $injector.has('stripeObject') | ||
stripe = $injector.get('stripeObject') | ||
|
||
card = stripe.elements().create 'card', | ||
hidePostalCode: false | ||
style: | ||
base: | ||
fontFamily: "Roboto, Arial, sans-serif" | ||
fontSize: '16px' | ||
color: '#5c5c5c' | ||
'::placeholder': | ||
color: '#6c6c6c' | ||
card.mount('#card-element') | ||
|
||
# Elements validates user input as it is typed. To help your customers | ||
# catch mistakes, you should listen to change events on the card Element | ||
# and display any errors: | ||
card.addEventListener 'change', (event) -> | ||
displayError = document.getElementById('card-errors') | ||
if event.error | ||
displayError.textContent = event.error.message | ||
else | ||
displayError.textContent = '' | ||
|
||
return | ||
|
||
AdminStripeElements.stripe = stripe | ||
AdminStripeElements.card = card |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
angular.module("admin.payments", ['ofn.admin']) |
47 changes: 47 additions & 0 deletions
47
app/assets/javascripts/admin/payments/services/payment.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
angular.module('admin.payments').factory 'Payment', (AdminStripeElements, currentOrderNumber, paymentMethods, PaymentMethods, PaymentResource, StatusMessage, $window)-> | ||
new class Payment | ||
order: currentOrderNumber | ||
form_data: {} | ||
|
||
paymentMethodType: -> | ||
PaymentMethods.byID[@form_data.payment_method].method_type | ||
|
||
preprocess: -> | ||
munged_payment = {} | ||
munged_payment["payment"] = {payment_method_id: @form_data.payment_method, amount: @form_data.amount} | ||
munged_payment["order_id"] = @order | ||
# Not tested with Gateway other than Stripe. Could fall back to Rails for this? | ||
# Works ok without extra source_attrs for Cash, Bank Transfer etc. | ||
switch @paymentMethodType() | ||
when 'gateway' | ||
angular.extend munged_payment.payment, { | ||
source_attributes: | ||
number: @form_data.card_number | ||
month: @form_data.card_month | ||
year: @form_data.card_year | ||
verification_value: @form_data.card_verification_value | ||
} | ||
when 'stripe' | ||
angular.extend munged_payment.payment, { | ||
source_attributes: | ||
gateway_payment_profile_id: @form_data.token | ||
cc_type: @form_data.cc_type | ||
last_digits: @form_data.card.last4 | ||
month: @form_data.card.exp_month | ||
year: @form_data.card.exp_year | ||
} | ||
munged_payment | ||
|
||
purchase: -> | ||
if @paymentMethodType() == 'stripe' | ||
AdminStripeElements.requestToken(@form_data, @submit) | ||
else | ||
@submit() | ||
|
||
submit: => | ||
munged = @preprocess() | ||
PaymentResource.create({order_id: munged.order_id}, munged, (response, headers, status)=> | ||
$window.location.pathname = "/admin/orders/" + munged.order_id + "/payments" | ||
, (response) -> | ||
StatusMessage.display 'error', t("spree.admin.payments.source_forms.stripe.error_saving_payment") | ||
) |
38 changes: 38 additions & 0 deletions
38
app/assets/javascripts/admin/payments/services/stripe_elements.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
angular.module("admin.payments").factory 'AdminStripeElements', ($rootScope, StatusMessage) -> | ||
new class AdminStripeElements | ||
|
||
# These are both set from the AdminStripeElements directive | ||
stripe: null | ||
card: null | ||
|
||
# New Stripe Elements method | ||
requestToken: (secrets, submit) -> | ||
return unless @stripe? && @card? | ||
|
||
cardData = @makeCardData(secrets) | ||
|
||
@stripe.createToken(@card, cardData).then (response) => | ||
if(response.error) | ||
StatusMessage.display 'error', response.error.message | ||
else | ||
secrets.token = response.token.id | ||
secrets.cc_type = @mapCC(response.token.card.brand) | ||
secrets.card = response.token.card | ||
submit() | ||
|
||
# Maps the brand returned by Stripe to that required by activemerchant | ||
mapCC: (ccType) -> | ||
switch ccType | ||
when 'MasterCard' then return 'master' | ||
when 'Visa' then return 'visa' | ||
when 'American Express' then return 'american_express' | ||
when 'Discover' then return 'discover' | ||
when 'JCB' then return 'jcb' | ||
when 'Diners Club' then return 'diners_club' | ||
|
||
# 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} |
5 changes: 5 additions & 0 deletions
5
app/assets/javascripts/admin/resources/resources/payment_resource.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
angular.module("admin.resources").factory 'PaymentResource', ($resource) -> | ||
$resource('/admin/orders/:order_id/payments.json', {order_id: "@order_id"}, { | ||
'create': | ||
method: 'POST' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
app/overrides/spree/admin/payments/new/add_angular_to_form.html.haml.deface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/replace 'code[erb-loud]:contains("form_for @payment")' | ||
= form_for @payment, :url => admin_order_payments_path(@order), :html => {"ng-app" => "admin.payments", "ng-controller" => "PaymentCtrl"} do |f| |
3 changes: 3 additions & 0 deletions
3
app/overrides/spree/admin/payments/new/add_save_bar.html.haml.deface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/ insert_before "fieldset.no-border-top" | ||
%save-bar{ persist: "true" } |
2 changes: 2 additions & 0 deletions
2
app/overrides/spree/admin/payments/new/override_submit_for_button.html.haml.deface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/replace 'code[erb-loud]:contains("button @order.cart?")' | ||
= button_tag t(:update), type: 'button', "ng-click" => "submitPayment()" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 15 additions & 2 deletions
17
app/views/spree/admin/payments/source_forms/_stripe.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,17 @@ | ||
-# = render "spree/admin/payments/source_forms/gateway", payment_method: payment_method | ||
.stripe | ||
%script{:src => "https://js.stripe.com/v3/", :type => "text/javascript"} | ||
- if Stripe.publishable_key | ||
:javascript | ||
angular.module('admin.payments').value("stripeObject", Stripe("#{Stripe.publishable_key}")) | ||
|
||
%strong | ||
= t('.no_payment_via_admin_backend') | ||
.row | ||
.three.columns | ||
= label_tag :cardholder_name, t(:cardholder_name) | ||
.six.columns | ||
= text_field_tag :cardholder_name, nil, {size: 40, "ng-model" => 'form_data.name'} | ||
.row | ||
.three.columns | ||
= label_tag :card_details, t(:card_details) | ||
.six.columns | ||
%stripe-elements |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters