From 09d9aaedde72eb356763ecbdced860ead6f5f40d Mon Sep 17 00:00:00 2001 From: Avery Kushner Date: Wed, 6 Apr 2022 14:24:53 -0700 Subject: [PATCH] Adds support for simulating incoming ACH --- unit/api/payment_resource.py | 12 +++++++++++- unit/models/codecs.py | 2 +- unit/models/payment.py | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/unit/api/payment_resource.py b/unit/api/payment_resource.py index 6ef30a43..33c13fda 100644 --- a/unit/api/payment_resource.py +++ b/unit/api/payment_resource.py @@ -1,6 +1,6 @@ from unit.api.base_resource import BaseResource from unit.models.payment import * -from unit.models.codecs import DtoDecoder +from unit.models.codecs import DtoDecoder, split_json_api_single_response class PaymentResource(BaseResource): @@ -45,3 +45,13 @@ def list(self, params: ListPaymentParams = None) -> Union[UnitResponse[List[Paym else: return UnitError.from_json_api(response.json()) + def simulate_incoming_ach(self, request: SimulateIncomingAchRequest) -> Union[UnitResponse[PaymentDTO], UnitError]: + payload = request.to_json_api() + response = super().post(f"sandbox/{self.resource}", payload) + if super().is_20x(response.status_code): + data = response.json().get("data") + # TODO Fix dto + _id, _type, attributes, relationships = split_json_api_single_response(data) + return UnitResponse[SimulateIncomingAchPaymentDTO](SimulateIncomingAchPaymentDTO.from_json_api(_id, _type, attributes, relationships), None) + else: + return UnitError.from_json_api(response.json()) diff --git a/unit/models/codecs.py b/unit/models/codecs.py index 78d11d8c..bccb4852 100644 --- a/unit/models/codecs.py +++ b/unit/models/codecs.py @@ -9,7 +9,7 @@ from unit.models.card import IndividualDebitCardDTO, BusinessDebitCardDTO, IndividualVirtualDebitCardDTO,\ BusinessVirtualDebitCardDTO, PinStatusDTO, CardLimitsDTO from unit.models.transaction import * -from unit.models.payment import AchPaymentDTO, BookPaymentDTO, WirePaymentDTO +from unit.models.payment import AchPaymentDTO, BookPaymentDTO, WirePaymentDTO, SimulateIncomingAchPaymentDTO from unit.models.customerToken import CustomerTokenDTO, CustomerVerificationTokenDTO from unit.models.fee import FeeDTO from unit.models.event import * diff --git a/unit/models/payment.py b/unit/models/payment.py index 3e80116d..80c1161c 100644 --- a/unit/models/payment.py +++ b/unit/models/payment.py @@ -35,6 +35,29 @@ def from_json_api(_id, _type, attributes, relationships): attributes["amount"], attributes.get("addenda"), attributes.get("reason"), settlement_date, attributes.get("tags"), relationships) +class SimulateIncomingAchPaymentDTO(BasePayment): + def __init__(self, id: str, created_at: datetime, status: AchStatus, direction: str, + description: str, amount: int, reason: Optional[str], + settlement_date: Optional[datetime], tags: Optional[Dict[str, str]], + relationships: Optional[Dict[str, Relationship]]): + BasePayment.__init__(self, id, created_at, direction, description, amount, reason, tags, relationships) + self.type = 'achPayment' + self.attributes["status"] = status + self.settlement_date = settlement_date + + @staticmethod + def from_json_api(_id, _type, attributes, relationships): + return BasePayment( + _id, + date_utils.to_datetime(attributes["createdAt"]), + attributes.get("direction"), + attributes["description"], + attributes["amount"], + attributes.get("reason"), + attributes.get("tags"), + relationships + ) + class BookPaymentDTO(BasePayment): def __init__(self, id: str, created_at: datetime, status: str, direction: Optional[str], description: str, amount: int, reason: Optional[str], tags: Optional[Dict[str, str]], @@ -140,6 +163,20 @@ def to_json_api(self) -> Dict: return payload +class SimulateIncomingAchRequest(CreatePaymentBaseRequest): + def __init__( + self, amount: int, description: str, + relationships: Dict[str, Relationship], + direction: str = "Credit" + ): + CreatePaymentBaseRequest.__init__(self, amount, description, relationships, None, None, direction) + self.verify_counterparty_balance = False + + def to_json_api(self) -> Dict: + payload = CreatePaymentBaseRequest.to_json_api(self) + + return payload + class CreateVerifiedPaymentRequest(CreatePaymentBaseRequest): def __init__(self, amount: int, description: str, plaid_processor_token: str, relationships: Dict[str, Relationship], counterparty_name: Optional[str], verify_counterparty_balance: Optional[bool],