Passed
Push — main ( 4648c5...0d6b3e )
by Acho
01:43
created

constructs.go   A

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 59
dl 0
loc 90
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A smobilpay.*CollectParams.toPayload 0 24 5
A smobilpay.*Transaction.IsFailed 0 2 2
A smobilpay.*Transaction.IsPending 0 2 2
1
package smobilpay
2
3
import (
4
	"time"
5
)
6
7
// QuoteParams is the input needed to initialize a transaction
8
type QuoteParams struct {
9
	PayItemID string `json:"payItemId"`
10
	Amount    string `json:"amount"`
11
}
12
13
// Quote represents an initialized transaction
14
type Quote struct {
15
	QuoteID             string      `json:"quoteId"`
16
	ExpiresAt           time.Time   `json:"expiresAt"`
17
	PayItemID           string      `json:"payItemId"`
18
	AmountLocalCurrency string      `json:"amountLocalCur"`
19
	PriceLocalCurrency  int         `json:"priceLocalCur"`
20
	PriceSystemCurrency int         `json:"priceSystemCur"`
21
	LocalCurrency       string      `json:"localCur"`
22
	SystemCurrency      string      `json:"systemCur"`
23
	Promotion           interface{} `json:"promotion"`
24
}
25
26
// CollectParams is the input needed to confirm a transaction
27
type CollectParams struct {
28
	QuoteID               string `json:"quoteId"`
29
	CustomerPhoneNumber   string `json:"customerPhonenumber"`
30
	CustomerEmailAddress  string `json:"customerEmailaddress"`
31
	CustomerName          string `json:"customerName"`
32
	CustomerAddress       string `json:"customerAddress"`
33
	CustomerNumber        string `json:"customerNumber"`
34
	ServiceNumber         string `json:"serviceNumber"`
35
	ExternalTransactionID string `json:"trid"`
36
}
37
38
func (params *CollectParams) toPayload() map[string]string {
39
	payload := map[string]string{
40
		"quoteId":              params.QuoteID,
41
		"customerPhonenumber":  params.CustomerPhoneNumber,
42
		"customerEmailaddress": params.CustomerEmailAddress,
43
	}
44
45
	if params.CustomerName != "" {
46
		payload["customerName"] = params.CustomerName
47
	}
48
49
	if params.CustomerAddress != "" {
50
		payload["customerAddress"] = params.CustomerAddress
51
	}
52
53
	if params.ServiceNumber != "" {
54
		payload["serviceNumber"] = params.ServiceNumber
55
	}
56
57
	if params.ExternalTransactionID != "" {
58
		payload["trid"] = params.ExternalTransactionID
59
	}
60
61
	return payload
62
}
63
64
// Transaction represents a transaction
65
type Transaction struct {
66
	PaymentTransactionNumber string      `json:"ptn"`
67
	Timestamp                time.Time   `json:"timestamp"`
68
	AgentBalance             string      `json:"agentBalance"`
69
	ReceiptNumber            string      `json:"receiptNumber"`
70
	VerificationCode         string      `json:"veriCode"`
71
	ClearingDate             string      `json:"clearingDate"`
72
	PriceLocalCurrency       string      `json:"priceLocalCur"`
73
	PriceSystemCurrency      string      `json:"priceSystemCur"`
74
	LocalCurrency            string      `json:"localCur"`
75
	SystemCurrency           string      `json:"systemCur"`
76
	ExternalTransactionID    *string     `json:"trid"`
77
	Pin                      interface{} `json:"pin"`
78
	Status                   string      `json:"status"`
79
	PayItemDescription       *string     `json:"payItemDescr"`
80
	PayItemID                string      `json:"payItemId"`
81
}
82
83
// IsFailed checks if a transaction failed
84
func (transaction *Transaction) IsFailed() bool {
85
	return transaction.Status == "ERRORED" || transaction.Status == "ERROREDREFUNDED"
86
}
87
88
// IsPending checks if a transaction is pending
89
func (transaction *Transaction) IsPending() bool {
90
	return transaction.Status == "PENDING" || transaction.Status == "INPROCESS"
91
}
92