goProject/trunk/goutil/appChargeUtil/verify.go
皮蛋13361098506 1b77f62820 初始化项目
2025-01-06 16:01:02 +08:00

83 lines
1.8 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package appChargeUtil
import (
"errors"
"fmt"
"goutil/webUtil"
)
const (
con_SandBoxUrl = "https://sandbox.itunes.apple.com/verifyReceipt"
con_ProductionUrl = "https://buy.itunes.apple.com/verifyReceipt"
)
var (
NetworkError = errors.New("NetWorkError")
)
// 验证充值是否有效
// bundleIdentifierList配置的BundleIdentifier列表
// productId输入的ProductId
// receiptData订单数据
// isSandBox是否为沙盒模式
// 返回值:
// 充值收据对象
// 是否有效
// 错误对象如果err==NetWorkError,则表明为网络错误)
func ValidateCharge(bundleIdentifierList []string, productId, receiptData string, isSandBox bool) (receiptObj *Receipt, isValid bool, err error) {
// 判断参数是否为空
if len(bundleIdentifierList) == 0 || productId == "" || receiptData == "" {
return
}
// 获取Receipt对象
receiptObj, err = getReceipt(receiptData, isSandBox)
if err != nil {
return
}
if receiptObj.IsBundleIdentifierValid(bundleIdentifierList) == false {
return
}
if receiptObj.IsProductIdValid(productId) == false {
return
}
isValid = true
return
}
func getReceipt(receiptData string, isSandBox bool) (receiptObj *Receipt, err error) {
weburl := con_ProductionUrl
if isSandBox {
weburl = con_SandBoxUrl
}
data := []byte(convertReceiptToPost(receiptData))
statusCode, returnBytes, err := webUtil.PostByteData2(weburl, data, webUtil.GetFormHeader(), nil)
if err != nil {
err = NetworkError
return
}
if statusCode != 200 {
err = fmt.Errorf("StatusCode is wrong:%d", statusCode)
return
}
if len(returnBytes) == 0 {
err = fmt.Errorf("返回的数据为空")
return
}
receiptObj, err = newReceipt(string(returnBytes))
return
}
func convertReceiptToPost(receiptData string) string {
return fmt.Sprintf("{\"receipt-data\":\"%s\"}", receiptData)
}