-
Notifications
You must be signed in to change notification settings - Fork 0
/
refund.go
75 lines (64 loc) · 2.55 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
package refund
import (
"fmt"
"time"
"github.com/kofc7186/fundraiser-manager/pkg/square/types/models"
)
type RefundStatus string
const (
REFUND_STATUS_UNKNOWN RefundStatus = ""
REFUND_STATUS_PENDING RefundStatus = "PENDING"
REFUND_STATUS_REJECTED RefundStatus = "REJECTED"
REFUND_STATUS_COMPLETED RefundStatus = "COMPLETED"
REFUND_STATUS_FAILED RefundStatus = "FAILED"
)
func parseRefundStatus(status string) (RefundStatus, error) {
switch RefundStatus(status) {
case REFUND_STATUS_PENDING:
return REFUND_STATUS_PENDING, nil
case REFUND_STATUS_REJECTED:
return REFUND_STATUS_REJECTED, nil
case REFUND_STATUS_COMPLETED:
return REFUND_STATUS_COMPLETED, nil
case REFUND_STATUS_FAILED:
return REFUND_STATUS_FAILED, nil
}
return REFUND_STATUS_UNKNOWN, fmt.Errorf("%q is not a valid RefundStatus", status)
}
type Refund struct {
Expiration time.Time `json:"expiration" firestore:"expiration"`
FeeAmount float64 `json:"feeAmount" firestore:"feeAmount"`
ID string `json:"id" firestore:"id"`
IdempotencyKeys map[string]bool `json:"idempotencyKeys" firestore:"idempotencyKeys"`
Reason string `json:"reason" firestore:"reason"`
RefundAmount float64 `json:"refundAmount" firestore:"refundAmount"`
SquarePaymentID string `json:"squarePaymentID" firestore:"squarePaymentID"`
SquareOrderID string `json:"squareOrderID" firestore:"squareOrderID"`
SquareUpdatedTime time.Time `json:"squareUpdatedTime" firestore:"squareUpdatedTime"`
Status RefundStatus `json:"status" firestore:"status"`
Unlinked bool `json:"unlinked" firestore:"unlinked"`
}
func CreateInternalRefundFromSquareRefund(squareRefund models.PaymentRefund) (*Refund, error) {
r := &Refund{
ID: squareRefund.Id,
Reason: squareRefund.Reason,
RefundAmount: float64(squareRefund.AmountMoney.Amount) / float64(100),
SquarePaymentID: squareRefund.PaymentId,
SquareOrderID: squareRefund.OrderId,
Unlinked: squareRefund.Unlinked,
}
var err error
// per https://developer.squareup.com/reference/square/payments-api/webhooks/refund.created
// this will be an RFC3339 timestamp
r.SquareUpdatedTime, err = time.Parse(time.RFC3339, squareRefund.UpdatedAt)
if err != nil {
return nil, err
}
if r.Status, err = parseRefundStatus(squareRefund.Status); err != nil {
return nil, err
}
for _, fee := range squareRefund.ProcessingFee {
r.FeeAmount += float64(fee.AmountMoney.Amount) / float64(100)
}
return r, nil
}