Skip to content

Commit

Permalink
western union status api
Browse files Browse the repository at this point in the history
  • Loading branch information
domdinicola committed Dec 18, 2024
1 parent cc0ca3e commit 34067b3
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/hope_payment_gateway/apps/fsp/western_union/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def __init__(self) -> None:
self.quote_client = Client(quote_wsdl, transport=transport, settings=settings)
self.quote_client.set_ns_prefix("xrsi", "http://www.westernunion.com/schema/xrsi")

status_wsdl = str(Path(__file__).parent / "wsdl" / "PayStatus_Service_H2HService.wsdl")
self.status_client = Client(status_wsdl, transport=transport, settings=settings)
self.status_client.set_ns_prefix("xrsi", "http://www.westernunion.com/schema/xrsi")

transaction_wsdl = str(Path(__file__).parent / "wsdl" / "SendMoneyStore_Service_H2HService.wsdl")
self.transaction_client = Client(transaction_wsdl, transport=transport, settings=settings)
self.transaction_client.set_ns_prefix("xrsi", "http://www.westernunion.com/schema/xrsi")
Expand Down Expand Up @@ -314,8 +318,30 @@ def create_transaction(self, base_payload, update=True):
return pr

def query_status(self, transaction_id, update):
# western union does not have an API to address this
pass
pr = PaymentRecord.objects.get(
fsp_code=transaction_id, parent__fsp__vendor_number=config.WESTERN_UNION_VENDOR_NUMBER
)
wu_env = config.WESTERN_UNION_WHITELISTED_ENV
frm = pr.extra_data.get("foreign_remote_system", None)
mtcn = pr.extra_data.get("mtcn", None)
payload = {
"channel": {"type": "H2H", "name": "CHANNEL", "version": "9500"},
"mtcn": mtcn,
"foreign_remote_system": frm,
}
response = self.response_context(self.status_client, "PayStatus", payload, f"SOAP_HTTP_Port_{wu_env}")
if update:
wu_status = response["content"]["payment_transactions"]["payment_transaction"][0]["pay_status_description"]
flow = PaymentRecordFlow(pr)
status = {"PAID": PaymentRecordState.TRANSFERRED_TO_BENEFICIARY}.get(wu_status, None)
if pr.status != status:
if status in [PaymentRecordState.TRANSFERRED_TO_BENEFICIARY]:
pr.message = "Payment Record update by manual sync"
pr.success = True
flow.confirm()
pr.save()

return response

def search_request(self, frm, mtcn):
payload = FinancialServiceProvider.objects.get(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.westernunion.com/PayStatus_H2H" xmlns:xrsi="http://www.westernunion.com/schema/xrsi" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="PayStatus_Service_H2H" targetNamespace="http://www.westernunion.com/PayStatus_H2H">
<wsdl:types>
<xsd:schema xmlns:mrm="http://www.westernunion.com/PayStatus" targetNamespace="http://www.westernunion.com/PayStatus" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.westernunion.com/schema/xrsi" schemaLocation="../schema/XRSIPayStatusInquiryRequest.xsd"/>
<xsd:import namespace="http://www.westernunion.com/schema/xrsi" schemaLocation="../schema/XRSIPayStatusInquiryReply.xsd"/>
<xsd:import namespace="http://www.westernunion.com/schema/xrsi" schemaLocation="../schema/XRSIErrorReply.xsd"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="PayStatus_in">
<wsdl:part element="xrsi:pay-status-inquiry-request-data" name="pay-status-inquiry-request-data"/>
</wsdl:message>
<wsdl:message name="PayStatus_out">
<wsdl:part element="xrsi:pay-status-inquiry-reply" name="pay-status-inquiry-reply"/>
</wsdl:message>
<wsdl:message name="PayStatus_fault1">
<wsdl:part element="xrsi:error-reply" name="error-reply"/>
</wsdl:message>
<wsdl:portType name="PayStatusPortType">
<wsdl:operation name="PayStatus">
<wsdl:input message="tns:PayStatus_in" name="PayStatus_Input"/>
<wsdl:output message="tns:PayStatus_out" name="PayStatus_Output"/>
<wsdl:fault message="tns:PayStatus_fault1" name="PayStatus_Fault"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="PayStatus_H2H_SOAP_HTTP_Binding" type="tns:PayStatusPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="PayStatus">
<soap:operation/>
<wsdl:input name="PayStatus_Input">
<soap:body parts="pay-status-inquiry-request-data" use="literal"/>
</wsdl:input>
<wsdl:output name="PayStatus_Output">
<soap:body parts="pay-status-inquiry-reply" use="literal"/>
</wsdl:output>
<wsdl:fault name="PayStatus_Fault">
<soap:fault name="PayStatus_Fault" use="literal"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="PayStatus_Service_H2H">
<wsdl:port binding="tns:PayStatus_H2H_SOAP_HTTP_Binding" name="SOAP_HTTP_Port_uat">
<soap:address location="https://wugateway2pi.westernunion.com/PayStatus_Service_H2H"/>
</wsdl:port>
<wsdl:port binding="tns:PayStatus_H2H_SOAP_HTTP_Binding" name="SOAP_HTTP_Port_prd">
<soap:address location="https://wugateway2.westernunion.com/PayStatus_Service_H2H"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
22 changes: 22 additions & 0 deletions src/hope_payment_gateway/apps/gateway/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def western_union(self, button):
self.wu_prepare_payload,
self.wu_send_money_validation,
self.wu_send_money,
self.wu_status,
self.wu_status_update,
self.wu_search_request,
self.wu_cancel,
]
Expand Down Expand Up @@ -129,6 +131,26 @@ def wu_send_money(self, request, pk) -> TemplateResponse:
loglevel = messages.SUCCESS if log.success else messages.ERROR
messages.add_message(request, loglevel, log.message)

