forked from gotsunami/mangopay2-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefund.go
117 lines (102 loc) · 3.17 KB
/
refund.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
// Copyright 2014 Mathias Monnerville. All rights reserved.
// Use of this source code is governed by a GPL
// license that can be found in the LICENSE file.
package mango
import (
"encoding/json"
)
const (
RefundReasonInitializedByClient = "INITIALIZED_BY_CLIENT"
RefundReasonBankaccountIncorrect = "BANKACCOUNT_INCORRECT"
RefundReasonOwnerDoNotMatchBankaccount = "OWNER_DOT_NOT_MATCH_BANKACCOUNT"
RefundReasonBankaccountHasBeenClosed = "BANKACCOUNT_HAS_BEEN_CLOSED"
RefundReasonWithdrawalImpossibleOnSavingsAccounts = "WITHDRAWAL_IMPOSSIBLE_ON_SAVINGS_ACCOUNTS"
RefundReasonOther = "OTHER"
)
// List of refunds.
type RefundList []*Refund
type refundKind int
const (
// Pay a wallet back
transferRefund refundKind = iota
// Pay a user (bank account) back
payInRefund
)
// A refund is a request to pay a wallet back.
//
// http://docs.mangopay.com/api-references/refund/%E2%80%A2-refund-a-transfer/
type Refund struct {
ProcessReply
AuthorId string
DebitedFunds Money
Fees Money
CreditedFunds Money
Type string // PAY_IN, PAY_OUT or TRANSFER
Nature string
CreditedUserId string
InitialTransactionId string
InitialTransactionType string
DebitedWalletId string
CreditedWalletId string
RefundReason RefundReason
transfer *Transfer
payIn *PayIn
kind refundKind
}
type RefundReason struct {
RefundReasonType string
RefundReasonMessage string
}
func (r *Refund) String() string {
return struct2string(r)
}
// Save creates a refund.
func (r *Refund) save() error {
data := JsonObject{}
j, err := json.Marshal(r)
if err != nil {
return err
}
if err := json.Unmarshal(j, &data); err != nil {
return err
}
// Force float64 to int conversion after unmarshalling.
for _, field := range []string{"CreationDate", "ExecutionDate"} {
data[field] = int(data[field].(float64))
}
// Fields not allowed when creating a tranfer.
for _, field := range []string{"Id", "CreationDate", "ExecutionDate", "CreditedFunds", "CreditedUserId", "ResultCode", "ResultMessage", "Status", "Fees", "InitialTransactionType", "InitialTransactionId", "DebitedFunds", "Nature", "DebitedWalletId", "CreditedWalletId", "Type"} {
delete(data, field)
}
var action mangoAction
var service *MangoPay
r.String()
switch r.kind {
case transferRefund:
action = actionCreateTransferRefund
data["AuthorId"] = r.transfer.AuthorId
data["TransferId"] = r.transfer.Id
service = r.transfer.service
case payInRefund:
action = actionCreatePayInRefund
data["AuthorId"] = r.payIn.AuthorId
data["PayInId"] = r.payIn.Id
service = r.payIn.service
}
ins, err := service.anyRequest(new(Refund), action, data)
if err != nil {
return err
}
t, p, k := r.transfer, r.payIn, r.kind
*r = *(ins.(*Refund))
r.transfer, r.payIn, r.kind = t, p, k
return nil
}
// Refund fetches a refund (tranfer or payin).
func (m *MangoPay) Refund(id string) (*Refund, error) {
any, err := m.anyRequest(new(Refund), actionFetchRefund, JsonObject{"Id": id})
if err != nil {
return nil, err
}
return any.(*Refund), nil
}