smobilpay.*CollectParams.toPayload   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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