鍍金池/ 教程/ iOS/ 支付請求
恢復(fù)購買記錄
內(nèi)置購買
介紹
準(zhǔn)備應(yīng)用程序?qū)彶?/span>
創(chuàng)建和配置產(chǎn)品
獲取產(chǎn)品信息
支付請求
使用訂閱
如何測試應(yīng)用程序內(nèi)置購買
產(chǎn)品交付

支付請求

In the second part of the purchase process, after the user has chosen to purchase a particular product, your app submits a payment request to the App Store, as shown in Figure 3-1.

購買過程的第二步是,在用戶已經(jīng)選擇好要購買的產(chǎn)品后,你的應(yīng)用程序向應(yīng)用商店提交一個支付請求,如下圖:

Figure 3-1 Stages of the purchase process—requesting payment http://wiki.jikexueyuan.com/project/in-app-purchase/images/6.png" alt="" />

Creating a Payment Request

一、創(chuàng)建一個支付請求

When the user selects a product to buy, create a payment request using a product object, and set the quantity if needed, as shown in Listing 3-1. The product object comes from the array of products returned by your app’s products request, as discussed in “Retrieving Product Information.”

當(dāng)用戶選擇了一個要買的產(chǎn)品后,使用一個產(chǎn)品對象創(chuàng)建一個支付請求,并根據(jù)需要設(shè)置好購買數(shù)量,如下列表。 正如In-App Purchase----(三) ----Retrieving Product Information 中所述,產(chǎn)品對象來自應(yīng)用的 products requst 返回的數(shù)組。 Listing 3-1 Creating a payment request

SKProduct *product = <# Product returned by a products request #>;
SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
payment.quantity = 2;

Detecting Irregular Activity

二、檢測不規(guī)則活動

The App Store uses an irregular activity detection engine to help combat fraud. Some apps can provide additional information to improve the engine’s ability to detect unusual transactions. If your users have an account with you, in addition to their App Store accounts, provide this additional piece of information when requesting payment.

應(yīng)用商店使用一個不規(guī)則活動檢測引擎來幫助打擊欺騙行為。 一個應(yīng)用程序可以提供額外的信息來提高引擎的性能以便檢測不正常的交易。 如果你的用戶除了有自己的應(yīng)用商店賬號外,還有一個你的賬號,就可以在請求支付時提供該額外的信息片段。

By way of illustration, consider the following two examples. In the normal case, many different users on your server each buy coins to use in your game, and each user pays for the purchase from a different App Store account. In contrast, it would be very unusual for a single user on your server to buy coins multiple times, paying for each purchase from a different App Store account. The App Store can’t detect this kind of irregular activity on its own—it needs information from your app about which account on your server is associated with the transaction.

通過演示的方法,考慮以下兩個例子。 在正常情況下,你的服務(wù)器上的很多不同的用戶購買游戲中金幣(coins),并且每個用戶都從一個不同的應(yīng)用商店賬號購買。相反,如果同一個用戶在你的服務(wù)器上多次購買金幣,但是每次購買都從不同的應(yīng)用商店賬號上支付就非常不尋常。應(yīng)用商店自己不能檢測到該類型的不規(guī)則活動--它需要從你的應(yīng)用程序中知道你服務(wù)器上的哪個賬號跟該筆交易有關(guān)聯(lián)。

To provide this information, populate the applicationUsername property of the payment object with a one-way hash of the user’s account name on your server, such as in the example shown in Listing 3-2.

要想提供該信息,用你服務(wù)器中的用戶賬號名稱,以單向散列方式填寫applicationUsername特性,如下:

Listing 3-2 Providing an application username

# import <CommonCrypto/CommonCrypto.h>

// Custom method to calculate the SHA-256 hash using Common Crypto
- (NSString *)hashedValueForAccountName:(NSString*)userAccountName
{
    const int HASH_SIZE = 32;
    unsigned char hashedChars[HASH_SIZE];
    const char *accountName = [userAccountName UTF8String];
    size_t accountNameLen = strlen(accountName);

    // Confirm that the length of the user name is small enough
    // to be recast when calling the hash function.
    if (accountNameLen > UINT32_MAX) {
        NSLog(@"Account name too long to hash: %@", userAccountName);
        return nil;
    }
    CC_SHA256(accountName, (CC_LONG)accountNameLen, hashedChars);

    // Convert the array of bytes into a string showing its hex representation.
    NSMutableString *userAccountHash = [[NSMutableString alloc] init];
    for (int i = 0; i < HASH_SIZE; i++) {
        // Add a dash every four bytes, for readability.
        if (i != 0 && i%4 == 0) {
            [userAccountHash appendString:@"-"];
        }
        [userAccountHash appendFormat:@"%02x", hashedChars[i]];
    }

    return userAccountHash;
}

If you use another approach to populate this property, ensure that the value you provide is an opaque identifier uniquely associated with the user’s account on your server. Don’t use the Apple ID for your developer account, the user’s Apple ID, or the user’s unhashed account name on your server.

如果你使用另一種方法填寫該特性,請確保你提供的值是一個不透明的識別碼,它只跟你服務(wù)器上的用戶賬號有關(guān)。 不要使用蘋果 ID 作為你的開發(fā)者賬號,用戶的蘋果 ID,或用戶在你的服務(wù)器的非散列賬戶名。

Submitting a Payment Request

三、遞交一個支付請求

Adding a payment request to the transaction queue submits it the App Store. If you add a payment object to the queue multiple times, it’s submitted multiple times—the user is charged multiple times and your app is expected to deliver the product multiple times.

向要遞交給應(yīng)用商店的交易隊列添加一個支付請求。 如果你多次把一個支付對象添加到隊列,它就會被提交多次---用戶就會被多次要求支付并且應(yīng)用程序就回多次傳遞產(chǎn)品。

[[SKPaymentQueue defaultQueue] addPayment:payment];\

For every payment request your app submits, it gets back a corresponding transaction that it must process. Transactions and the transaction queue are discussed in “Waiting for the App Store to Process Transactions.”

對于你的應(yīng)用程序遞交的每個支付請求,都會返回一個相應(yīng)地交易,它必須處理它。 交易和交易隊列在“Waiting for the App Store to Process Transactions.”中討論。