Fetch the repository succeeded.
package apple
import (
"context"
"crypto/ecdsa"
"net/http"
"gitee.com/ujq/gopay"
"gitee.com/ujq/gopay/pkg/xhttp"
)
// Client AppleClient
type Client struct {
iss string // Your issuer ID from the Keys page in App Store Connect (Ex: "57246542-96fe-1a63-e053-0824d011072a")
bid string // Your app’s bundle ID (Ex: “com.example.testbundleid2021”)
kid string // Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
isProd bool // 是否是正式环境
privateKey *ecdsa.PrivateKey
hc *xhttp.Client
}
// NewClient 初始化Apple客户端
// iss:issuer ID
// bid:bundle ID
// kid:private key ID
// privateKey:私钥文件读取后的字符串内容
// isProd:是否是正式环境
func NewClient(iss, bid, kid, privateKey string, isProd bool) (client *Client, err error) {
if iss == gopay.NULL || bid == gopay.NULL || kid == gopay.NULL || privateKey == gopay.NULL {
return nil, gopay.MissAppleInitParamErr
}
ecPrivateKey, err := ParseECPrivateKeyFromPEM([]byte(privateKey))
if err != nil {
return nil, err
}
//ecPrivateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
//if err != nil {
// panic(err)
//}
client = &Client{
iss: iss,
bid: bid,
kid: kid,
privateKey: ecPrivateKey,
isProd: isProd,
hc: xhttp.NewClient(),
}
return client, nil
}
func (c *Client) doRequestGet(ctx context.Context, path string) (res *http.Response, bs []byte, err error) {
uri := hostUrl + path
if !c.isProd {
uri = sandBoxHostUrl + path
}
token, err := c.generatingToken()
if err != nil {
return nil, nil, err
}
req := c.hc.Req()
req.Header.Set("Authorization", "Bearer "+token)
res, bs, err = req.Get(uri).EndBytes(ctx)
if err != nil {
return nil, nil, err
}
return res, bs, nil
}
func (c *Client) doRequestPost(ctx context.Context, path string, bm gopay.BodyMap) (res *http.Response, bs []byte, err error) {
uri := hostUrl + path
if !c.isProd {
uri = sandBoxHostUrl + path
}
token, err := c.generatingToken()
if err != nil {
return nil, nil, err
}
req := c.hc.Req()
req.Header.Set("Authorization", "Bearer "+token)
res, bs, err = req.Post(uri).SendBodyMap(bm).EndBytes(ctx)
if err != nil {
return nil, nil, err
}
return res, bs, nil
}
func (c *Client) doRequestPut(ctx context.Context, path string, bm gopay.BodyMap) (res *http.Response, bs []byte, err error) {
uri := hostUrl + path
if !c.isProd {
uri = sandBoxHostUrl + path
}
token, err := c.generatingToken()
if err != nil {
return nil, nil, err
}
req := c.hc.Req()
req.Header.Set("Authorization", "Bearer "+token)
res, bs, err = req.Put(uri).SendBodyMap(bm).EndBytes(ctx)
if err != nil {
return nil, nil, err
}
return res, bs, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。