From a8c1fd25fc156602a91e7b6e696dcadce39048c4 Mon Sep 17 00:00:00 2001 From: isabellaahc Date: Wed, 26 May 2021 13:49:07 -0400 Subject: [PATCH] Added capture method on Fulltransaction --- .../transaccion_completa/request/__init__.py | 7 ++++++ .../transaccion_completa/response/__init__.py | 19 +++++++++++++++ transbank/transaccion_completa/schema.py | 13 ++++++++++ transbank/transaccion_completa/transaction.py | 23 +++++++++++++++--- .../request/__init__.py | 8 +++++++ .../response/__init__.py | 17 +++++++++++++ transbank/transaccion_completa_mall/schema.py | 14 +++++++++++ .../transaccion_completa_mall/transaction.py | 24 +++++++++++++++---- 8 files changed, 118 insertions(+), 7 deletions(-) diff --git a/transbank/transaccion_completa/request/__init__.py b/transbank/transaccion_completa/request/__init__.py index 400ef8b..c62d772 100644 --- a/transbank/transaccion_completa/request/__init__.py +++ b/transbank/transaccion_completa/request/__init__.py @@ -33,6 +33,13 @@ def __init__(self, amount: float): self.amount = amount +class TransactionCaptureRequest(object): + def __init__(self, buy_order: str, authorization_code: str, capture_amount: float): + self.buy_order = buy_order + self.authorization_code = authorization_code + self.capture_amount = capture_amount + + class TransactionInstallmentsRequest(object): def __init__(self, installments_number: float): self.installments_number = installments_number diff --git a/transbank/transaccion_completa/response/__init__.py b/transbank/transaccion_completa/response/__init__.py index 0ed9d7b..aafa207 100644 --- a/transbank/transaccion_completa/response/__init__.py +++ b/transbank/transaccion_completa/response/__init__.py @@ -54,6 +54,7 @@ def __repr__(self): self.accounting_date, self.transaction_date, self.authorization_code, self.payment_type_code, self.response_code, self.installments_number, self.installments_amount, self.balance) + class TransactionStatusResponse(object): def __init__(self, amount: float, status: str, buy_order: str, session_id: str, card_detail: CardDetail, accounting_date: str, transaction_date: str, authorization_code: str, @@ -119,6 +120,23 @@ def __repr__(self): self.response_code) +class TransactionCaptureResponse(object): + def __init__(self, authorization_code: str, authorization_date: str, captured_amount: float, response_code: str): + self.authorization_code = authorization_code + self.authorization_date = authorization_date + self.captured_amount = captured_amount + self.response_code = response_code + + def __repr__(self): + return """ + authorization_code: {}, + authorization_date: {}, + captured_amount: {}, + response_code: {} + """.format(self.authorization_code, self.authorization_date, self.captured_amount, + self.response_code) + + class TransactionInstallmentsResponse(object): def __init__(self, installments_amount: float, id_query_installments: str, deferred_periods: str): self.installments_amount = installments_amount @@ -131,3 +149,4 @@ def __repr__(self): id_query_installments: {}, deferred_periods: {} """.format(self.installments_amount, self.id_query_installments, self.deferred_periods) + diff --git a/transbank/transaccion_completa/schema.py b/transbank/transaccion_completa/schema.py index 8c39b5f..b5b10ac 100644 --- a/transbank/transaccion_completa/schema.py +++ b/transbank/transaccion_completa/schema.py @@ -81,3 +81,16 @@ class RefundTransactionResponseSchema(Schema): nullified_amount = fields.Raw() balance = fields.Raw() response_code = fields.Raw() + + +class CaptureTransactionRequestSchema(Schema): + buy_order = fields.Str() + authorization_code = fields.Str() + capture_amount = fields.Float() + + +class CaptureTransactionResponseSchema(Schema): + authorization_code = fields.Str() + authorization_date = fields.Str() + captured_amount = fields.Float() + response_code = fields.Raw() diff --git a/transbank/transaccion_completa/transaction.py b/transbank/transaccion_completa/transaction.py index e426eed..99bb836 100644 --- a/transbank/transaccion_completa/transaction.py +++ b/transbank/transaccion_completa/transaction.py @@ -8,14 +8,16 @@ from transbank.error.transaction_commit_error import TransactionCommitError from transbank.error.transaction_refund_error import TransactionRefundError from transbank.error.transaction_status_error import TransactionStatusError +from transbank.error.transaction_capture_error import TransactionCaptureError from transbank.error.transaction_installments_error import TransactionInstallmentsError from transbank.transaccion_completa.request import TransactionCreateRequest, TransactionCommitRequest, \ - TransactionStatusRequest, TransactionRefundRequest, TransactionInstallmentsRequest + TransactionStatusRequest, TransactionRefundRequest, TransactionCaptureRequest, TransactionInstallmentsRequest from transbank.transaccion_completa.response import TransactionCreateResponse, TransactionCommitResponse, \ - TransactionStatusResponse, TransactionRefundResponse, TransactionInstallmentsResponse + TransactionStatusResponse, TransactionRefundResponse, TransactionCaptureResponse, TransactionInstallmentsResponse from transbank.transaccion_completa.schema import CreateTransactionRequestSchema, CreateTransactionResponseSchema, \ CommitTransactionRequestSchema, CommitTransactionResponseSchema, InstallmentsTransactionRequestSchema, \ - InstallmentsTransactionResponseSchema, RefundTransactionRequestSchema, RefundTransactionResponseSchema + InstallmentsTransactionResponseSchema, RefundTransactionRequestSchema, RefundTransactionResponseSchema, \ + CaptureTransactionRequestSchema, CaptureTransactionResponseSchema class Transaction(object): @@ -88,6 +90,21 @@ def refund(cls, token: str, amount: str, options: Options = None): return TransactionRefundResponse(**response_dict) raise TransactionRefundError(message=response_dict["error_message"]) + @classmethod + def capture(cls, token: str, buy_order: str, authorization_code: str, capture_amount: float, options: Options = None): + options = cls.build_options(options) + endpoint = '{}/{}/capture'.format(cls.__base_url(options.integration_type), token) + request = TransactionCaptureRequest(buy_order=buy_order, authorization_code=authorization_code, + capture_amount=capture_amount) + response = requests.put(endpoint, data=CaptureTransactionRequestSchema().dumps(request).data, + headers=HeadersBuilder.build(options)) + response_json = response.text + response_dict = CaptureTransactionResponseSchema().loads(response_json).data + if response.status_code in range (200,209): + return TransactionCaptureResponse(**response_dict) + raise TransactionCaptureError(message=response_dict["error_message"]) + + @classmethod def installments(cls, token: str, installments_number: float, options: Options = None): options = cls.build_options(options) diff --git a/transbank/transaccion_completa_mall/request/__init__.py b/transbank/transaccion_completa_mall/request/__init__.py index a488740..5984851 100644 --- a/transbank/transaccion_completa_mall/request/__init__.py +++ b/transbank/transaccion_completa_mall/request/__init__.py @@ -42,6 +42,14 @@ def __init__(self, commerce_code: str, buy_order: str, amount: float): self.amount = amount +class TransactionCaptureRequest(object): + def __init__(self, commerce_code: str, buy_order: str, authorization_code: str, capture_amount: float): + self.commerce_code = commerce_code + self.buy_order= buy_order + self.authorization_code = authorization_code + self.capture_amount = capture_amount + + class TransactionInstallmentsRequest(object): def __init__(self, installments_number: float, buy_order: str, commerce_code: str): self.installments_number = installments_number diff --git a/transbank/transaccion_completa_mall/response/__init__.py b/transbank/transaccion_completa_mall/response/__init__.py index 2f9594a..0a93e17 100644 --- a/transbank/transaccion_completa_mall/response/__init__.py +++ b/transbank/transaccion_completa_mall/response/__init__.py @@ -87,6 +87,23 @@ def __repr__(self): self.response_code) +class TransactionCaptureResponse(object): + def __init__(self, authorization_code: str, authorization_date: str, + captured_amount: float, response_code: str): + self.authorization_code = authorization_code + self.authorization_date = authorization_date + self.captured_amount = captured_amount + self.response_code = response_code + + def __repr__(self): + return """ + authorization_code: {}, + authorization_date: {}, + captured_amount: {}, + response_code: {} + """.format(self.authorization_code, self.authorization_date, self.captured_amount, self.response_code) + + class TransactionInstallmentsResponse(object): def __init__(self, installments_amount: float, id_query_installments: str, deferred_periods: str): self.installments_amount = installments_amount diff --git a/transbank/transaccion_completa_mall/schema.py b/transbank/transaccion_completa_mall/schema.py index e38b032..17f059c 100644 --- a/transbank/transaccion_completa_mall/schema.py +++ b/transbank/transaccion_completa_mall/schema.py @@ -80,3 +80,17 @@ class RefundTransactionResponseSchema(Schema): nullified_amount = fields.Raw() balance = fields.Raw() response_code = fields.Raw() + + +class CaptureTransactionRequestSchema(Schema): + commerce_code = fields.Str() + buy_order = fields.Str() + authorization_code = fields.Str() + capture_amount = fields.Float() + + +class CaptureTransactionResponseSchema(Schema): + authorization_code = fields.Str() + authorization_date = fields.Str() + captured_amount = fields.Float() + response_code = fields.Raw() diff --git a/transbank/transaccion_completa_mall/transaction.py b/transbank/transaccion_completa_mall/transaction.py index 680db45..7218756 100644 --- a/transbank/transaccion_completa_mall/transaction.py +++ b/transbank/transaccion_completa_mall/transaction.py @@ -8,15 +8,16 @@ from transbank.error.transaction_commit_error import TransactionCommitError from transbank.error.transaction_refund_error import TransactionRefundError from transbank.error.transaction_status_error import TransactionStatusError +from transbank.error.transaction_capture_error import TransactionCaptureError from transbank.error.transaction_installments_error import TransactionInstallmentsError from transbank.transaccion_completa_mall.request import TransactionCreateRequest, TransactionCommitRequest, \ - TransactionStatusRequest, TransactionRefundRequest, TransactionInstallmentsRequest + TransactionStatusRequest, TransactionRefundRequest, TransactionInstallmentsRequest, TransactionCaptureRequest from transbank.transaccion_completa_mall.response import TransactionCreateResponse, TransactionCommitResponse, \ - TransactionStatusResponse, TransactionRefundResponse, TransactionInstallmentsResponse + TransactionStatusResponse, TransactionRefundResponse, TransactionCaptureResponse, TransactionInstallmentsResponse from transbank.transaccion_completa_mall.schema import CreateTransactionRequestSchema, CreateTransactionResponseSchema, \ CommitTransactionRequestSchema, CommitTransactionResponseSchema, InstallmentsTransactionRequestSchema, \ InstallmentsTransactionResponseSchema, RefundTransactionRequestSchema, RefundTransactionResponseSchema, \ - StatusTransactionResponseSchema + StatusTransactionResponseSchema, CaptureTransactionRequestSchema, CaptureTransactionResponseSchema class Transaction(object): @@ -77,7 +78,7 @@ def status(cls, token: str, options: Options = None): raise TransactionStatusError(message=response_dict["error_message"]) @classmethod - def refund(cls, token: str, child_buy_order: str, child_commerce_code:str, amount: str, options: Options = None): + def refund(cls, token: str, child_buy_order: str, child_commerce_code: str, amount: str, options: Options = None): options = cls.build_options(options) endpoint = '{}/{}/refunds'.format(cls.__base_url(options.integration_type), token) request = TransactionRefundRequest(buy_order=child_buy_order, commerce_code=child_commerce_code, amount=amount) @@ -89,6 +90,21 @@ def refund(cls, token: str, child_buy_order: str, child_commerce_code:str, amoun return TransactionRefundResponse(**response_dict) raise TransactionRefundError(message=response_dict["error_message"]) + @classmethod + def capture(cls, token: str, child_commerce_code: str, child_buy_order: str, authorization_code: str, + capture_amount: float, options: Options = None): + options = cls.build_options(options) + endpoint = '{}/{}/capture'.format(cls.__base_url(options.integration_type), token) + request = TransactionCaptureRequest(commerce_code=child_commerce_code, buy_order=child_buy_order, + authorization_code=authorization_code, capture_amount=capture_amount) + response = requests.put(endpoint, data=CaptureTransactionRequestSchema().dumps(request).data, + headers=HeadersBuilder.build(options)) + response_json = response.text + response_dict = CaptureTransactionResponseSchema().loads(response_json).data + if response.status_code in (200, 299): + return TransactionCaptureResponse(**response_dict) + raise TransactionCaptureError(message=response_dict["error_message"]) + @classmethod def installments(cls, token: str, details: list, options: Options = None): options = cls.build_options(options)