-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayments.go
104 lines (90 loc) · 2.85 KB
/
payments.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
package sfutils
import (
"bytes"
"context"
"encoding/json"
"github.com/prometheus/common/log"
"google.golang.org/api/androidpublisher/v3"
"google.golang.org/api/option"
"io/ioutil"
"net/http"
)
var GooglePayPackageName string
var GooglePayPathCredentials string
var err error
var service *androidpublisher.Service
// IOSResponseData Apple's response structure
type IOSResponseData struct {
Status float64 `json:"status"`
Receipt struct {
InApp []struct {
ProductID string `json:"product_id"`
OriginalTransactionID string `json:"original_transaction_id"`
} `json:"in_app"`
} `json:"receipt"`
}
// GetOrCreateGooglePlayService return or create a new service for payment verification
func GetOrCreateGooglePlayService() *androidpublisher.Service {
ctx := context.Background()
if service == nil {
service, err = androidpublisher.NewService(ctx, option.WithCredentialsFile(GooglePayPathCredentials)) // create server for send request
if err != nil {
log.Fatalf("create service: %s", err)
panic(err)
}
}
return service
}
// CheckGooglePlay check if the service is available for payment
// and then get information about the product using his product_id
func CheckGooglePlay(productId, token string) (*androidpublisher.ProductPurchase, error) {
service = GetOrCreateGooglePlayService()
product, err := service.Purchases.Products.Get(GooglePayPackageName, productId, token).Do()
return product, err
}
// CheckApplePay request the AppStore for complete product information
func CheckApplePay(password, receipt string) (*IOSResponseData, error) {
prodUrl := "https://buy.itunes.apple.com/verifyReceipt"
testUrl := "https://sandbox.itunes.apple.com/verifyReceipt"
var bytesBody *bytes.Buffer
postBody, _ := json.Marshal(map[string]interface{}{
"password": password,
"receipt-data": receipt,
"exclude-old-transactions": true,
})
bytesBody = bytes.NewBuffer(postBody)
result, err := RequestToPayPlatform(prodUrl, "ios", bytesBody)
if err != nil {
return nil, err
}
status := result.Status
if status == float64(21007) { // 21007 this is test server
bytesBody = bytes.NewBuffer(postBody)
result, err = RequestToPayPlatform(testUrl, "ios", bytesBody)
if err != nil {
return nil, err
}
}
return result, nil
}
// RequestToPayPlatform parsing the answer and issuing an answer on a given platform
func RequestToPayPlatform(url, platform string, data *bytes.Buffer) (*IOSResponseData, error) {
resp, err := http.Post(url, "application/json", data)
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if platform == "ios" {
responseData := &IOSResponseData{}
err = json.Unmarshal([]byte(body), &responseData)
if err != nil {
return nil, err
}
return responseData, nil
}
return nil, nil
}