iOS 应用内支付(内购)

2016-08-16  本文已影响1357人  a2ebcc3676c1

一、iTunes Connect 相关设置

二、代码相关

#import <StoreKit/StoreKit.h>
#import <StoreKit/SKPaymentTransaction.h>
-(id)initWithDelegate:(id)delegate{
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    self.delegate = delegate;
    return self;
}

-(void)removePaymentFromQueue{
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
    self.delegate = nil;
}

-(void)buy:(NSString*)productId{
    if(![SKPaymentQueue canMakePayments]){
        pr(@"error:不支持内购");
    }else{
        NSSet *nsset = [NSSet setWithArray:@[productId]];
        SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
        request.delegate=self;
        [request start];
    }
}

#pragma mark SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    NSArray *productArray = response.products;
    if(productArray != nil && productArray.count>0){
        SKMutablePayment * payment = [SKMutablePayment paymentWithProduct:productArray[0]];
        [[SKPaymentQueue defaultQueue] addPayment:payment];
    }
}
#pragma mark SKPaymentTransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
    for(SKPaymentTransaction* transaction in transactions){
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased://交易完成
                if ([self.delegate respondsToSelector:@selector(didSuccess:)]) {
                    [self.delegate didSuccess:transaction];
                }
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored://已经购买过
                if ([self.delegate respondsToSelector:@selector(didRestored:)]) {
                    [self.delegate didRestored:transaction];
                }
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed://购买失败
                if ([self.delegate respondsToSelector:@selector(didFiled:)]) {
                    [self.delegate didFiled:transaction];
                }
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            default:break;
        }
    }
}

- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions{
    pr(@"transactions:%@",transactions);
}
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error{
    pr(@"error:%@",error);
}
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue{
    pr(@"queue:%@",queue);
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray *)downloads{
    pr(@"downloads:%@",downloads);
}
NSData * receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
NSString * desc = [receiptData base64EncodedStringWithOptions:0];
//向苹果苹果服务器验证支付结果
- (void)dd {
    NSURLRequest * appstoreRequest = [NSURLRequest requestWithURL:[[NSBundle mainBundle]appStoreReceiptURL]];
    NSData * receiptData = [NSURLConnection sendSynchronousRequest:appstoreRequest returningResponse:nil error:nil];

    // Create the JSON object that describes the request
    NSError *error;
    NSDictionary *requestContents = @{
                                      @"receipt-data": [receiptData base64EncodedStringWithOptions:0]
                                      };
    NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                          options:0
                                                            error:&error];

    if (!requestData) { /* ... Handle error ... */ }

    // Create a POST request with the receipt data.
    NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
    NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
    [storeRequest setHTTPMethod:@"POST"];
    [storeRequest setHTTPBody:requestData];

    // Make a connection to the iTunes Store on a background queue.
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                               if (connectionError) {
                                   /* ... Handle error ... */
                               } else {
                                   NSError *error;
                                   NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
                                   if (!jsonResponse) {
                                       /* ... Handle error ...*/
                                   }
                                   /* ... Send a response back to the device ... */
                               }
                           }];

}
上一篇 下一篇

猜你喜欢

热点阅读