Skip to content
Closed
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
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repos:
- repo: https://github.com/melisource/fury_websec-git-hooks
rev: v2.0.0
hooks:
- id: pre_commit_hook
stages: [commit]
- id: post_commit_hook
stages: [post-commit]

- repo: https://github.com/melisource/fury_datasec-git-hooks
rev: 1.2.2
hooks:
- id: pre_commit_hook
stages: [commit]
- id: post_commit_hook
stages: [post-commit]
14 changes: 9 additions & 5 deletions mercadopago/http/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ def request(self, method, url, maxretries=None, **kwargs):
http.mount("https://", HTTPAdapter(max_retries=retry_strategy))
with http as session:
api_result = session.request(method, url, **kwargs)
response = {
"status": api_result.status_code,
"response": api_result.json()
}
response = {"status": api_result.status_code, "response": None}

return response
if api_result.status_code != 204 and api_result.content:
try:
response["response"] = api_result.json()
except ValueError as e:
print(f"Failed to parse JSON: {str(e)}")
response["response"] = None

return response

def get(self, url, headers, params=None, timeout=None, maxretries=None): # pylint: disable=too-many-arguments
"""Makes a GET request to the API"""
Expand Down
2 changes: 2 additions & 0 deletions mercadopago/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from mercadopago.resources.disbursement_refund import DisbursementRefund
from mercadopago.resources.identification_type import IdentificationType
from mercadopago.resources.merchant_order import MerchantOrder
from mercadopago.resources.order import Order
from mercadopago.resources.payment import Payment
from mercadopago.resources.payment_methods import PaymentMethods
from mercadopago.resources.plan import Plan
Expand All @@ -31,6 +32,7 @@
'HttpClient',
'IdentificationType',
'MerchantOrder',
'Order',
'Payment',
'PaymentMethods',
'Plan',
Expand Down
207 changes: 207 additions & 0 deletions mercadopago/resources/order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
"""
Module: order
"""
from mercadopago.core import MPBase

class Order(MPBase):
"""
This class provides the methods to access the API that will allow you to create
your own order experience on your website.

From basic to advanced configurations, you control the whole experience.

[Click here for more info](https://www.mercadopago.com/developers/en/guides/online-payments/checkout-api/introduction/) # pylint: disable=line-too-long
"""

def create(self, order_object, request_options=None):
"""[Click here for more info](https://www.mercadopago.com/developers/en/reference/order/online-payments/create/post/) # pylint: disable=line-too-long

Args:
order_object (dict): Order to be created
request_options (mercadopago.config.request_options, optional): An instance of
RequestOptions can be pass changing or adding custom options to the REST call.
Defaults to None.

Raises:
ValueError: Param order_object must be a Dictionary

Returns:
dict: Order creation response
"""
if not isinstance(order_object, dict):
raise ValueError("Param order_object must be a Dictionary")

return self._post(uri="/v1/orders", data=order_object, request_options=request_options)

def get(self, order_id, request_options=None):
"""[Click here for more info](https://www.mercadopago.com/developers/en/reference/order/online-payments/get-order/get ) # pylint: disable=line-too-long

Args:
order_id (str): The Order ID
request_options (mercadopago.config.request_options, optional): An instance of
RequestOptions can be pass changing or adding custom options to the REST call.
Defaults to None.

Raises:
ValueError: Param order_id must be a string

Returns:
dict: Order returned in the response to the request made for its creation.
"""

if not isinstance(order_id, str):
raise ValueError("Param order_id must be a string")

return self._get(uri="/v1/orders/" + str(order_id), request_options=request_options)

def process(self, order_id, request_options=None):
"""[Click here for more info](https://www.mercadopago.com/developers/en/reference/order/online/process-order/post) # pylint: disable=line-too-long
Args:
order_id (str): ID of the order to be processed. This value is returned in the response to the Create order request.
request_options (mercadopago.config.request_options, optional): An instance of
RequestOptions can be pass changing or adding custom options to the REST call.
Defaults to None.

Raises:
ValueError: Param order_id must be a string
Returns:
dict: Order returned in the response to the request made for its creation.
"""

if not isinstance(order_id, str):
raise ValueError("Param order_id must be a string")

return self._post(uri="/v1/orders/" + str(order_id) + "/process", request_options=request_options)

def cancel(self, order_id, request_options=None):
"""[Click here for more info](https://www.mercadopago.com/developers/en/reference/order/in-person-payments/point/cancel-order/post) # pylint: disable=line-too-long
Args:
order_id (str): Order ID
request_options (mercadopago.config.request_options, optional): An instance of
RequestOptions can be pass changing or adding custom options to the REST call.
Defaults to None.

Raises:
ValueError: Param order_id must be a string

Returns:
dict: Order cancellation response
"""
if not isinstance(order_id, str):
raise ValueError("Param order_id must be a string")

return self._post(uri="/v1/orders/" + str(order_id) + "/cancel", request_options=request_options)

def capture(self, order_id, request_options=None):
"""[Click here for more info](https://www.mercadopago.com/developers/en/reference/order/online-payments/capture/post) # pylint: disable=line-too-long
Args:
order_id (str): ID of the order to be captured. This value is returned in the response to the Create order request.
request_options (mercadopago.config.request_options, optional): An instance of
RequestOptions can be pass changing or adding custom options to the REST call.
Defaults to None.
Raises:
ValueError: Param order_id must be a string
Returns:
dict: Order returned in the response to the request made for its creation.
"""

