Skip to content

Commit

Permalink
Merge pull request #89 from seand7565/error_catching
Browse files Browse the repository at this point in the history
Add better error handling to button_actions.js
  • Loading branch information
elia authored Aug 12, 2020
2 parents fb97cba + e09d864 commit 8d35862
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ SolidusPaypalCommercePlatform.hideOverlay = function() {
document.getElementById("paypal_commerce_platform_overlay").style.display = "none";
}

SolidusPaypalCommercePlatform.handleError = function(error) {
console.log(error.name, error.message)
console.log("PayPal Debug ID: " + error.debug_id)
alert("There was a problem connecting with PayPal.")
}

SolidusPaypalCommercePlatform.sendOrder = function(payment_method_id) {
return Spree.ajax({
url: '/solidus_paypal_commerce_platform/paypal_orders/' + Spree.current_order_id,
Expand All @@ -14,8 +20,10 @@ SolidusPaypalCommercePlatform.sendOrder = function(payment_method_id) {
payment_method_id: payment_method_id,
order_token: Spree.current_order_token
}
}).then(function(response) {
return response.table.id;
}).then(function(success_response) {
return success_response.table.result.table.id
}, function(failure_response) {
return failure_response.responseJSON.table.error.table
})
}

Expand Down Expand Up @@ -97,8 +105,10 @@ SolidusPaypalCommercePlatform.shippingChange = function(data, actions) {
actions.reject()
}
}).then(function(response) {
actions.order.patch([response]);
});
actions.order.patch([response]).catch(function() {
actions.reject()
})
})
}

SolidusPaypalCommercePlatform.verifyTotal = function(paypal_total) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ SolidusPaypalCommercePlatform.renderButton = function(payment_method_id, style)
style: style,
createOrder: SolidusPaypalCommercePlatform.sendOrder.bind(null, payment_method_id),
onApprove: SolidusPaypalCommercePlatform.approveOrder,
onShippingChange: SolidusPaypalCommercePlatform.shippingChange
onShippingChange: SolidusPaypalCommercePlatform.shippingChange,
onError: SolidusPaypalCommercePlatform.handleError
}).render('#paypal-button-container')
}

Expand All @@ -12,7 +13,8 @@ SolidusPaypalCommercePlatform.renderCartButton = function(payment_method_id, sty
style: style,
createOrder: SolidusPaypalCommercePlatform.sendOrder.bind(null, payment_method_id),
onApprove: SolidusPaypalCommercePlatform.finalizeOrder.bind(null, payment_method_id),
onShippingChange: SolidusPaypalCommercePlatform.shippingChange
onShippingChange: SolidusPaypalCommercePlatform.shippingChange,
onError: SolidusPaypalCommercePlatform.handleError
}).render('#paypal-button-container')
}

Expand All @@ -21,6 +23,7 @@ SolidusPaypalCommercePlatform.renderProductButton = function(payment_method_id,
style: style,
createOrder: SolidusPaypalCommercePlatform.createAndSendOrder.bind(null, payment_method_id),
onApprove: SolidusPaypalCommercePlatform.finalizeOrder.bind(null, payment_method_id),
onShippingChange: SolidusPaypalCommercePlatform.shippingChange
onShippingChange: SolidusPaypalCommercePlatform.shippingChange,
onError: SolidusPaypalCommercePlatform.handleError
}).render('#paypal-button-container')
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class PaypalOrdersController < ::Spree::Api::BaseController

def show
authorize! :show, @order, order_token
render json: @payment_method.gateway.create_order(@order, @payment_method.auto_capture), status: :ok
order_request = @payment_method.gateway.create_order(@order, @payment_method.auto_capture)

render json: order_request, status: order_request.status_code
end

private
Expand Down
3 changes: 1 addition & 2 deletions app/models/solidus_paypal_commerce_platform/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ def create_order(order, auto_capture)
request = OrdersCreateRequest.new
paypal_order = SolidusPaypalCommercePlatform::PaypalOrder.new(order)
request.request_body paypal_order.to_json(intent)

@client.execute(request).result
@client.execute(request)
end

def get_order(order_id)
Expand Down
5 changes: 3 additions & 2 deletions lib/solidus_paypal_commerce_platform/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ def initialize(test_mode: nil, client_id:, client_secret: "")

def execute(request)
@paypal_client.execute(request)
rescue PayPalHttp::HttpError
OpenStruct.new(status_code: nil)
rescue PayPalHttp::HttpError => e
Rails.logger.error e.result
OpenStruct.new(status_code: 422, error: e.result)
end

def execute_with_response(request, success_message: nil, failure_message: nil)
Expand Down

0 comments on commit 8d35862

Please sign in to comment.