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

[Feature] Order - get capture, refund, cancel, update and delete transaction #6

Merged
merged 8 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
29 changes: 29 additions & 0 deletions examples/order/cancel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative '../../lib/mercadopago'


sdk = Mercadopago::SDK.new('<ACCESS_TOKEN>')

def cancel_order(sdk)

order_id = '<ORDER_ID>'

# Set Custom Headers
custom_headers = {
'X-Idempotency-Key': '<SOME_UNIQUE_VALUE>'
}
custom_request_options = Mercadopago::RequestOptions.new(custom_headers: custom_headers)

# Send Request
result = sdk.order.cancel(order_id, request_options: custom_request_options)
order = result[:response]
puts result[:status]
puts order['id']

rescue MercadoPago::MPApiException => e
puts "Status code: #{e.api_response.status_code}"
puts "Content: #{e.api_response.content}"
rescue StandardError => e
puts e.message
end

cancel_order(sdk)
29 changes: 29 additions & 0 deletions examples/order/capture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative '../../lib/mercadopago'


sdk = Mercadopago::SDK.new('<ACCESS_TOKEN>')

def capture_order(sdk)

order_id = '<ORDER_ID>'

# Set Custom Headers
custom_headers = {
'X-Idempotency-Key': '<SOME_UNIQUE_VALUE>'
}
custom_request_options = Mercadopago::RequestOptions.new(custom_headers: custom_headers)

# Send Request
result = sdk.order.capture(order_id, request_options: custom_request_options)
order = result[:response]
puts result[:status]
puts order['id']

rescue MercadoPago::MPApiException => e
puts "Status code: #{e.api_response.status_code}"
puts "Content: #{e.api_response.content}"
rescue StandardError => e
puts e.message
end

capture_order(sdk)
File renamed without changes.
29 changes: 29 additions & 0 deletions examples/order/refund.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative '../../lib/mercadopago'


sdk = Mercadopago::SDK.new('<ACCESS_TOKEN>')

def refund_order(sdk)

order_id = '<ORDER_ID>'

# Set Custom Headers
custom_headers = {
'X-Idempotency-Key': '<SOME_UNIQUE_VALUE>'
}
custom_request_options = Mercadopago::RequestOptions.new(custom_headers: custom_headers)

# Send Request
result = sdk.order.refund(order_id, request_options: custom_request_options)
order = result[:response]
puts result[:status]
puts order['id']

rescue MercadoPago::MPApiException => e
puts "Status code: #{e.api_response.status_code}"
puts "Content: #{e.api_response.content}"
rescue StandardError => e
puts e.message
end

refund_order(sdk)
22 changes: 22 additions & 0 deletions examples/order/transaction/delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require_relative '../../../lib/mercadopago'

sdk = Mercadopago::SDK.new('<ACCESS_TOKEN>')

def delete_order_transaction(sdk)
# Set Custom Headers
custom_headers = {
'X-Idempotency-Key': '<SOME_UNIQUE_VALUE>'
}
custom_request_options = Mercadopago::RequestOptions.new(custom_headers: custom_headers)

# Send Request
result = sdk.order_transaction.delete('<ORDER_ID>', '<TRANSACTION_ID>', request_options: custom_request_options)
puts result[:status]
rescue MercadoPago::MPApiException => e
puts "Status code: #{e.api_response.status_code}"
puts "Content: #{e.api_response.content}"
rescue StandardError => e
puts e.message
end

delete_order_transaction(sdk)
30 changes: 30 additions & 0 deletions examples/order/transaction/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative '../../../lib/mercadopago'

sdk = Mercadopago::SDK.new('<ACCESS_TOKEN>')

def update_order_transaction(sdk)
# Create Transaction Order Update
request = {
payment_method: {
installments: 5
}
}

# Set Custom Headers
custom_headers = {
'X-Idempotency-Key': '<SOME_UNIQUE_VALUE>'
}
custom_request_options = Mercadopago::RequestOptions.new(custom_headers: custom_headers)

