-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobilepay.go
72 lines (62 loc) · 2.14 KB
/
mobilepay.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 mobilepay provides functionality for decrypting mobile
// payment requests like those from Apple or Android Pay.
package mobilepay
import (
"bytes"
"crypto/ecdsa"
"encoding/base64"
"encoding/json"
"fmt"
)
// DecryptedToken is what will be returned when a mobile
// payment token is successfully decrypted.
type DecryptedToken struct {
Dpan string
ExpireMonth string
ExpireYear string
Method string
Cryptogram string
Eci string
}
// NewAndroidPayDecryptedToken checks that the android pay token data is valid
// then decrypts it returning a pointer to the DecryptedToken. An error is
// returned if the android pay token is invalid or there was a problem
// decrypting it. For more documentation on Android Pay encrypted token data
// and decryption check out https://developers.google.com/android-pay/integration/payment-token-cryptography
func NewAndroidPayDecryptedToken(msg, epk, tag string, mpk *ecdsa.PrivateKey) (*DecryptedToken, error) {
rawmsg, err := base64.StdEncoding.DecodeString(msg)
if err != nil {
return nil, fmt.Errorf("Could not b64 decode encrypted message %v", err)
}
rawepk, err := base64.StdEncoding.DecodeString(epk)
if err != nil {
return nil, fmt.Errorf("Could not b64 decode ephemeral public key %v", err)
}
rawtag, err := base64.StdEncoding.DecodeString(tag)
if err != nil {
return nil, fmt.Errorf("Could not b64 decode tag %v", err)
}
tokjson, err := androidPayVerifyAndDecrypt(rawmsg, rawepk, rawtag, mpk)
if err != nil {
return nil, err
}
var token interface{}
decoder := json.NewDecoder(bytes.NewReader(tokjson))
decoder.UseNumber()
if err := decoder.Decode(&token); err != nil {
return nil, err
}
mapping := token.(map[string]interface{})
return &DecryptedToken{
mapping["dpan"].(string),
mapping["expirationMonth"].(json.Number).String(),
mapping["expirationYear"].(json.Number).String(),
mapping["authMethod"].(string),
mapping["3dsCryptogram"].(string),
mapping["3dsEciIndicator"].(string),
}, nil
}
// NewApplePayDecryptedToken is not implemented yet.
func NewApplePayDecryptedToken() (*DecryptedToken, error) {
return nil, fmt.Errorf("Not implemented yet!")
}