if not isinstance(order_id, str):
raise ValueError("Param order_id must be a string")

return self._post(uri="/v1/orders/" + str(order_id) + "/capture", request_options=request_options)

def create_transaction(self, order_id, transaction_object, request_options=None):
"""[Click here for more info](https://www.mercadopago.com/developers/en/reference/order/online-payments/add-transaction/post) # pylint: disable=line-too-long

Args:
order_id (str): The ID of the order to which the transaction will be added
transaction_object (dict): Transaction to be added
request_options (mercadopago.config.request_options, optional): An instance of
RequestOptions can be pass changing or adding custom options to the REST call.
Defaults to None.

Raises:
ValueError: Param transaction_object must be a Dictionary

Returns:
dict: Transaction created response
"""
if not isinstance(transaction_object, dict):
raise ValueError("Param transaction_object must be a Dictionary")

response = self._post(uri=f"/v1/orders/{order_id}/transactions", data=transaction_object,
request_options=request_options)
if response.get("status") != 201:
raise Exception(f"Failed to add transaction: {response}")
return response

def update_transaction(self, order_id, transaction_id, transaction_object, request_options=None):
"""[Click here for more info](https://www.mercadopago.com/developers/en/reference/order/online-payments/update-transaction/put) # pylint: disable=line-too-long

Args:
order_id (str): The ID of the order to which the transaction belongs
transaction_id (str): The ID of the transaction to be updated
transaction_object (dict): Transaction details to be updated
request_options (mercadopago.config.request_options, optional): An instance of
RequestOptions can be pass changing or adding custom options to the REST call.
Defaults to None.

Raises:
ValueError: Param transaction_object must be a Dictionary

Returns:
dict: Transaction update response
"""
if not isinstance(transaction_object, dict):
raise ValueError("Param transaction_object must be a Dictionary")

response = self._put(uri=f"/v1/orders/{order_id}/transactions/{transaction_id}", data=transaction_object,
request_options=request_options)
if response.get("status") != 200:
raise Exception(f"Failed to update transaction: {response}")
return response

def refund_transaction(self, order_id, transaction_object=None, request_options=None):
"""[Click here for more info](https://www.mercadopago.com/developers/en/reference/order/online-payments/refund/post) # pylint: disable=line-too-long
Args:
order_id (str): The ID of the order to which the transaction belongs
transaction_object (dict, optional): Transaction details to be updated
request_options (mercadopago.config.request_options, optional): An instance of
RequestOptions can be pass changing or adding custom options to the REST call.
Defaults to None.
Raises:
ValueError: Param transaction_object must be a Dictionary
Returns:
dict: Order refunded response
"""
if transaction_object is not None and not isinstance(transaction_object, dict):
raise ValueError("Param transaction_object must be a Dictionary")

response = self._post(uri=f"/v1/orders/{order_id}/refund", data=transaction_object,
request_options=request_options)
if response.get("status") != 201:
raise Exception(f"Failed to refund transaction: {response}")
return response

def delete_transaction(self, order_id, transaction_id, request_options=None):
"""[Click here for more info](https://www.mercadopago.com/developers/en/reference/order/online-payments/delete-transaction/delete) # pylint: disable=line-too-long
Args:
order_id (str): The ID of the order to which the transaction belongs
transaction_id (str): The ID of the transaction to be deleted
request_options (mercadopago.config.request_options, optional): An instance of
RequestOptions can be pass changing or adding custom options to the REST call.
Defaults to None.
Raises:
ValueError: Params order_id and transaction_id must be strings
Returns:
Status 204 - No Content - if deleted successfully
"""
if not isinstance(order_id, str) or not isinstance(transaction_id, str):
raise ValueError("Params order_id and transaction_id must be strings")

response = self._delete(uri=f"/v1/orders/{order_id}/transactions/{transaction_id}",
request_options=request_options)

if response.get("status") != 204:
raise Exception(f"Failed to delete transaction: {response}")
return response
27 changes: 18 additions & 9 deletions mercadopago/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DisbursementRefund,
IdentificationType,
MerchantOrder,
Order,
Payment,
PaymentMethods,
Plan,
Expand All @@ -33,15 +34,16 @@ class SDK:
5. Disbursement Refund
6. Identification Type
7. Merchant Order
8. Payment Methods
9. Payment
10. Preapproval
11. Preference
12. Refund
13. User
14. Chargeback
15. Subscription
16. Plan
8. Order
9. Payment Methods
10. Payment
11. Preapproval
12. Preference
13. Refund
14. User
15. Chargeback
16. Subscription
17. Plan
"""

def __init__(
Expand Down Expand Up @@ -120,6 +122,13 @@ def merchant_order(self, request_options=None):
"""
return MerchantOrder(request_options is not None and request_options
or self.request_options, self.http_client)

def order(self, request_options=None):
"""
Returns the attribute value of the function
"""
return Order(request_options is not None and request_options
or self.request_options, self.http_client)

def payment(self, request_options=None):
"""
Expand Down
Loading
Loading