# Send Request
result = sdk.order_transaction.update('<ORDER_ID>', '<TRANSACTION_ID>', request, request_options: custom_request_options)
order = result[:response]
puts order
rescue MercadoPago::MPApiException => e
puts "Status code: #{e.api_response.status_code}"
puts "Content: #{e.api_response.content}"
rescue StandardError => e
puts e.message
end

update_order_transaction(sdk)
4 changes: 2 additions & 2 deletions lib/mercadopago/core/mp_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def _post(uri:, data:, request_options: nil)

request_options = _check_request_options(request_options)
headers = _check_headers(request_options, { 'Content-Type': @config.mime_json })
payload = data&.to_json

@http_client.post(url: @config.api_base_url + uri, data: data.to_json, headers: headers,
timeout: request_options.connection_timeout)
@http_client.post(url: @config.api_base_url + uri, data: payload, headers: headers, timeout: request_options.connection_timeout)
Comment on lines +54 to +56
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] Aqui vc só extraiu o data.to_json para uma variável? Por que foi necessário colocar o &?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o é em Ruby é uma validação, não um endereço!

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

é um bug @andreagostinho-meli, se eu fizer data.to_json independente se data tiver valor ou nao ele vai tentar fazer o to_json e irá enviar para o payload um valor NULL.
com o & se data estiver nulo ele nao tenta fazer o to_json, é quase um if ternário

end

def _put(uri:, data:, request_options: nil)
Expand Down
2 changes: 1 addition & 1 deletion lib/mercadopago/http/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def delete(url:, headers:, timeout: nil)
result = RestClient::Request.execute(method: 'delete', url: url, headers: headers, timeout: timeout)
{
status: result.code,
response: JSON.parse(result.body)
response: result.body.is_a?(String) && !result.body.strip.empty? ? JSON.parse(result.body) : nil
}
rescue RestClient::Exception => e
{
Expand Down
16 changes: 16 additions & 0 deletions lib/mercadopago/resources/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,21 @@ def process(order_id, request_options: nil)
_post(uri: "/v1/orders/#{order_id}/process", data: nil,request_options: request_options)
end

def refund(order_id, refund_data: nil, request_options: nil)
unless refund_data.nil?
raise TypeError, 'Param refund_data must be a Hash' unless refund_data.is_a?(Hash)
end

_post(uri: "/v1/orders/#{order_id}/refund", data: refund_data, request_options: request_options)
end

def cancel(order_id, request_options: nil)
_post(uri: "/v1/orders/#{order_id}/cancel", data: nil, request_options: request_options)
end

def capture(order_id, request_options: nil)
_post(uri: "/v1/orders/#{order_id}/capture", data: nil, request_options: request_options)
end

end
end
11 changes: 11 additions & 0 deletions lib/mercadopago/resources/order_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,16 @@ def create(order_id, order_transaction_data, request_options: nil)

_post(uri: "/v1/orders/#{order_id}/transactions", data: order_transaction_data, request_options: request_options)
end

def update(order_id, transaction_id, order_transaction_data, request_options: nil)
raise TypeError, 'Param order_transaction_data must be a Hash' unless order_transaction_data.is_a?(Hash)

_put(uri: "/v1/orders/#{order_id}/transactions/#{transaction_id}", data: order_transaction_data, request_options: request_options)
end

def delete(order_id, transaction_id, request_options: nil)
_delete(uri: "/v1/orders/#{order_id}/transactions/#{transaction_id}", request_options: request_options)
end

end
end
2 changes: 1 addition & 1 deletion tests/test_customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_all
sdk = Mercadopago::SDK.new(ENV['ACCESS_TOKEN'])

customer_object = {
email: 'test_payer_999922@testuser.com',
email: 'test_payer_9999SAE@testuser.com',
first_name: 'Rafa',
last_name: 'Williner',
phone: {
Expand Down
Loading