This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplepay.go
56 lines (46 loc) · 1.67 KB
/
applepay.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
package qiwi
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
)
// PKPaymentToken is ApplePay payment token structure
// see https://developer.apple.com/library/archive/documentation/PassKit/Reference/PaymentTokenJSON/PaymentTokenJSON.html
type PKPaymentToken struct {
Version string `json:"version"`
Data string `json:"data"`
Header *APHeader `json:"header"`
Signature string `json:"signature"`
}
// APHeader holds Header of PKPaymentToken.
type APHeader struct {
AppData string `json:"applicationData,omitempty"` // optional, HEX-string
Key string `json:"wrappedKey,omitempty"` // used only for RSA_v1
PubKey string `json:"ephemeralPublicKey,omitempty"` // used only for EC_v1
PubKeyHash string `json:"publicKeyHash"`
TransactionID string `json:"transactionId"`
}
// ApplePay executes payment via ApplePay
// pass context, amount and ApplePay token string.
func (p *Payment) ApplePay(ctx context.Context, amount int, token string) (err error) {
// Decode token from base64
data, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return fmt.Errorf("[QIWI] %w: %s", ErrBadJSON, err)
}
p.PaymentMethod.ApplePayToken = &PKPaymentToken{Header: &APHeader{}}
// Parse JSON data+-
err = json.Unmarshal(data, p.PaymentMethod.ApplePayToken)
if err != nil {
return fmt.Errorf("[QIWI] %w: %s", ErrBadJSON, err)
}
p.PaymentMethod.Type = ApplePayPayment
p.Amount = NewAmountInRubles(amount)
// Make request link
requestLink := fmt.Sprintf("/payin/v1/sites/%s/payments/%s", p.siteid, p.payid)
// Send request
err = proceedRequest(ctx, http.MethodPut, requestLink, p)
return p.checkErrors(err)
}