-
Notifications
You must be signed in to change notification settings - Fork 0
/
cash_in.go
266 lines (218 loc) · 7.24 KB
/
cash_in.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package orange_money_apis
import (
"encoding/json"
"fmt"
"github.com/go-playground/validator/v10"
)
type CashInParams struct {
CustomerKey string `validate:"required"`
CustomerSecret string `validate:"required"`
XAuthToken string `validate:"required"`
MerchantNumber string `validate:"required,ynoteMerchantNumber"`
Pin string `validate:"required"`
IsProd bool
Logger DebugLogger
}
type InitializeCashInParams struct {
NotificationUrl string `validate:"required,http_url"`
Amount uint32 `validate:"required"` // todo: check if 0 is valid.
ReferenceId string `validate:"required"`
Comment string `validate:"required"`
BuyerAccountPhone string `validate:"required,omNumber"`
}
type CashIn struct {
Config CashInParams
}
type _PayTokenRes struct {
Message string `json:"message"`
Data struct {
PayToken string `json:"payToken"`
} `json:"data"`
}
type _CashInRes struct {
json interface{}
Message string `json:"message"`
Data struct {
Id int `json:"id"`
Createtime string `json:"createtime"`
SubscriberMsisdn string `json:"subscriberMsisdn"`
Amount string `json:"amount"`
PayToken string `json:"payToken"`
Txnid string `json:"txnid"`
Txnmode string `json:"txnmode"`
Inittxnmessage string `json:"inittxnmessage"`
Inittxnstatus string `json:"inittxnstatus"`
Confirmtxnstatus string `json:"confirmtxnstatus"`
Confirmtxnmessage string `json:"confirmtxnmessage"`
Status string `json:"status"`
NotifUrl string `json:"notifUrl"`
Description string `json:"description"`
ChannelUserMsisdn string `json:"channelUserMsisdn"`
} `json:"data"`
}
type NewCashInRes struct {
Raw string
Status int8
PayToken string
}
const (
apiLocationProd string = "https://api-s1.orange.cm"
apiLocationDev = "https://mockapi.taurs.dev/karibu-cap/orange_money_apis"
)
func (this *CashIn) getApiLocation() string {
if this.Config.IsProd {
return apiLocationProd
}
return apiLocationDev
}
func (this *CashIn) requestNewPayToken() (payToken string, error error) {
const loggingID = "requestNewPayToken"
accessToken, tokenError := requestNewAccesToken(this.Config.CustomerKey, this.Config.CustomerSecret, this.getApiLocation())
if tokenError != nil {
return "", tokenError
}
header := map[string][]string{
"X-AUTH-TOKEN": {this.Config.XAuthToken},
"Authorization": {utils.join("Bearer ", accessToken)},
}
endPoint := utils.join(this.getApiLocation(), "/omcoreapis/1.0.2/mp/init")
response, reqError := request.post(endPoint, nil, header)
if reqError != nil {
return "", reqError
}
this.Config.Logger.Debug(
fmt.Sprintf("%s:start", loggingID),
map[string]any{
"response": response,
"message": "Initializing payment(generating pay token)",
"endPoint": endPoint,
"header": header,
},
)
if response.status != 200 && response.status != 201 {
return "", utils.newError(map[string]any{
"errorMessage": "Failed to request a new payToken",
"response": response.asText(),
"endPoint": endPoint,
"body": nil,
"header": header,
})
}
var parsedResponse _PayTokenRes
jsonError := response.asJson(&parsedResponse)
if jsonError != nil {
return "", jsonError
}
return parsedResponse.Data.PayToken, nil
}
func (this *CashIn) RequestNewCashIn(config *InitializeCashInParams) (*NewCashInRes, error) {
const loggingID = "CashIn.RequestNewCashIn"
this.Config.Logger.Debug(fmt.Sprintf("%s:start", loggingID), nil)
validate := validator.New()
validate.RegisterValidation("omNumber", isOmNumber)
validate.RegisterValidation("ynoteMerchantNumber", isYnoteMerchantNumber)
err := validate.Struct(config)
if err != nil {
return nil, err
}
payToken, payTokenResError := this.requestNewPayToken()
if payTokenResError != nil {
return nil, payTokenResError
}
accessToken, accessTokenError := requestNewAccesToken(this.Config.CustomerKey, this.Config.CustomerSecret, this.getApiLocation())
if accessTokenError != nil {
return nil, accessTokenError
}
header := map[string][]string{
"X-AUTH-TOKEN": {this.Config.XAuthToken},
"Authorization": {utils.join("Bearer ", accessToken)},
"Content-Type": {"application/json"},
}
body := map[string]string{
"subscriberMsisdn": config.BuyerAccountPhone,
"notifUrl": config.NotificationUrl,
"orderId": config.ReferenceId,
"description": config.Comment,
"amount": utils.join(config.Amount),
"channelUserMsisdn": this.Config.MerchantNumber,
"payToken": payToken,
"pin": this.Config.Pin,
}
serializedBody, serializationError := json.Marshal(body)
if serializationError != nil {
return nil, serializationError
}
endPoint := utils.join(this.getApiLocation(), "/omcoreapis/1.0.2/mp/pay")
this.Config.Logger.Debug(
fmt.Sprintf("%s:requesting payment", loggingID),
map[string]any{"header": header, "body": string(serializedBody), "url": endPoint},
)
response, requestError := request.post(endPoint, serializedBody, header)
if requestError != nil {
return nil, requestError
}
if response.status != 200 && response.status != 201 {
return nil, utils.newError(map[string]any{
"message": "Cashin request failed",
"response": response.asText(),
"enPoint": endPoint,
"reqHeader": header,
"reqBody": body,
})
}
this.Config.Logger.Debug(fmt.Sprintf("%s:payment_request_end_with_data", loggingID), response.asText())
var parsedResponse _CashInRes
resUnwrapError := response.asJson(&parsedResponse)
if resUnwrapError != nil {
return nil, resUnwrapError
}
return &NewCashInRes{
Status: getStatusFromProviderRawStatus(parsedResponse.Data.Status),
PayToken: payToken,
Raw: response.asText(),
}, nil
}
func (this *CashIn) FetchCashInStatus(payToken string) (*NewCashInRes, error) {
accessToken, accessTokenError := requestNewAccesToken(this.Config.CustomerKey, this.Config.CustomerSecret, this.getApiLocation())
if accessTokenError != nil {
return nil, accessTokenError
}
header := map[string][]string{
"X-AUTH-TOKEN": {this.Config.XAuthToken},
"Authorization": {utils.join("Bearer ", accessToken)},
}
var body []byte
endPoint := utils.join(this.getApiLocation(), "/omcoreapis/1.0.2/mp/paymentstatus/", payToken)
response, err := request.post(endPoint, body, header)
if err != nil {
return nil, err
}
if response.status != 200 && response.status != 201 {
return nil, utils.newError(map[string]any{
"message": "Failed to retreive the status of the requested cash in.",
"response": response.asText(),
"endPoint": endPoint,
"reqBody": body,
"reqHeader": header,
})
}
var parsedResponse _CashInRes
resUnwrapError := response.asJson(parsedResponse)
if resUnwrapError != nil {
return nil, resUnwrapError
}
return &NewCashInRes{
Status: getStatusFromProviderRawStatus(parsedResponse.Data.Status),
PayToken: payToken,
Raw: response.asText(),
}, nil
}
func New(config CashInParams) (*CashIn, *validator.ValidationErrors) {
validate := validator.New()
err := validate.Struct(config)
if validate.Struct(config) != nil {
validationErrors, _ := err.(validator.ValidationErrors)
return nil, &validationErrors
}
return &CashIn{Config: config}, nil
}