@view(html_attrs={"style": "background-color:yellow;color:blue"}, label="Check Status")
def wu_status(self, request, pk) -> TemplateResponse:
context = self.get_common_context(request, pk)
obj = PaymentRecord.objects.get(pk=pk)
if mtcn := obj.extra_data.get("mtcn", None):
context["msg"] = f"Search request through MTCN \n" f"PARAM: mtcn {mtcn}"
context.update(WesternUnionClient().query_status(obj.fsp_code, False))
return TemplateResponse(request, "request.html", context)
messages.warning(request, "Missing MTCN")

@view(html_attrs={"style": "background-color:yellow;color:blue"}, label="Status Update")
def wu_status_update(self, request, pk) -> TemplateResponse:
context = self.get_common_context(request, pk)
obj = PaymentRecord.objects.get(pk=pk)
if mtcn := obj.extra_data.get("mtcn", None):
context["msg"] = f"Search request through MTCN \n" f"PARAM: mtcn {mtcn}"
context.update(WesternUnionClient().query_status(obj.fsp_code, True))
return TemplateResponse(request, "request.html", context)
messages.warning(request, "Missing MTCN")

@view(html_attrs={"style": "background-color:yellow;color:blue"}, label="Search Request")
def wu_search_request(self, request, pk) -> TemplateResponse:
context = self.get_common_context(request, pk)
Expand Down
25 changes: 25 additions & 0 deletions tests/western_union/endpoints/status.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
responses:
- response:
auto_calculate_content_length: false
body: '<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"><soapenv:Body><xrsi:pay-status-inquiry-reply
xmlns:xrsi="http://www.westernunion.com/schema/xrsi"><payment_transactions><payment_transaction><sender><name
name_type="C"><business_name>UNICEF</business_name></name></sender><receiver><name
name_type="D"><first_name>CONTE</first_name><last_name>ALBERTO</last_name></name></receiver><financials><originators_principal_amount>100000</originators_principal_amount></financials><payment_details><originating_country_currency><iso_code><country_code>SD</country_code><currency_code>SDG</currency_code></iso_code></originating_country_currency></payment_details><filing_date>12/18/24
</filing_date><filing_time>04:41:18</filing_time><money_transfer_key>1048645159</money_transfer_key><pay_status_description>PAID</pay_status_description></payment_transaction></payment_transactions><foreign_remote_system><identifier>IDENTIFIER</identifier><reference_no>REFNO</reference_no><counter_id>COUNTER</counter_id></foreign_remote_system><number_matches>1</number_matches><current_page_number>1</current_page_number><last_page_number>1</last_page_number></xrsi:pay-status-inquiry-reply></soapenv:Body></soapenv:Envelope>'
content_type: text/plain
headers:
Content-Security-Policy: 'frame-ancestors *;script-src ''self'' ''unsafe-inline''
''unsafe-eval'' ; style-src ''self'' ''unsafe-inline'' ; img-src ''self''
data: ; connect-src ''self'' wss:;'
Transfer-Encoding: chunked
X-Backside-Transport: OK OK
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Global-Transaction-ID: 4bc148eb6762b637012641b4
X-XSS-Protection: 1; mode=block
method: POST
status: 200
url: https://wugateway2pi.westernunion.com/PayStatus_Service_H2H
38 changes: 38 additions & 0 deletions tests/western_union/endpoints/test_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import responses
from constance.test import override_config
from factories import PaymentRecordFactory

from hope_payment_gateway.apps.fsp.western_union.api.client import WesternUnionClient
from hope_payment_gateway.apps.gateway.models import PaymentRecordState


# @_recorder.record(file_path="tests/western_union/endpoints/status.yaml")
@responses.activate
@override_config(WESTERN_UNION_VENDOR_NUMBER="12345")
def test_status(django_app, admin_user, wu):
responses.patch("https://wugateway2pi.westernunion.com/Search_Service_H2H")
responses._add_from_file(file_path="tests/western_union/endpoints/status.yaml")
ref_no, mtcn, frm = (
"Y3snz233UkGt1Gw4",
"8560724095",
{
"identifier": "IDENTIFIER",
"reference_no": "REFNO",
"counter_id": "COUNTER",
},
)
pr = PaymentRecordFactory(
fsp_code=mtcn,
record_code=ref_no,
extra_data={
"mtcn": mtcn,
"foreign_remote_system": frm,
"channel": {"type": "H2H", "name": "TEST", "version": "9500"},
},
parent__fsp=wu,
status=PaymentRecordState.TRANSFERRED_TO_FSP,
)
resp = WesternUnionClient().query_status(pr.fsp_code, True)
pr.refresh_from_db()
assert pr.status == PaymentRecordState.TRANSFERRED_TO_BENEFICIARY
assert (resp["title"], resp["code"]) == ("PayStatus", 200)

0 comments on commit 34067b3

Please sign in to comment.