appstoreserverapi

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2024 License: MIT Imports: 18 Imported by: 0

README

app store server api

文档:https://developer.apple.com/documentation/appstoreserverapi

已完成的 API

API 说明
LookupOrder 根据用户订单ID,查询用户的内购交易信息
GetTransactionInfo 根据订单交易ID,查询用户的交易信息
GetTransactionHistory 根据订单交易ID,查询用户的所有交易记录
GetAllSubscriptionStatuses 根据订阅交易ID,查询用户的所有订阅数据
GetRefundHistory 根据交易ID,查询用户的所有退款交易

以上方法的调用,请参考相应方法的 test

QA

  1. api key 如何创建?
    https://developer.apple.com/documentation/appstoreserverapi/creating_api_keys_to_use_with_the_app_store_server_api

登陆 app store connect 后台 (http://appstoreconnect.apple.com)

img_5.png

img_3.png

注意 API 密钥只能下载一次,请立即保存
img_2.png

  1. LookupOrder 方法需要的 customerOrderID 从哪里获取?

Apple 给的说明如下:
When customers make one or more in-app purchases in your app, the App Store emails them a receipt. The receipt contains an order ID. Use this order ID to call Look Up Order ID. Customers can also retrieve their order IDs from their purchase history on the App Store;

其实就是这个东西,用户反馈时候,让用户提供就可以了
img_4.png

JWS 如何验证

文档:https://developer.apple.com/documentation/appstoreserverapi/jwsdecodedheader

img.png

x5c 证书链的验证

img_1.png

参考文章

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiError

type ApiError struct {
	Code    int    `json:"errorCode"`
	Message string `json:"errorMessage"`
}

ApiError https://developer.apple.com/documentation/appstoreserverapi/error_codes

func ApiErrorFromError

func ApiErrorFromError(err error) (*ApiError, bool)

func ParseApiError

func ParseApiError(data []byte) (*ApiError, error)

func (ApiError) Error

func (e ApiError) Error() string

type AutoRenewStatus

type AutoRenewStatus int32

AutoRenewStatus https://developer.apple.com/documentation/appstoreserverapi/autorenewstatus

const (
	AutoRenewStatusOff AutoRenewStatus = 0
	AutoRenewStatusOn  AutoRenewStatus = 1
)

type AutoRenewableSubscriptionStatus

type AutoRenewableSubscriptionStatus int32

AutoRenewableSubscriptionStatus 自动续订状态 https://developer.apple.com/documentation/appstoreserverapi/status

const (
	// The auto-renewable subscription is active
	AutoRenewableSubscriptionStatusActive AutoRenewableSubscriptionStatus = 1
	// The auto-renewable subscription is expired
	AutoRenewableSubscriptionStatusExpired AutoRenewableSubscriptionStatus = 2
	// The auto-renewable subscription is in a billing retry period
	AutoRenewableSubscriptionStatusInBillingRetryPeriod AutoRenewableSubscriptionStatus = 3
	// The auto-renewable subscription is in a Billing Grace Period
	AutoRenewableSubscriptionStatusInBillingGracePeriod AutoRenewableSubscriptionStatus = 4
	// The auto-renewable subscription is revoked.
	// The App Store refunded the transaction or revoked it from Family Sharing.
	AutoRenewableSubscriptionStatusRevoked AutoRenewableSubscriptionStatus = 5
)

type Config

type Config struct {
	// Your app’s bundle ID (Ex: “com.example.testbundleid2021”)
	BundleID string
	// Your issuer ID from the Keys page in App Store Connect (Ex: "57246542-96fe-1a63-e053-0824d011072a")
	Issuer string
	// Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
	KeyID string
	// Your private key content
	PrivateKey []byte

	// http request timeout
	Timeout time.Duration
}

Config config for generate token for api 文档:https://developer.apple.com/documentation/appstoreserverapi/generating_tokens_for_api_requests

type Environment

type Environment string

Environment https://developer.apple.com/documentation/appstoreserverapi/environment

const (
	EnvironmentSandbox    Environment = "Sandbox"
	EnvironmentProduction Environment = "Production"
)

type ExpirationIntent

type ExpirationIntent int32

ExpirationIntent https://developer.apple.com/documentation/appstoreserverapi/expirationintent

const (
	ExpirationIntentCustomerCanceled                  ExpirationIntent = 1
	ExpirationIntentBillingError                      ExpirationIntent = 2
	ExpirationIntentCustomerDoNotConsentPriceIncrease ExpirationIntent = 3
	ExpirationIntentProductNotAvailable               ExpirationIntent = 4
	ExpirationIntentOtherReason                       ExpirationIntent = 5
)

type GetAllSubscriptionStatusesResp

type GetAllSubscriptionStatusesResp struct {
	Environment Environment                       `json:"environment"`
	AppAppleID  int64                             `json:"appAppleId"`
	BundleID    string                            `json:"bundleId"`
	Data        []SubscriptionGroupIdentifierItem `json:"data"`
}

type GetRefundHistoryResp

type GetRefundHistoryResp struct {
	HasMore            bool             `json:"hasMore"`
	Revision           string           `json:"revision"`
	SignedTransactions []JWSTransaction `json:"signedTransactions"`
	// contains filtered or unexported fields
}

func (*GetRefundHistoryResp) Next

Next GetTransactionHistoryResp 的下一页

type GetTransactionHistoryReq

type GetTransactionHistoryReq struct {
	TransactionID string
	Query         *GetTransactionHistoryReqQuery
}

type GetTransactionHistoryReqQuery

type GetTransactionHistoryReqQuery struct {
	// A token you provide to get the next set of up to 20 transactions. All responses include a revision token.
	// Use the revision token from the previous HistoryResponse
	Revision string `form:"revision"`
	// An optional start date of the timespan for the transaction history records you’re requesting.
	// The startDate needs to precede the endDate if you specify both dates.
	// The results include a transaction if its purchaseDate is equal to or greater than the startDate
	// The start date of a timespan, expressed in UNIX time, in milliseconds
	StartDate int64 `form:"startDate"`
	// An optional end date of the timespan for the transaction history records you’re requesting.
	// Choose an endDate that’s later than the startDate if you specify both dates.
	// Using an endDate in the future is valid.
	// The results include a transaction if its purchaseDate is earlier than the endDate.
	EndDate int64 `form:"endDate"`

	// An optional filter that indicates the product identifier to include in the transaction history.
	// Your query may specify more than one productID
	ProductID []string `form:"productId"`

	// An optional filter that indicates the product type to include in the transaction history.
	// Your query may specify more than one productType
	ProductType []ProductType `form:"productType"`

	// An optional sort order for the transaction history records.
	// The response sorts the transaction records by their recently modified date.
	// The default value is ASCENDING, so you receive the oldest records first
	Sort string `form:"sort"`

	// An optional filter that indicates the subscription group identifier to include in the transaction history.
	// Your query may specify more than one subscriptionGroupIdentifier
	SubscriptionGroupIdentifier []string `form:"subscriptionGroupIdentifier"`

	// An optional filter that limits the transaction history by the in-app ownership type
	InAppOwnershipType InAppOwnershipType `form:"inAppOwnershipType"`

	// An optional Boolean value that indicates whether the response includes only revoked transactions
	// when the value is true, or contains only nonrevoked transactions when the value is false.
	// By default, the request doesn't include this parameter
	Revoked *bool `form:"revoked"`
}

func (*GetTransactionHistoryReqQuery) Values

type GetTransactionHistoryResp

type GetTransactionHistoryResp struct {
	AppAppleID         int64            `json:"appAppleId"`
	BundleID           string           `json:"bundleId"`
	Environment        Environment      `json:"environment"`
	HasMore            bool             `json:"hasMore"`
	Revision           string           `json:"revision"`
	SignedTransactions []JWSTransaction `json:"signedTransactions"`
	// contains filtered or unexported fields
}

GetTransactionHistoryResp A response that contains the customer’s transaction history for an app

func (*GetTransactionHistoryResp) GetTransactions added in v0.1.2

func (resp *GetTransactionHistoryResp) GetTransactions() ([]Transaction, error)

GetTransactions 获取当前返回数据中的交易信息

func (*GetTransactionHistoryResp) Next

Next GetTransactionHistory 的下一页

type InAppOwnershipType

type InAppOwnershipType string

InAppOwnershipType https://developer.apple.com/documentation/appstoreserverapi/inappownershiptype

const (
	InAppOwnershipTypeFamilyShared InAppOwnershipType = "FAMILY_SHARED"
	InAppOwnershipTypePurchased    InAppOwnershipType = "PURCHASED"
)

type JWSRenewalInfo

type JWSRenewalInfo string

func (JWSRenewalInfo) GetRenewInfo added in v0.1.2

func (s JWSRenewalInfo) GetRenewInfo() (*RenewalInfo, error)

type JWSTransaction

type JWSTransaction string

func (JWSTransaction) GetTransaction added in v0.1.2

func (s JWSTransaction) GetTransaction() (*Transaction, error)

type LookupOrderResp

type LookupOrderResp struct {
	// 0 contains an array of one or more signed transactions for the in-app purchase based on the order ID.
	// 1 1 doesn't contain a signed transactions array.
	Status             int              `json:"status"`
	SignedTransactions []JWSTransaction `json:"signedTransactions"`
}

type OfferDiscountType

type OfferDiscountType string

OfferDiscountType https://developer.apple.com/documentation/appstoreserverapi/offerdiscounttype

const (
	OfferDiscountTypeFreeTrial  OfferDiscountType = "FREE_TRIAL"
	OfferDiscountTypePayAsYouGo OfferDiscountType = "PAY_AS_YOU_GO"
	OfferDiscountTypePayUpFront OfferDiscountType = "PAY_UP_FRONT"
)

type OfferType

type OfferType int

OfferType https://developer.apple.com/documentation/appstoreserverapi/offertype

const (
	OfferTypeIntroductory OfferType = 1
	OfferTypePromotional  OfferType = 2
	OfferTypeSubscription OfferType = 3
)

type ProductType

type ProductType string
const (
	ProductTypeAutoRenewable ProductType = "AUTO_RENEWABLE"
	ProductTypeNonRenewable  ProductType = "NON_RENEWABLE"
	ProductTypeConsumable    ProductType = "CONSUMABLE"
	ProductTypeNonConsumable ProductType = "NON_CONSUMABLE"
)

type RenewalInfo added in v0.1.2

type RenewalInfo struct {
	AutoRenewProductID          string           `json:"autoRenewProductId"`
	AutoRenewStatus             AutoRenewStatus  `json:"autoRenewStatus"`
	Environment                 Environment      `json:"environment"`
	ExpirationIntent            ExpirationIntent `json:"expirationIntent"`
	GracePeriodExpiresDate      int64            `json:"gracePeriodExpiresDate"`
	IsInBillingRetryPeriod      bool             `json:"isInBillingRetryPeriod"`
	OfferIdentifier             string           `json:"offerIdentifier"`
	OfferType                   OfferType        `json:"offerType"`
	OriginalTransactionID       string           `json:"originalTransactionId"`
	PriceIncreaseStatus         *int32           `json:"priceIncreaseStatus"`
	ProductID                   string           `json:"productId"`
	RecentSubscriptionStartDate int64            `json:"recentSubscriptionStartDate"`
	RenewalDate                 int64            `json:"renewalDate"`
	SignedDate                  int64            `json:"signedDate"`
}

RenewalInfo subscription renewal information for an auto-renewable subscription https://developer.apple.com/documentation/appstoreserverapi/jwsrenewalinfodecodedpayload

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service app store server api 实现 api: https://developer.apple.com/documentation/appstoreserverapi/generating_tokens_for_api_requests

func NewService

func NewService(token *Token) *Service

func (*Service) Debug

func (s *Service) Debug(debug bool) *Service

func (*Service) GetAllSubscriptionStatuses

func (s *Service) GetAllSubscriptionStatuses(ctx context.Context, transactionID string, status []AutoRenewableSubscriptionStatus) (*GetAllSubscriptionStatusesResp, error)

GetAllSubscriptionStatuses Get the statuses for all of a customer’s auto-renewable subscriptions in your app.

  • status: An optional filter that indicates the status of subscriptions to include in the response. Your query may specify more than one status query parameter

https://developer.apple.com/documentation/appstoreserverapi/get_all_subscription_statuses

func (*Service) GetRefundHistory

func (s *Service) GetRefundHistory(ctx context.Context, transactionID string, revision string) (*GetRefundHistoryResp, error)

GetRefundHistory https://developer.apple.com/documentation/appstoreserverapi/get_refund_history

func (*Service) GetTransactionHistory

func (s *Service) GetTransactionHistory(ctx context.Context, req *GetTransactionHistoryReq) (*GetTransactionHistoryResp, error)

GetTransactionHistory Get a customer’s in-app purchase transaction history for your app https://developer.apple.com/documentation/appstoreserverapi/get_transaction_history

func (*Service) GetTransactionInfo

func (s *Service) GetTransactionInfo(ctx context.Context, transactionID string) (*Transaction, error)

GetTransactionInfo Get information about a single transaction for your app https://developer.apple.com/documentation/appstoreserverapi/get_transaction_info

func (*Service) Host

func (s *Service) Host() string

func (*Service) LookupOrder

func (s *Service) LookupOrder(ctx context.Context, customerOrderID string) ([]Transaction, error)

LookupOrder Get a customer’s in-app purchases from a receipt using the order ID. api: https://developer.apple.com/documentation/appstoreserverapi/look_up_order_id

func (*Service) Sandbox

func (s *Service) Sandbox(sandbox bool) *Service

type Sort

type Sort string
const (
	SortAsc  Sort = "ASCENDING"
	SortDesc Sort = "DESCENDING"
)

type SubscriptionGroupIdentifierItem

type SubscriptionGroupIdentifierItem struct {
	SubscriptionGroupIdentifier string                         `json:"subscriptionGroupIdentifier"`
	LastTransactions            []SubscriptionLastTransactions `json:"lastTransactions"`
}

type SubscriptionLastTransactions

type SubscriptionLastTransactions struct {
	OriginalTransactionID string                          `json:"originalTransactionId"`
	Status                AutoRenewableSubscriptionStatus `json:"status"`
	SignedRenewalInfo     JWSRenewalInfo                  `json:"signedRenewalInfo"`
	SignedTransactionInfo JWSTransaction                  `json:"signedTransactionInfo"`
}

type Token

type Token struct {
	// contains filtered or unexported fields
}

Token JSON Web Tokens signed to authorize App Store Server API requests

func NewToken

func NewToken(conf *Config) *Token

func (*Token) Get

func (t *Token) Get() (string, error)

type Transaction added in v0.1.2

type Transaction struct {
	// A UUID you create at the time of purchase that associates the transaction with a customer on your own service.
	// If your app doesn’t provide an appAccountToken, this string is empty.
	AppAccountToken string `json:"appAccountToken,omitempty"`
	// The bundle identifier of the app.
	BundleID string `json:"bundleId,omitempty"`
	// The server environment, either sandbox or production
	Environment Environment `json:"environment,omitempty"`

	// A string that describes whether the transaction was purchased by the customer,
	// or is available to them through Family Sharing
	InAppOwnershipType InAppOwnershipType `json:"inAppOwnershipType,omitempty"`

	// The UNIX time, in milliseconds, that represents the purchase date of the original transaction identifier
	OriginalPurchaseDate int64 `json:"originalPurchaseDate,omitempty"`
	// The transaction identifier of the original purchase
	OriginalTransactionID string `json:"originalTransactionId,omitempty"`

	// The unique identifier of the product
	ProductID string `json:"productId,omitempty"`
	// The three-letter ISO 4217 currency code associated with the price parameter.
	// This value is present only if price is present
	Currency string `json:"currency,omitempty"`
	// An integer value that represents the price multiplied by 1000 of the in-app purchase or subscription offer
	// you configured in App Store Connect and that the system records at the time of the purchase.
	// For more information, see price. The currency parameter indicates the currency of this price
	Price int `json:"price,omitempty"`
	// The number of consumable products the customer purchased.
	Quantity int `json:"quantity,omitempty"`
	// The unique identifier of the transaction
	TransactionID string `json:"transactionId,omitempty"`
	// The type of the in-app purchase
	Type TransactionType `json:"type,omitempty"`
	// The reason for the purchase transaction, which indicates whether it’s a customer’s purchase or a renewal
	// for an auto-renewable subscription that the system initiates
	TransactionReason TransactionReason `json:"transactionReason,omitempty"`
	// The UNIX time, in milliseconds, that the App Store charged the customer’s account for a purchase,
	// restored product, subscription, or subscription renewal after a lapse
	PurchaseDate int64 `json:"purchaseDate,omitempty"`

	// The UNIX time, in milliseconds, that the App Store refunded the transaction or revoked it from Family Sharing
	RevocationDate int64 `json:"revocationDate,omitempty"`
	// The reason that the App Store refunded the transaction or revoked it from Family Sharing.
	RevocationReason *int `json:"revocationReason,omitempty"`

	// The UNIX time, in milliseconds, that the App Store signed the JSON Web Signature (JWS) data
	SignedDate int64 `json:"signedDate,omitempty"`
	// The three-letter code that represents the country or region associated with the App Store storefront for the purchase
	Storefront string `json:"storefront,omitempty"`
	// An Apple-defined value that uniquely identifies the App Store storefront associated with the purchase
	StorefrontID string `json:"storefrontId,omitempty"`

	// The identifier of the subscription group to which the subscription belongs.
	SubscriptionGroupIdentifier string `json:"subscriptionGroupIdentifier,omitempty"`
	// A Boolean value that indicates whether the customer upgraded to another subscription
	IsUpgraded bool `json:"isUpgraded,omitempty"`
	// The UNIX time, in milliseconds, that the subscription expires or renews.
	ExpiresDate int64 `json:"expiresDate,omitempty"`
	// The payment mode you configure for the subscription offer
	OfferDiscountType OfferDiscountType `json:"offerDiscountType,omitempty"`
	// The identifier that contains the offer code or the promotional offer identifier
	// The offerIdentifier applies only when the offerType has a value of 2 or 3.
	// The offerIdentifier provides details about the subscription offer in effect for the transaction.
	// Its value is either the offer code or the promotional offer
	OfferIdentifier string `json:"offerIdentifier,omitempty"`
	// A value that represents the promotional offer type.
	OfferType OfferType `json:"offerType,omitempty"`
	// The unique identifier of subscription purchase events across devices, including subscription renewals.
	WebOrderLineItemID string `json:"webOrderLineItemId,omitempty"`
}

Transaction A decoded payload that contains transaction information https://developer.apple.com/documentation/appstoreserverapi/jwstransactiondecodedpayload

type TransactionReason

type TransactionReason string

TransactionReason https://developer.apple.com/documentation/appstoreserverapi/transactionreason

const (
	TransactionReasonPurchase TransactionReason = "PURCHASE"
	TransactionReasonRenewal  TransactionReason = "RENEWAL"
)

type TransactionType

type TransactionType string

TransactionType https://developer.apple.com/documentation/appstoreserverapi/type

const (
	TransactionTypeAutoRenewableSubscription TransactionType = "Auto-Renewable Subscription"
	TransactionTypeNonRenewingSubscription   TransactionType = "Non-Renewing Subscription"
	TransactionTypeConsumable                TransactionType = "Consumable"
	TransactionTypeNonConsumable             TransactionType = "Non-Consumable"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL