Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and refactoring #20

Merged
merged 5 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ class OrdersController < ::Spree::Api::BaseController

def create
authorize! :update, @order, order_token
request = SolidusPaypalCommercePlatform::Requests.new(
@payment_method.client_id, @payment_method.client_secret
).create_order(@order, @payment_method.auto_capture)
@payment_method.request.create_order(@order, @payment_method.auto_capture)
render json: request, status: :ok
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ class WizardController < ::Spree::Api::BaseController

def create
authorize! :create, Spree::PaymentMethod
api_credentials = fetch_api_credentials(params)
if payment_method = create_payment_method(api_credentials)

@payment_method = Spree::PaymentMethod.new(payment_method_params)

if @payment_method.save
flash[:success] = "The PayPal Commerce Platform payment method has been successfully created"
render json: {
redirectUrl: spree.edit_admin_payment_method_url(payment_method.id)
redirectUrl: spree.edit_admin_payment_method_url(@payment_method.id)
}, status: :ok
else
render json: {}, status: 500
Expand All @@ -17,22 +19,22 @@ def create

private

def fetch_api_credentials(credentials)
SolidusPaypalCommercePlatform::Requests.new(credentials[:sharedId]).trade_tokens(credentials)
end

def create_payment_method(api_credentials)
return false unless api_credentials
Spree::PaymentMethod.create(
def payment_method_params
{
name: "PayPal Commerce Platform",
type: SolidusPaypalCommercePlatform::Gateway,
preferences: {
client_id: api_credentials.client_id,
client_secret: api_credentials.client_secret,
test_mode: !(Rails.env == "production")
}
)
preferred_client_id: api_credentials.client_id,
preferred_client_secret: api_credentials.client_secret,
preferred_test_mode: !Rails.env.production?
}
end

def api_credentials
@api_credentials ||= begin
paypal_client = SolidusPaypalCommercePlatform::Requests.new(params[:sharedId])

paypal_client.trade_tokens(params)
end
end
end
end
22 changes: 13 additions & 9 deletions app/models/solidus_paypal_commerce_platform/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ def initialize(client_id, client_secret = "")
end

def trade_tokens(credentials)
access_token = get_access_token(credentials).result.access_token
get_api_credentials({accessToken:access_token}).result
access_token = get_access_token(
auth_code: credentials.fetch(:authCode),
nonce: credentials.fetch(:nonce),
).result.access_token

get_api_credentials(accessToken: access_token).result
end

def create_order(order, auto_capture)
Expand Down Expand Up @@ -58,7 +62,7 @@ def post_authorize(order_number)
path: "/v2/checkout/orders/#{order_number}/authorize",
headers: {
"Content-Type" => "application/json",
"Authoriation" => @env.authorizationString(),
"Authorization" => @env.authorizationString(),
"PayPal-Partner-Attribution-Id" => "Solidus_PCP_SP",
},
verb: "POST"
Expand All @@ -72,7 +76,7 @@ def post_capture_authorized(authorization_id)
path: "/v2/payments/authorizations/#{authorization_id}/capture",
headers: {
"Content-Type" => "application/json",
"Authoriation" => @env.authorizationString(),
"Authorization" => @env.authorizationString(),
"PayPal-Partner-Attribution-Id" => "Solidus_PCP_SP",
},
verb: "POST"
Expand All @@ -86,7 +90,7 @@ def post_capture(order_number)
path: "/v2/checkout/orders/#{order_number}/capture",
headers: {
"Content-Type" => "application/json",
"Authoriation" => @env.authorizationString(),
"Authorization" => @env.authorizationString(),
"PayPal-Partner-Attribution-Id" => "Solidus_PCP_SP",
},
verb: "POST"
Expand All @@ -101,22 +105,22 @@ def post_order(order, intent)
body: SolidusPaypalCommercePlatform::PaypalOrder.new(order).to_json(intent),
headers: {
"Content-Type" => "application/json",
"Authoriation" => @env.authorizationString(),
"Authorization" => @env.authorizationString(),
"PayPal-Partner-Attribution-Id" => "Solidus_PCP_SP",
},
verb: "POST"
})
)
end

def get_access_token(credentials)
def get_access_token(auth_code:, nonce:)
@client.execute(
Request.new({
path: "/v1/oauth2/token",
body: {
grant_type: "authorization_code",
code: credentials[:authCode],
code_verifier: credentials[:nonce]
code: auth_code,
code_verifier: nonce,
},
headers: {
"Content-Type" => "application/x-www-form-urlencoded",
Expand Down
18 changes: 9 additions & 9 deletions spec/models/gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

before do
response = OpenStruct.new(
status_code: 201,
status_code: 201,
result: OpenStruct.new(
purchase_units: [
OpenStruct.new(
payments: OpenStruct.new(
authorizations: [
OpenStruct.new(id: SecureRandom.hex(4))
]
)
]
)
]
)
)
)
]
)
)

allow_any_instance_of(PayPal::PayPalHttpClient).to receive(:execute) { response }
allow_any_instance_of(PayPal::SandboxEnvironment).to receive(:authorizationString) { "test auth" }
Expand All @@ -33,7 +33,7 @@
path: "/v2/checkout/orders/#{paypal_order_id}/capture",
headers: {
"Content-Type" => "application/json",
"Authoriation" => "test auth",
"Authorization" => "test auth",
"PayPal-Partner-Attribution-Id" => "Solidus_PCP_SP",
},
verb: "POST"
Expand All @@ -51,7 +51,7 @@
path: "/v2/checkout/orders/#{paypal_order_id}/authorize",
headers: {
"Content-Type" => "application/json",
"Authoriation" => "test auth",
"Authorization" => "test auth",
"PayPal-Partner-Attribution-Id" => "Solidus_PCP_SP",
},
verb: "POST"
Expand All @@ -70,7 +70,7 @@
path: "/v2/payments/authorizations/#{authorization_id}/capture",
headers: {
"Content-Type" => "application/json",
"Authoriation" => "test auth",
"Authorization" => "test auth",
"PayPal-Partner-Attribution-Id" => "Solidus_PCP_SP",
},
verb: "POST"
Expand Down