Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
Solidus 2.3: Inherit from PaymentMethod and rename provider -> gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-brough committed Jan 9, 2018
1 parent 029260e commit 9137a38
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
8 changes: 4 additions & 4 deletions app/controllers/spree/paypal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def express
items.reject! do |item|
item[:Amount][:value].zero?
end
pp_request = provider.build_set_express_checkout(express_checkout_request_details(order, items))
pp_request = gateway.build_set_express_checkout(express_checkout_request_details(order, items))

begin
pp_response = provider.set_express_checkout(pp_request)
pp_response = gateway.set_express_checkout(pp_request)
if pp_response.success?
redirect_to payment_method.
express_checkout_url(pp_response, useraction: 'commit')
Expand Down Expand Up @@ -113,8 +113,8 @@ def payment_method
Spree::PaymentMethod.find(params[:payment_method_id])
end

def provider
payment_method.provider
def gateway
payment_method.gateway
end

def payment_details items
Expand Down
52 changes: 24 additions & 28 deletions app/models/spree/gateway/pay_pal_express.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'paypal-sdk-merchant'
module Spree
class Gateway::PayPalExpress < Gateway
class Gateway::PayPalExpress < Spree::PaymentMethod
preference :use_new_layout, :boolean, default: true
preference :login, :string
preference :password, :string
Expand All @@ -14,17 +14,17 @@ def supports?(source)
true
end

def provider_class
def gateway_class
::PayPal::SDK::Merchant::API
end

def provider
def gateway
::PayPal::SDK.configure(
mode: preferred_server.present? ? preferred_server : "sandbox",
username: preferred_login,
password: preferred_password,
signature: preferred_signature)
provider_class.new
gateway_class.new
end

def auto_capture?
Expand All @@ -36,12 +36,12 @@ def method_type
end

def purchase(amount, express_checkout, gateway_options={})
details_request = provider.build_get_express_checkout_details({
details_request = gateway.build_get_express_checkout_details({
:Token => express_checkout.token
})
details_response = provider.get_express_checkout_details(details_request)
details_response = gateway.get_express_checkout_details(details_request)

request = provider.build_do_express_checkout_payment(
request = gateway.build_do_express_checkout_payment(
:DoExpressCheckoutPaymentRequestDetails => {
:PaymentAction => "Sale",
:Token => express_checkout.token,
Expand All @@ -50,7 +50,7 @@ def purchase(amount, express_checkout, gateway_options={})
},
)

response = provider.do_express_checkout_payment(request)
response = gateway.do_express_checkout_payment(request)

if response.success?
transaction_id = purchase_transaction_id(response)
Expand Down Expand Up @@ -88,15 +88,15 @@ def credit(credit_cents, transaction_id, originator:, **_options)

refund_type = payment.amount == amount.to_f ? "Full" : "Partial"

refund_transaction = provider.build_refund_transaction(
refund_transaction = gateway.build_refund_transaction(
{ TransactionID: payment.transaction_id,
RefundType: refund_type,
Amount: {
currencyID: payment.currency,
value: amount },
RefundSource: "any" })

refund_transaction_response = provider.refund_transaction(refund_transaction)
refund_transaction_response = gateway.refund_transaction(refund_transaction)

if refund_transaction_response.success?
payment.source.update_attributes(
Expand Down Expand Up @@ -131,23 +131,22 @@ def express_checkout_url(pp_response, extra_params={})
end

def do_authorize(token, payer_id)
response =
self.
provider.
do_express_checkout_payment(
checkout_payment_params(token, payer_id))
response = gateway.do_express_checkout_payment(
checkout_payment_params(token, payer_id)
)

build_response(response, authorization_transaction_id(response))
end

def do_capture(amount_cents, authorization, currency)
response = provider.
do_capture(
provider.build_do_capture(
amount: amount_cents / 100.0,
authorization_id: authorization,
complete_type: "Complete",
currencycode: options[:currency]))
response = gateway.do_capture(
gateway.build_do_capture(
amount: amount_cents / 100.0,
authorization_id: authorization,
complete_type: "Complete",
currencycode: options[:currency],
)
)

build_response(response, capture_transaction_id(response))
end
Expand Down Expand Up @@ -184,17 +183,15 @@ def build_response(response, transaction_id)
end

def payment_details(token)
self.
provider.
gateway.
get_express_checkout_details(
checkout_details_params(token)).
get_express_checkout_details_response_details.
PaymentDetails
end

def checkout_payment_params(token, payer_id)
self.
provider.
gateway.
build_do_express_checkout_payment(
build_checkout_payment_params(
token,
Expand All @@ -203,8 +200,7 @@ def checkout_payment_params(token, payer_id)
end

def checkout_details_params(token)
self.
provider.
gateway.
build_get_express_checkout_details(Token: token)
end

Expand Down
22 changes: 11 additions & 11 deletions spec/models/pay_pal_express_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
describe Spree::Gateway::PayPalExpress do
let(:gateway) { Spree::Gateway::PayPalExpress.create!(name: "PayPalExpress") }
let(:payment_method) { Spree::Gateway::PayPalExpress.create!(name: "PayPalExpress") }

context "payment purchase" do
let(:payment) do
payment = FactoryGirl.create(:payment, payment_method: gateway, amount: 10)
payment = FactoryGirl.create(:payment, payment_method: payment_method, amount: 10)
allow(payment).to receive_messages source: mock_model(Spree::PaypalExpressCheckout, token: 'fake_token', payer_id: 'fake_payer_id', update: true)
payment
end

let(:provider) do
provider = double('Provider')
allow(gateway).to receive_messages(provider: provider)
provider
let(:gateway) do
gateway = double('gateway')
allow(payment_method).to receive_messages(gateway: gateway)
gateway
end

before do
expect(provider).
expect(gateway).
to receive(:build_get_express_checkout_details).
with({Token: 'fake_token'}).
and_return(pp_details_request = double)
Expand All @@ -28,11 +28,11 @@
value: "10.00"
}}))

expect(provider).to receive(:get_express_checkout_details).
expect(gateway).to receive(:get_express_checkout_details).
with(pp_details_request).
and_return(pp_details_response)

expect(provider).
expect(gateway).
to receive(:build_do_express_checkout_payment).
with(
{ DoExpressCheckoutPaymentRequestDetails: {
Expand All @@ -53,7 +53,7 @@
)
allow(response).
to receive_message_chain("do_express_checkout_payment_response_details.payment_info.first.transaction_id").and_return '12345'
expect(provider).
expect(gateway).
to receive(:do_express_checkout_payment).
and_return(response)

Expand All @@ -72,7 +72,7 @@
allow(response).
to receive_message_chain("do_express_checkout_payment_response_details.payment_info.first.transaction_id").and_return '12345'

expect(provider).to receive(:do_express_checkout_payment).and_return(response)
expect(gateway).to receive(:do_express_checkout_payment).and_return(response)

expect { payment.authorize! }.
to raise_error(Spree::Core::GatewayError)
Expand Down

0 comments on commit 9137a38

Please sign in to comment.