diff --git a/app/controllers/solidus_paypal_commerce_platform/orders_controller.rb b/app/controllers/solidus_paypal_commerce_platform/orders_controller.rb index 6158fa66..383920b4 100644 --- a/app/controllers/solidus_paypal_commerce_platform/orders_controller.rb +++ b/app/controllers/solidus_paypal_commerce_platform/orders_controller.rb @@ -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 diff --git a/app/controllers/solidus_paypal_commerce_platform/wizard_controller.rb b/app/controllers/solidus_paypal_commerce_platform/wizard_controller.rb index 3a67b58d..ac9fdeaa 100644 --- a/app/controllers/solidus_paypal_commerce_platform/wizard_controller.rb +++ b/app/controllers/solidus_paypal_commerce_platform/wizard_controller.rb @@ -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 @@ -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 diff --git a/app/models/solidus_paypal_commerce_platform/requests.rb b/app/models/solidus_paypal_commerce_platform/requests.rb index b0c5315e..59d9c8e7 100644 --- a/app/models/solidus_paypal_commerce_platform/requests.rb +++ b/app/models/solidus_paypal_commerce_platform/requests.rb @@ -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) @@ -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" @@ -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" @@ -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" @@ -101,7 +105,7 @@ 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" @@ -109,14 +113,14 @@ def post_order(order, intent) ) 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", diff --git a/spec/models/gateway_spec.rb b/spec/models/gateway_spec.rb index a5600894..a195aa23 100644 --- a/spec/models/gateway_spec.rb +++ b/spec/models/gateway_spec.rb @@ -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" } @@ -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" @@ -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" @@ -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"