-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfondy_v2.go
156 lines (130 loc) · 5.14 KB
/
fondy_v2.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
/*
* MIT License
*
* Copyright (c) 2024 Anton (stremovskyy) Stremovskyy <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package gofondy
import (
"errors"
"fmt"
"github.com/stremovskyy/gofondy/consts"
"github.com/stremovskyy/gofondy/manager"
"github.com/stremovskyy/gofondy/models"
"github.com/stremovskyy/gofondy/models/models_v2"
"github.com/stremovskyy/gofondy/utils"
)
var v2 V2
type fondyV2 struct {
manager manager.FondyManager
options *models.Options
}
func (g *gateway) V2() V2 {
if v2 == nil {
v2 = &fondyV2{
manager: g.manager,
options: g.options,
}
}
return v2
}
func (g *fondyV2) SplitRefund(invoiceRequest *models.InvoiceRequest) (*models_v2.Order, error) {
request := &models_v2.Order{
MerchantID: invoiceRequest.Merchant.MerchantIDInt(),
Amount: invoiceRequest.GetAmountString(),
OrderID: invoiceRequest.GetInvoiceIDString(),
Currency: utils.StringRef(string(consts.CurrencyCodeUAH)),
ServerCallbackURL: invoiceRequest.ServerCallbackURL,
}
raw, err := g.manager.SplitRefund(request, invoiceRequest.Merchant)
if err != nil {
return nil, models.NewAPIError(800, "Http request failed", err, request, raw)
}
fondyResponse, err := models_v2.UnmarshalResponse(*raw)
if err != nil {
return nil, models.NewAPIError(801, "Unmarshal response fail", err, request, raw)
}
err = fondyResponse.Error()
if err != nil {
return nil, models.NewAPIError(802, "Fondy Gate Response Failure", err, request, raw)
}
order, err := fondyResponse.Order()
if err != nil {
return nil, err
}
if order.ReverseStatus != consts.FondyReverseStatusSuccess && order.ReverseStatus != consts.FondyReverseStatusApproved {
err = fmt.Errorf("reverse status is %s, (%s)", order.ReverseStatus, *order.ResponseDescription)
return nil, models.NewAPIError(803, "Fondy Gate Response Failure", err, request, raw)
}
return order, nil
}
func (g *fondyV2) Split(invoiceRequest *models.InvoiceRequest) (*models_v2.Order, error) {
err := invoiceRequest.Merchant.SplitAccounts.Error()
if err != nil {
return nil, errors.New("split accounts problem " + err.Error())
}
if !invoiceRequest.Merchant.IsTechnical {
return nil, errors.New("split accounts problem: only technical accounts can split")
}
if len(invoiceRequest.Merchant.SplitAccounts) == 0 {
return nil, errors.New("split accounts problem: no split accounts")
}
request := &models.FondyRequestObject{
MerchantID: invoiceRequest.GetMerchantIDString(),
OrderID: invoiceRequest.GetInvoiceIDString(),
AdditionalData: invoiceRequest.AdditionalData,
ServerCallbackURL: invoiceRequest.ServerCallbackURL,
}
rawStatus, err := g.manager.Status(request, invoiceRequest.Merchant)
if err != nil {
return nil, models.NewAPIError(800, "Http request failed", err, request, rawStatus)
}
fondyStatusResponse, err := models.UnmarshalStatusResponse(*rawStatus)
if err != nil {
return nil, models.NewAPIError(801, "Unmarshal response fail", err, request, rawStatus)
}
if !fondyStatusResponse.Response.Captured() {
return nil, errors.New("split accounts problem: order is not captured")
}
order := &models_v2.Order{
MerchantID: invoiceRequest.Merchant.MerchantIDInt(),
Amount: invoiceRequest.GetAmountString(),
OrderID: invoiceRequest.GetInvoiceIDString(),
Currency: utils.StringRef(string(consts.CurrencyCodeUAH)),
OrderType: utils.StringRef("settlement"),
Rectoken: invoiceRequest.PaymentCardToken,
OperationID: invoiceRequest.GetInvoiceIDString(),
OrderDesc: invoiceRequest.GetDescriptionString(),
ServerCallbackURL: invoiceRequest.ServerCallbackURL,
}
raw, err := g.manager.SplitPayment(order, invoiceRequest.Merchant)
if err != nil {
return nil, models.NewAPIError(800, "Http splitRequest failed", err, nil, raw)
}
fondyResponse, err := models_v2.UnmarshalResponse(*raw)
if err != nil {
return nil, models.NewAPIError(801, "Unmarshal response fail", err, nil, raw)
}
err = fondyResponse.Error()
if err != nil {
return nil, models.NewAPIError(802, "Fondy Gate Response Failure", err, nil, raw)
}
return fondyResponse.Order()
}