Skip to content

Commit

Permalink
Send amount of payment to PayPal on capture
Browse files Browse the repository at this point in the history
Previously, we were just telling PayPal to capture the amount that
was authorized. However, admin users have the ability to edit the
amount of the payment, so now we're sending the amount of the payment
to PayPal for capturing.

You can capture less than the authorized amount just fine, however
trying to capture MORE than that amount will fail the payment and
flash a "Something went wrong" error. I think that's fine for the
functionality we want - I would expect to not be able to charge
more than the customer authorized.

Should fix solidusio#29
  • Loading branch information
seand7565 committed Jul 9, 2020
1 parent bf696e9 commit d9d917f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 12 additions & 6 deletions app/models/solidus_paypal_commerce_platform/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ def initialize(options)
@options = options
end

def purchase(_money, source, _options)
response = @client.execute_with_response(OrdersCaptureRequest.new(source.paypal_order_id))
def purchase(money, source, options)
request = OrdersCaptureRequest.new(source.paypal_order_id)
request.request_body(amount: { currency_code: options[:currency], value: Money.new(money).dollars })
response = @client.execute_with_response(request)
capture_id = response.params["result"].purchase_units[0].payments.captures[0].id
source.update(capture_id: capture_id) if response.success?
response
Expand All @@ -33,11 +35,15 @@ def authorize(_money, source, _options)
response
end

def capture(_money, _response_code, options)
def capture(money, _response_code, options)
authorization_id = options[:originator].source.authorization_id
response = @client.execute_with_response(AuthorizationsCaptureRequest.new(authorization_id))
capture_id = response.params["result"].id
options[:originator].source.update(capture_id: capture_id) if response.success?
request = AuthorizationsCaptureRequest.new(authorization_id)
request.request_body(amount: { currency_code: options[:currency], value: Money.new(money).dollars })
response = @client.execute_with_response(request)
if response.success?
capture_id = response.params["result"].id
options[:originator].source.update(capture_id: capture_id)
end
response
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
it "sends a purchase request to paypal" do
paypal_order_id = SecureRandom.hex(8)
source = paypal_payment_method.payment_source_class.create(paypal_order_id: paypal_order_id)
expect_request(:OrdersCaptureRequest).to receive(:new).with(paypal_order_id)
expect_request(:OrdersCaptureRequest).to receive(:new).with(paypal_order_id).and_call_original
paypal_payment_method.purchase(1000, source, {})
end
end
Expand All @@ -54,7 +54,7 @@
authorization_id = SecureRandom.hex(8)
source = paypal_payment_method.payment_source_class.create(authorization_id: authorization_id)
payment.source = source
expect_request(:AuthorizationsCaptureRequest).to receive(:new).with(authorization_id)
expect_request(:AuthorizationsCaptureRequest).to receive(:new).with(authorization_id).and_call_original
paypal_payment_method.capture(1000, {}, originator: payment)
end
end
Expand Down

0 comments on commit d9d917f

Please sign in to comment.