/
flow.go
72 lines (63 loc) · 2 KB
/
flow.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
package fazpass
import (
"bytes"
"crypto/rsa"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/anvarisy/go-fazpass-sdk/utils"
"io"
"net/http"
)
func Default() FlowInterface {
return &Flow{}
}
type Flow struct {
client *http.Client
}
type FlowInterface interface {
WrappingData(pubKey *rsa.PublicKey, model interface{}) ([]byte, error)
SendingData(baseUrl string, wrappedMessage []byte, merchantKey string) (*http.Response, error)
ExtractingData(privKey *rsa.PrivateKey, response *http.Response, data *Data) (*Data, error)
}
func (flow *Flow) WrappingData(pubKey *rsa.PublicKey, model interface{}) ([]byte, error) {
var (
err error
marshalled []byte
encrypted []byte
wrapedData []byte
)
marshalled, err = json.Marshal(model)
encrypted, err = utils.EncryptWithPublicKey(marshalled, pubKey)
message := base64.StdEncoding.EncodeToString([]byte(encrypted))
wrapedData, err = json.Marshal(&Transmission{Message: message})
return wrapedData, err
}
func (flow *Flow) SendingData(baseUrl string, wrappedMessage []byte, merchantKey string) (*http.Response, error) {
client := &http.Client{}
request, err := http.NewRequest("POST", baseUrl, bytes.NewReader(wrappedMessage))
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", "Bearer "+merchantKey)
response, err := client.Do(request)
if err != nil {
return nil, err
}
return response, nil
}
func (flow *Flow) ExtractingData(privKey *rsa.PrivateKey, response *http.Response, data *Data) (*Data, error) {
defer response.Body.Close()
messageBodyResponse, _ := io.ReadAll(response.Body)
transmissionResponse := &Transmission{}
json.Unmarshal(messageBodyResponse, transmissionResponse)
decryptMessage, err := base64.StdEncoding.DecodeString(string(transmissionResponse.Message))
decrypted, _ := utils.DecryptWithPrivateKey(decryptMessage, privKey)
json.Unmarshal(decrypted, data)
fmt.Print(data.SessionId)
if err != nil {
return data, err
}
return data, nil
}