/
payment.go
155 lines (127 loc) · 4.85 KB
/
payment.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
package acmeserverless
import (
"encoding/json"
"strconv"
"github.com/retgits/creditcard"
)
// CreditCardValidatedEvent is sent by the payment service when the creditcard has been validated.
type CreditCardValidatedEvent struct {
// Metadata for the event.
Metadata Metadata `json:"metadata"`
// Data contains the payload data for the event.
Data CreditCardValidationDetails `json:"data"`
}
// UnmarshalCreditCardValidatedEvent parses the JSON-encoded data and stores the result in a
// CreditCardValidatedEvent.
func UnmarshalCreditCardValidatedEvent(data []byte) (CreditCardValidatedEvent, error) {
var r CreditCardValidatedEvent
err := json.Unmarshal(data, &r)
return r, err
}
// Marshal returns the JSON encoding of CreditCardValidatedEvent.
func (e *CreditCardValidatedEvent) Marshal() ([]byte, error) {
return json.Marshal(e)
}
// CreditCardValidationDetails contain the details of the validation by the payment service.
type CreditCardValidationDetails struct {
// Indicates whether the transaction was a success or not.
Success bool `json:"success"`
// The HTTP statuscode of the event.
Status int `json:"status"`
// A string containing the result of the service.
Message string `json:"message"`
// The monetary amount of the transaction.
Amount string `json:"amount,omitempty"`
// The unique identifier of the transaction.
TransactionID string `json:"transactionID"`
// The unique identifier of the order.
OrderID string `json:"orderID"`
}
// UnmarshalCreditCardValidationDetails parses the JSON-encoded data and stores the result in a
// CreditCardValidationDetails.
func UnmarshalCreditCardValidationDetails(data []byte) (CreditCardValidationDetails, error) {
var r CreditCardValidationDetails
err := json.Unmarshal(data, &r)
return r, err
}
// Marshal returns the JSON encoding of CreditCardValidationDetails.
func (e *CreditCardValidationDetails) Marshal() ([]byte, error) {
return json.Marshal(e)
}
// PaymentRequestDetails contains the data that is needed to validate the payment.
type PaymentRequestDetails struct {
// The unique identifier of the order.
OrderID string `json:"orderID"`
// Card used for the transaction.
Card creditcard.Card `json:"card"`
// Total monetary value of the transaction.
Total string `json:"total"`
}
// UnmarshalPaymentRequestDetails parses the JSON-encoded data and stores the result in a
// PaymentRequestDetails.
func UnmarshalPaymentRequestDetails(data []byte) (PaymentRequestDetails, error) {
var r PaymentRequestDetails
err := json.Unmarshal(data, &r)
return r, err
}
// Marshal returns the JSON encoding of PaymentRequestDetails.
func (e *PaymentRequestDetails) Marshal() ([]byte, error) {
return json.Marshal(e)
}
// PaymentRequestedEvent is sent by the Order service when the creditcard for the order should be
// validated and charged.
type PaymentRequestedEvent struct {
// Metadata for the event.
Metadata Metadata `json:"metadata"`
// Data contains the payload data for the event.
Data PaymentRequestDetails `json:"data"`
}
// UnmarshalPaymentRequestedEvent parses the JSON-encoded data and stores the result in a
// PaymentRequestedEvent.
func UnmarshalPaymentRequestedEvent(data []byte) (PaymentRequestedEvent, error) {
var r PaymentRequestedEvent
err := json.Unmarshal(data, &r)
return r, err
}
// Marshal returns the JSON encoding of PaymentRequestedEvent.
func (e *PaymentRequestedEvent) Marshal() ([]byte, error) {
return json.Marshal(e)
}
// UnmarshalShopPayment parses the JSON-encoded data and stores the result in a
// ShopPayment.
func UnmarshalShopPayment(data []byte) (ShopPayment, error) {
var r ShopPayment
err := json.Unmarshal(data, &r)
return r, err
}
// Marshal returns the JSON encoding of ShopPayment.
func (r *ShopPayment) Marshal() ([]byte, error) {
return json.Marshal(r)
}
// ShopPayment allows for backward compatibility between the serverless version and the containerized version
// of the ACME Fitness Shop. This format should only be used for HTTP based interactions.
type ShopPayment struct {
Card ShopCard `json:"card"`
Total string `json:"total"`
}
// ShopCard allows for backward compatibility between the serverless version and the containerized version
// of the ACME Fitness Shop. This format should only be used for HTTP based interactions.
type ShopCard struct {
Number string `json:"number"`
ExpYear string `json:"expYear"`
ExpMonth string `json:"expMonth"`
Ccv string `json:"ccv"`
}
// ToCreditCard maps the old shopcard data to the new creditcard and allows for backward compatibility between
// the serverless version and the containerized version of the ACME Fitness Shop. This format should only be used
// for HTTP based interactions.
func (s *ShopCard) ToCreditCard() creditcard.Card {
em, _ := strconv.Atoi(s.ExpMonth)
ey, _ := strconv.Atoi(s.ExpYear)
return creditcard.Card{
CVV: s.Ccv,
Number: s.Number,
ExpiryMonth: em,
ExpiryYear: ey,
}
}