Skip to content

Commit

Permalink
Add tests, skip confirm step, support non-decimal currencies, domain_…
Browse files Browse the repository at this point in the history
…verification_certificate as textarea
  • Loading branch information
dimon-k committed Jul 25, 2018
1 parent 4277e47 commit 31f5dce
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/models/spree/gateway/stripe_apple_pay_gateway.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Spree
class Gateway::StripeApplePayGateway < Gateway::StripeGateway
preference :country_code, :string, default: 'US'
preference :domain_verification_certificate, :string
preference :domain_verification_certificate, :text

def method_type
'stripe_apple_pay'
end
end
end
end
15 changes: 15 additions & 0 deletions app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module SpreeGateway
module OrderDecorator
def confirmation_required?
return false if paid_with_apple_pay?

super
end

def paid_with_apple_pay?
payments.any?(&:apple_pay?)
end
end
end

Spree::Order.prepend SpreeGateway::OrderDecorator
9 changes: 9 additions & 0 deletions app/models/spree/payment_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SpreeGateway
module PaymentDecorator
def apple_pay?
payment_method.is_a? Spree::Gateway::StripeApplePayGateway
end
end
end

Spree::Payment.prepend SpreeGateway::PaymentDecorator
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class ApplePayDomainVerificationController < StoreController
include Spree::Core::ControllerHelpers

def show
gateway = Gateway.active.find_by!(type: 'Spree::Gateway::StripeApplePayGateway')
gateway = Spree::Gateway::StripeApplePayGateway.active.first

render plain: gateway.preferred_domain_verification_certificate
end
end
end
end
2 changes: 1 addition & 1 deletion lib/views/frontend/spree/checkout/payment/_stripe.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@
});
</script>

<%= render 'spree/checkout/payment/stripe_additional_info' %>
<%= render 'spree/checkout/payment/stripe_additional_info', bill_address: @order.bill_address %>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<%- if @order.has_checkout_step?('address') -%>
<script>
Spree.stripeAdditionalInfo = {
name: "<%= @order.bill_address.full_name %>",
address_line1: "<%= @order.bill_address.address1 %>",
address_line2: "<%= @order.bill_address.address2 %>",
address_city: "<%= @order.bill_address.city %>",
address_state: "<%= @order.bill_address.state_text %>",
address_zip: "<%= @order.bill_address.zipcode %>",
address_country: "<%= @order.bill_address.country %>"
name: "<%= bill_address.full_name %>",
address_line1: "<%= bill_address.address1 %>",
address_line2: "<%= bill_address.address2 %>",
address_city: "<%= bill_address.city %>",
address_state: "<%= bill_address.state_text %>",
address_zip: "<%= bill_address.zipcode %>",
address_country: "<%= bill_address.country %>"
}
</script>
<%- end -%>
<%- end -%>
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
<% @order.line_items.each do |line_item| %>
{
label: '<%= line_item.name %> x <%= line_item.quantity %>',
amount: <%= line_item.total.to_d * 100 %>
amount: <%= Spree::Money.new(line_item.total, currency: line_item.currency).amount_in_cents %>
},
<% end %>
<% if @order.tax_total != 0 %>
{
label: '<%= Spree.t(:tax) %>',
amount: <%= @order.tax_total.to_d * 100 %>
amount: <%= Spree::Money.new(@order.tax_total, currency: @order.currency).amount_in_cents %>
},
<% end %>
<% if @order.shipment_total != 0 %>
{
label: '<%= Spree.t(:shipment) %>',
amount: <%= @order.shipment_total.to_d * 100 %>
amount: <%= Spree::Money.new(@order.shipment_total, currency: @order.currency).amount_in_cents %>
}
<% end %>
],
total: {
label: '<%= Spree.t(:total) %>',
amount: <%= @order.total.to_d * 100 %>, // FIXME: support yen and other currerncies
amount: <%= Spree::Money.new(@order.total, currency: @order.currency).amount_in_cents %>
},
requestPayerName: true,
requestPayerEmail: true,
Expand Down
17 changes: 17 additions & 0 deletions spec/controllers/spree/apple_pay_domain_verification_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe Spree::ApplePayDomainVerificationController do
let!(:stripe_apple_pay_payment_method) do
Spree::Gateway::StripeApplePayGateway.create!(
name: 'ApplePay',
preferred_domain_verification_certificate: 'dvc_test_jJG9kgDS75aADOsdfD1fDxQ'
)
end

context '#show' do
it 'renders domain verification certificate' do
get :show
expect(response.body).to eq 'dvc_test_jJG9kgDS75aADOsdfD1fDxQ'
end
end
end
74 changes: 74 additions & 0 deletions spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require 'spec_helper'

describe Spree::Order do
let(:order_one) { OrderWalkthrough.up_to(:payment) }
let(:order_two) { OrderWalkthrough.up_to(:payment) }
let(:stripe_apple_pay_payment_method) do
Spree::Gateway::StripeApplePayGateway.create!(
name: 'ApplePay',
preferred_domain_verification_certificate: 'dvc_test_jJG9kgDS75aADOsdfD1fDxQ'
)
end
let(:stripe_credit_card_payment_method) do
Spree::Gateway::StripeGateway.create!(
name: 'CreditCard',
preferred_secret_key: 'sk_test_aFQiNCcaOlXgDIwJHDdWlGN',
preferred_publishable_key: 'pk_test_ZItEjGEZxRiCuzGZHdsbOaj'
)
end

before do
order_one.payments.create(payment_method_id: stripe_apple_pay_payment_method.id,
amount: 29.99,
state: 'completed')
order_two.payments.create(payment_method_id: stripe_credit_card_payment_method.id,
amount: 29.99,
state: 'completed')
order_one.update_totals
order_two.update_totals
end

describe '#paid_with_apple_pay?' do
context 'when an order was paid with Apply Pay' do
it 'returns true' do
expect(order_one.paid_with_apple_pay?).to eq true
end
end

context 'when an order was paid with Credit Card' do
it 'returns false' do
expect(order_two.paid_with_apple_pay?).to eq false
end
end
end

describe '#confirmation_required?' do
context 'when an order was paid with Apply Pay' do
it 'returns false' do
expect(order_one.confirmation_required?).to eq false
end
end

context 'when an order was paid with Credit Card' do
it 'returns true' do
expect(order_two.confirmation_required?).to eq true
end
end
end

context 'when an order was paid with Apply Pay' do
it 'proceeds to Complete order state' do
order_one.next

expect(order_one.state).to eq 'complete'
end
end

context 'when an order was paid with Credit Card' do
it 'proceeds to Confirm order state' do
order_two.next

expect(order_two.state).to eq 'confirm'
end
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

FactoryBot.find_definitions

require 'spree/order_decorator.rb'
require 'spree/payment_decorator.rb'

RSpec.configure do |config|
config.infer_spec_type_from_file_location!
config.mock_with :rspec do |mock|
Expand Down

0 comments on commit 31f5dce

Please sign in to comment.