From 1180684aa706b56bf182e5d1ade07f1b79c1b7a9 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Wed, 12 Jun 2024 12:02:28 -0500 Subject: [PATCH] Save airtime transfer transaction id as result and on event as external_id --- flows/actions/testdata/transfer_airtime.json | 4 +- flows/actions/transfer_airtime.go | 2 +- flows/events/airtime_transferred.go | 4 +- flows/events/base_test.go | 4 +- flows/services.go | 1 + services/airtime/dtone/service.go | 3 +- services/airtime/dtone/service_test.go | 3 +- .../airtime.test_successful_transfer.json | 106 +++++++++--------- 8 files changed, 69 insertions(+), 58 deletions(-) diff --git a/flows/actions/testdata/transfer_airtime.json b/flows/actions/testdata/transfer_airtime.json index 33a4f69f6..46a215a04 100644 --- a/flows/actions/testdata/transfer_airtime.json +++ b/flows/actions/testdata/transfer_airtime.json @@ -197,6 +197,7 @@ "created_on": "2018-10-18T14:20:30.000123456Z", "step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", "transfer_uuid": "9688d21d-95aa-4bed-afc7-f31b35731a3d", + "external_id": "2237512891", "sender": "tel:+17036975131", "recipient": "tel:+12065551212?channel=57f1078f-88aa-46f4-a59a-948a5739c03d&id=123", "currency": "USD", @@ -240,7 +241,7 @@ "created_on": "2018-10-18T14:20:30.000123456Z", "step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", "name": "Reward Transfer", - "value": "3", + "value": "2237512891", "category": "Success" } ], @@ -296,6 +297,7 @@ "created_on": "2018-10-18T14:20:30.000123456Z", "step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", "transfer_uuid": "9688d21d-95aa-4bed-afc7-f31b35731a3d", + "external_id": "", "sender": "tel:+17036975131", "recipient": "tel:+12065551212?channel=57f1078f-88aa-46f4-a59a-948a5739c03d&id=123", "currency": "", diff --git a/flows/actions/transfer_airtime.go b/flows/actions/transfer_airtime.go index 79df5a195..4cc6318ae 100644 --- a/flows/actions/transfer_airtime.go +++ b/flows/actions/transfer_airtime.go @@ -96,7 +96,7 @@ func (a *TransferAirtimeAction) transfer(run flows.Run, logEvent flows.EventCall } func (a *TransferAirtimeAction) saveSuccess(run flows.Run, step flows.Step, transfer *flows.AirtimeTransfer, logEvent flows.EventCallback) { - a.saveResult(run, step, a.ResultName, transfer.ActualAmount.String(), CategorySuccess, "", "", nil, logEvent) + a.saveResult(run, step, a.ResultName, transfer.ExternalID, CategorySuccess, "", "", nil, logEvent) } func (a *TransferAirtimeAction) saveFailure(run flows.Run, step flows.Step, logEvent flows.EventCallback) { diff --git a/flows/events/airtime_transferred.go b/flows/events/airtime_transferred.go index ab5355121..f243c4ec6 100644 --- a/flows/events/airtime_transferred.go +++ b/flows/events/airtime_transferred.go @@ -3,7 +3,6 @@ package events import ( "github.com/nyaruka/gocommon/urns" "github.com/nyaruka/goflow/flows" - "github.com/shopspring/decimal" ) @@ -20,6 +19,7 @@ const TypeAirtimeTransferred string = "airtime_transferred" // "type": "airtime_transferred", // "created_on": "2006-01-02T15:04:05Z", // "transfer_uuid": "552cd7ee-ccba-404d-9692-c1fe3b8d57c5", +// "external_id": "12345678", // "sender": "tel:4748", // "recipient": "tel:+1242563637", // "currency": "RWF", @@ -42,6 +42,7 @@ type AirtimeTransferredEvent struct { BaseEvent TransferUUID flows.AirtimeTransferUUID `json:"transfer_uuid"` + ExternalID string `json:"external_id"` Sender urns.URN `json:"sender"` Recipient urns.URN `json:"recipient"` Currency string `json:"currency"` @@ -55,6 +56,7 @@ func NewAirtimeTransferred(t *flows.AirtimeTransfer, httpLogs []*flows.HTTPLog) return &AirtimeTransferredEvent{ BaseEvent: NewBaseEvent(TypeAirtimeTransferred), TransferUUID: t.UUID, + ExternalID: t.ExternalID, Sender: t.Sender, Recipient: t.Recipient, Currency: t.Currency, diff --git a/flows/events/base_test.go b/flows/events/base_test.go index 3bafc2946..b7a5ccf95 100644 --- a/flows/events/base_test.go +++ b/flows/events/base_test.go @@ -57,7 +57,8 @@ func TestEventMarshaling(t *testing.T) { { events.NewAirtimeTransferred( &flows.AirtimeTransfer{ - UUID: flows.AirtimeTransferUUID("4c2d9b7a-e02c-4e6a-ab18-06df4cb5666d"), + UUID: "4c2d9b7a-e02c-4e6a-ab18-06df4cb5666d", + ExternalID: "98765432", Sender: urns.URN("tel:+593979099111"), Recipient: urns.URN("tel:+593979099222"), Currency: "USD", @@ -85,6 +86,7 @@ func TestEventMarshaling(t *testing.T) { "created_on": "2018-10-18T14:20:30.000123456Z", "currency": "USD", "desired_amount": 1.2, + "external_id": "98765432", "http_logs": [ { "url": "https://send.money.com/topup", diff --git a/flows/services.go b/flows/services.go index 07159f8e4..f8fa6844b 100644 --- a/flows/services.go +++ b/flows/services.go @@ -158,6 +158,7 @@ const ( // AirtimeTransfer is the result of an attempted airtime transfer type AirtimeTransfer struct { UUID AirtimeTransferUUID + ExternalID string Sender urns.URN Recipient urns.URN Currency string diff --git a/services/airtime/dtone/service.go b/services/airtime/dtone/service.go index c0d9450e8..44a279768 100644 --- a/services/airtime/dtone/service.go +++ b/services/airtime/dtone/service.go @@ -99,7 +99,7 @@ func (s *service) Transfer(sender urns.URN, recipient urns.URN, amounts map[stri transfer.Currency = product.Destination.Unit transfer.DesiredAmount = amounts[transfer.Currency] - // request synchronous confirmed transaction for this product + // request asynchronous confirmed transaction for this product tx, trace, err := s.client.TransactionAsync(string(transfer.UUID), product.ID, recipientPhoneNumber) if trace != nil { logHTTP(flows.NewHTTPLog(trace, flows.HTTPStatusFromCode, s.redactor)) @@ -112,6 +112,7 @@ func (s *service) Transfer(sender urns.URN, recipient urns.URN, amounts map[stri return transfer, fmt.Errorf("transaction to send product %d on operator %d ended with status %s", product.ID, operator.ID, tx.Status.Message) } + transfer.ExternalID = fmt.Sprintf("%d", tx.ID) transfer.ActualAmount = product.Destination.Amount return transfer, nil diff --git a/services/airtime/dtone/service_test.go b/services/airtime/dtone/service_test.go index b95233fbf..26124e1da 100644 --- a/services/airtime/dtone/service_test.go +++ b/services/airtime/dtone/service_test.go @@ -58,7 +58,8 @@ func TestServiceWithSuccessfulTranfer(t *testing.T) { ) assert.NoError(t, err) assert.Equal(t, &flows.AirtimeTransfer{ - UUID: flows.AirtimeTransferUUID("1ae96956-4b34-433e-8d1a-f05fe6923d6d"), + UUID: "1ae96956-4b34-433e-8d1a-f05fe6923d6d", + ExternalID: "2237512891", Sender: urns.URN("tel:+593979000000"), Recipient: urns.URN("tel:+593979123456"), Currency: "USD", diff --git a/test/testdata/runner/airtime.test_successful_transfer.json b/test/testdata/runner/airtime.test_successful_transfer.json index 364d9ba3b..eb944249f 100644 --- a/test/testdata/runner/airtime.test_successful_transfer.json +++ b/test/testdata/runner/airtime.test_successful_transfer.json @@ -1,5 +1,49 @@ { "http_mocks": { + "https://dvs-api.dtone.com/v1/async/transactions": [ + { + "body": { + "confirmation_date": "2021-03-24T20:05:06.111631000Z", + "confirmation_expiration_date": "2021-03-24T21:05:05.883561000Z", + "creation_date": "2021-03-24T20:05:05.883561000Z", + "credit_party_identifier": { + "mobile_number": "+593979123456" + }, + "external_id": "EX12345", + "id": 2237512891, + "product": { + "description": "", + "id": 6035, + "name": "3 USD", + "operator": { + "country": { + "iso_code": "ECU", + "name": "Ecuador", + "regions": null + }, + "id": 1596, + "name": "Claro Ecuador", + "regions": null + }, + "regions": null, + "service": { + "id": 1, + "name": "Mobile" + }, + "type": "FIXED_VALUE_RECHARGE" + }, + "status": { + "class": { + "id": 2, + "message": "CONFIRMED" + }, + "id": 20000, + "message": "CONFIRMED" + } + }, + "status": 200 + } + ], "https://dvs-api.dtone.com/v1/lookup/mobile-number": [ { "body": [ @@ -78,50 +122,6 @@ ], "status": 200 } - ], - "https://dvs-api.dtone.com/v1/async/transactions": [ - { - "body": { - "confirmation_date": "2021-03-24T20:05:06.111631000Z", - "confirmation_expiration_date": "2021-03-24T21:05:05.883561000Z", - "creation_date": "2021-03-24T20:05:05.883561000Z", - "credit_party_identifier": { - "mobile_number": "+593979123456" - }, - "external_id": "EX12345", - "id": 2237512891, - "product": { - "description": "", - "id": 6035, - "name": "3 USD", - "operator": { - "country": { - "iso_code": "ECU", - "name": "Ecuador", - "regions": null - }, - "id": 1596, - "name": "Claro Ecuador", - "regions": null - }, - "regions": null, - "service": { - "id": 1, - "name": "Mobile" - }, - "type": "FIXED_VALUE_RECHARGE" - }, - "status": { - "class": { - "id": 2, - "message": "CONFIRMED" - }, - "id": 20000, - "message": "CONFIRMED" - } - }, - "status": 200 - } ] }, "outputs": [ @@ -132,6 +132,7 @@ "created_on": "2018-07-06T12:30:08.123456789Z", "currency": "USD", "desired_amount": 3.5, + "external_id": "2237512891", "http_logs": [ { "created_on": "2018-07-06T12:30:02.123456789Z", @@ -167,8 +168,8 @@ "recipient": "tel:+12065551212", "sender": "", "step_uuid": "8720f157-ca1c-432f-9c0b-2014ddc77094", - "type": "airtime_transferred", - "transfer_uuid": "c34b6c7d-fa06-4563-92a3-d648ab64bccb" + "transfer_uuid": "c34b6c7d-fa06-4563-92a3-d648ab64bccb", + "type": "airtime_transferred" }, { "category": "Success", @@ -176,7 +177,7 @@ "name": "Transfer", "step_uuid": "8720f157-ca1c-432f-9c0b-2014ddc77094", "type": "run_result_changed", - "value": "3" + "value": "2237512891" } ], "segments": [], @@ -217,11 +218,12 @@ "created_on": "2018-07-06T12:30:08.123456789Z", "currency": "USD", "desired_amount": 3.5, + "external_id": "2237512891", "http_logs": [ { "created_on": "2018-07-06T12:30:02.123456789Z", "elapsed_ms": 1000, - "request": "POST /v1/lookup/mobile-number HTTP/1.1\r\nHost: dvs-api.dtone.com\r\nUser-Agent: Go-http-client/1.1\r\nContent-Length: 32\r\nAuthorization: Basic bnlhcnVrYToxMjM0NTY3ODk=\r\nContent-Type: application/json\r\nAccept-Encoding: gzip\r\n\r\n{\"mobile_number\":\"+12065551212\"}", + "request": "POST /v1/lookup/mobile-number HTTP/1.1\r\nHost: dvs-api.dtone.com\r\nUser-Agent: Go-http-client/1.1\r\nContent-Length: 32\r\nAuthorization: Basic bnlhcnVrYToxMjM0NTY3ODk=\r\nContent-Type: application/json\r\nAccept-Encoding: gzip\r\n\r\n{\"mobile_number\":\"+12065551212\"}", "response": "HTTP/1.0 200 OK\r\nContent-Length: 191\r\n\r\n[\n {\n \"id\": 1596,\n \"identified\": true,\n \"name\": \"Claro Ecuador\"\n }\n ]", "retries": 0, "status": "success", @@ -252,8 +254,8 @@ "recipient": "tel:+12065551212", "sender": "", "step_uuid": "8720f157-ca1c-432f-9c0b-2014ddc77094", - "type": "airtime_transferred", - "transfer_uuid": "c34b6c7d-fa06-4563-92a3-d648ab64bccb" + "transfer_uuid": "c34b6c7d-fa06-4563-92a3-d648ab64bccb", + "type": "airtime_transferred" }, { "category": "Success", @@ -261,7 +263,7 @@ "name": "Transfer", "step_uuid": "8720f157-ca1c-432f-9c0b-2014ddc77094", "type": "run_result_changed", - "value": "3" + "value": "2237512891" } ], "exited_on": "2018-07-06T12:30:14.123456789Z", @@ -284,7 +286,7 @@ "created_on": "2018-07-06T12:30:10.123456789Z", "name": "Transfer", "node_uuid": "75656148-9e8b-4611-82c0-7ff4b55fb44a", - "value": "3" + "value": "2237512891" } }, "status": "completed",