|
| 1 | +package dtone |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/nyaruka/gocommon/httpx" |
| 12 | + "github.com/nyaruka/gocommon/jsonx" |
| 13 | + |
| 14 | + "github.com/shopspring/decimal" |
| 15 | +) |
| 16 | + |
| 17 | +type StatusCID int |
| 18 | + |
| 19 | +const ( |
| 20 | + apiURL = "https://dvs-api.dtone.com/v1/" |
| 21 | + |
| 22 | + // see https://dvs-api-doc.dtone.com/#section/Overview/Transactions |
| 23 | + StatusCIDCreated StatusCID = 1 |
| 24 | + StatusCIDConfirmed StatusCID = 2 |
| 25 | + StatusCIDRejected StatusCID = 3 |
| 26 | + StatusCIDCancelled StatusCID = 4 |
| 27 | + StatusCIDSubmitted StatusCID = 5 |
| 28 | + StatusCIDCompleted StatusCID = 7 |
| 29 | + StatusCIDReversed StatusCID = 8 |
| 30 | + StatusCIDDeclined StatusCID = 9 |
| 31 | +) |
| 32 | + |
| 33 | +// Client is a DTOne client, see https://dvs-api-doc.dtone.com/ for API docs |
| 34 | +type Client struct { |
| 35 | + httpClient *http.Client |
| 36 | + httpRetries *httpx.RetryConfig |
| 37 | + key string |
| 38 | + secret string |
| 39 | +} |
| 40 | + |
| 41 | +// NewClient creates a new DT One client |
| 42 | +func NewClient(httpClient *http.Client, httpRetries *httpx.RetryConfig, key, secret string) *Client { |
| 43 | + return &Client{httpClient: httpClient, httpRetries: httpRetries, key: key, secret: secret} |
| 44 | +} |
| 45 | + |
| 46 | +// error response contains errors when a request fails |
| 47 | +type errorResponse struct { |
| 48 | + Errors []struct { |
| 49 | + Code int `json:"code"` |
| 50 | + Message string `json:"message"` |
| 51 | + } `json:"errors"` |
| 52 | +} |
| 53 | + |
| 54 | +func (e *errorResponse) Error() string { |
| 55 | + msgs := make([]string, len(e.Errors)) |
| 56 | + for i := range e.Errors { |
| 57 | + msgs[i] = e.Errors[i].Message |
| 58 | + } |
| 59 | + return strings.Join(msgs, ", ") |
| 60 | +} |
| 61 | + |
| 62 | +// Operator is a mobile operator |
| 63 | +type Operator struct { |
| 64 | + ID int `json:"id"` |
| 65 | + Name string `json:"name"` |
| 66 | + Country struct { |
| 67 | + Name string `json:"name"` |
| 68 | + ISOCode string `json:"iso_code"` |
| 69 | + Regions []struct { |
| 70 | + Name string `json:"name"` |
| 71 | + Code string `json:"code"` |
| 72 | + } `json:"regions"` |
| 73 | + } `json:"country"` |
| 74 | + Identified bool `json:"identified"` |
| 75 | +} |
| 76 | + |
| 77 | +// LookupMobileNumber see https://dvs-api-doc.dtone.com/#tag/Mobile-Number |
| 78 | +func (c *Client) LookupMobileNumber(ctx context.Context, phoneNumber string) ([]*Operator, *httpx.Trace, error) { |
| 79 | + var response []*Operator |
| 80 | + |
| 81 | + payload := &struct { |
| 82 | + MobileNumber string `json:"mobile_number"` |
| 83 | + }{ |
| 84 | + MobileNumber: phoneNumber, |
| 85 | + } |
| 86 | + |
| 87 | + trace, err := c.request(ctx, "POST", "lookup/mobile-number", payload, &response) |
| 88 | + if err != nil { |
| 89 | + return nil, trace, err |
| 90 | + } |
| 91 | + |
| 92 | + return response, trace, nil |
| 93 | +} |
| 94 | + |
| 95 | +// Product is an available digital services product |
| 96 | +type Product struct { |
| 97 | + ID int `json:"id"` |
| 98 | + Name string `json:"name"` |
| 99 | + Description string `json:"description"` |
| 100 | + Service struct { |
| 101 | + ID int `json:"id"` |
| 102 | + Name string `json:"name"` |
| 103 | + } `json:"service"` |
| 104 | + Operator struct { |
| 105 | + ID int `json:"id"` |
| 106 | + Name string `json:"name"` |
| 107 | + } `json:"operator"` |
| 108 | + Type string `json:"type"` |
| 109 | + Source struct { |
| 110 | + Amount decimal.Decimal `json:"amount"` |
| 111 | + Unit string `json:"unit"` |
| 112 | + UnitType string `json:"unit_type"` |
| 113 | + } `json:"source"` |
| 114 | + Destination struct { |
| 115 | + Amount decimal.Decimal `json:"amount"` |
| 116 | + Unit string `json:"unit"` |
| 117 | + UnitType string `json:"unit_type"` |
| 118 | + } `json:"destination"` |
| 119 | +} |
| 120 | + |
| 121 | +// Products see https://dvs-api-doc.dtone.com/#tag/Products |
| 122 | +func (c *Client) Products(ctx context.Context, _type string, operatorID int) ([]*Product, *httpx.Trace, error) { |
| 123 | + var response []*Product |
| 124 | + |
| 125 | + // TODO endpoint could return more than 100 products in which case we need to page |
| 126 | + |
| 127 | + trace, err := c.request(ctx, "GET", fmt.Sprintf("products?type=%s&operator_id=%d&per_page=100", _type, operatorID), nil, &response) |
| 128 | + if err != nil { |
| 129 | + return nil, trace, err |
| 130 | + } |
| 131 | + |
| 132 | + return response, trace, nil |
| 133 | +} |
| 134 | + |
| 135 | +// Transaction is a product sent to a beneficiary |
| 136 | +type Transaction struct { |
| 137 | + ID int64 `json:"id"` |
| 138 | + ExternalID string `json:"external_id"` |
| 139 | + CreationDate string `json:"creation_date"` |
| 140 | + ConfirmationExpirationDate string `json:"confirmation_expiration_date"` |
| 141 | + ConfirmationDate string `json:"confirmation_date"` |
| 142 | + Status struct { |
| 143 | + ID int `json:"id"` |
| 144 | + Message string `json:"message"` |
| 145 | + Class struct { |
| 146 | + ID StatusCID `json:"id"` |
| 147 | + Message string `json:"message"` |
| 148 | + } |
| 149 | + } `json:"status"` |
| 150 | +} |
| 151 | + |
| 152 | +// TransactionAsync see https://dvs-api-doc.dtone.com/#tag/Transactions |
| 153 | +func (c *Client) TransactionAsync(ctx context.Context, externalID string, productID int, mobileNumber string) (*Transaction, *httpx.Trace, error) { |
| 154 | + var response *Transaction |
| 155 | + |
| 156 | + type creditPartyIdentifier struct { |
| 157 | + MobileNumber string `json:"mobile_number"` |
| 158 | + } |
| 159 | + |
| 160 | + payload := &struct { |
| 161 | + ExternalID string `json:"external_id"` |
| 162 | + ProductID int `json:"product_id"` |
| 163 | + AutoConfirm bool `json:"auto_confirm"` |
| 164 | + CreditPartyIdentifier creditPartyIdentifier `json:"credit_party_identifier"` |
| 165 | + }{ |
| 166 | + ExternalID: externalID, |
| 167 | + ProductID: productID, |
| 168 | + AutoConfirm: true, |
| 169 | + CreditPartyIdentifier: creditPartyIdentifier{ |
| 170 | + MobileNumber: mobileNumber, |
| 171 | + }, |
| 172 | + } |
| 173 | + |
| 174 | + trace, err := c.request(ctx, "POST", "async/transactions", payload, &response) |
| 175 | + if err != nil { |
| 176 | + return nil, trace, err |
| 177 | + } |
| 178 | + |
| 179 | + return response, trace, nil |
| 180 | +} |
| 181 | + |
| 182 | +func (c *Client) request(ctx context.Context, method, endpoint string, payload any, response any) (*httpx.Trace, error) { |
| 183 | + url := apiURL + endpoint |
| 184 | + headers := map[string]string{} |
| 185 | + var body io.Reader |
| 186 | + |
| 187 | + if payload != nil { |
| 188 | + data, err := jsonx.Marshal(payload) |
| 189 | + if err != nil { |
| 190 | + return nil, err |
| 191 | + } |
| 192 | + body = bytes.NewReader(data) |
| 193 | + headers["Content-Type"] = "application/json" |
| 194 | + } |
| 195 | + |
| 196 | + req, err := httpx.NewRequest(ctx, method, url, body, headers) |
| 197 | + if err != nil { |
| 198 | + return nil, err |
| 199 | + } |
| 200 | + |
| 201 | + req.SetBasicAuth(c.key, c.secret) |
| 202 | + |
| 203 | + trace, err := httpx.DoTrace(c.httpClient, req, c.httpRetries, nil, -1) |
| 204 | + if err != nil { |
| 205 | + return trace, err |
| 206 | + } |
| 207 | + |
| 208 | + if trace.Response.StatusCode >= 400 { |
| 209 | + response := &errorResponse{} |
| 210 | + jsonx.Unmarshal(trace.ResponseBody, response) |
| 211 | + return trace, response |
| 212 | + } |
| 213 | + |
| 214 | + if response != nil { |
| 215 | + return trace, jsonx.Unmarshal(trace.ResponseBody, response) |
| 216 | + } |
| 217 | + return trace, nil |
| 218 | +} |
0 commit comments