iOS 开发每天分享优质文章iOS

iOS开发 - 苹果内购支付工具类

2019-08-01  本文已影响0人  阿唯不知道

亲测可用,这里做个记录,内购完整流程参考原文

调用方式

// 支付结果监听
[YWPayHepler shareHelper].payResultBlock = ^(BOOL result, NSString * _Nonnull resultMsg) {
    NSLog(@"结果回调:%@ ---> %@", (result ? @"支付成功" : @"支付失败"), resultMsg);
};

YWIPAPayHepler .h

#import <Foundation/Foundation.h>

typedef enum {
    SIAPPurchSuccess = 0,       // 购买成功
    SIAPPurchFailed = 1,        // 购买失败
    SIAPPurchCancle = 2,        // 取消购买
    SIAPPurchVerFailed = 3,     // 订单校验失败
    SIAPPurchVerSuccess = 4,    // 订单校验成功
    SIAPPurchNotArrow = 5,      // 不允许内购
}SIAPPurchType;

typedef void (^IAPCompletionHandle)(SIAPPurchType type, NSData *data);

@interface YWIPAPayHepler : NSObject

/**
 * 内购单例
 */
+ (instancetype)shareIAPPayHepler;

/**
 开始内购
 @param purchID     产品id
 @param handle      回调结果
 */
- (void)startPurchWithID:(NSString *)purchID completeHandle:(IAPCompletionHandle)handle;

@end

YWIPAPayHepler .m

#import "YWIPAPayHepler.h"
#import <StoreKit/StoreKit.h>

// 日志打印
#ifdef DEBUG
#   define YWLog(fmt, ...) NSLog((@"\n⭐️⭐️⭐️: %s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define YWLog(...)
#endif

@interface YWIPAPayHepler() <SKPaymentTransactionObserver,SKProductsRequestDelegate> {
    NSString                *_purchID;
    IAPCompletionHandle     _handle;
}

@end

@implementation YWIPAPayHepler

#pragma mark - ♻️life cycle
+ (instancetype)shareIAPPayHepler {
    
    static YWIPAPayHepler *IAPPayHepler = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        IAPPayHepler = [[YWIPAPayHepler alloc] init];
    });
    return IAPPayHepler;
}
- (instancetype)init{
    self = [super init];
    if (self) {
        // 购买监听写在程序入口,程序挂起时移除监听,这样如果有未完成的订单将会自动执行并回调 paymentQueue:updatedTransactions:方法
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    }
    return self;
}

- (void)dealloc{
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}


#pragma mark - * * * * * 🚪public * * * * *
- (void)startPurchWithID:(NSString *)purchID completeHandle:(IAPCompletionHandle)handle {
    if (purchID) {
        if ([SKPaymentQueue canMakePayments]) {
            // 开始购买服务
            _purchID = purchID;
            _handle = handle;
            NSSet *nsset = [NSSet setWithArray:@[purchID]];
            SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
            request.delegate = self;
            [request start];
        }else{
            [self handleActionWithType:SIAPPurchNotArrow data:nil];
        }
    }
}


#pragma mark - * * * * * 🔒private * * * * *
- (void)handleActionWithType:(SIAPPurchType)type data:(NSData *)data {
    
#if DEBUG
    switch (type) {
        case SIAPPurchSuccess:
            YWLog(@"购买成功");
            break;
        case SIAPPurchFailed:
            YWLog(@"购买失败");
            break;
        case SIAPPurchCancle:
            YWLog(@"用户取消购买");
            break;
        case SIAPPurchVerFailed:
            YWLog(@"订单校验失败");
            break;
        case SIAPPurchVerSuccess:
            YWLog(@"订单校验成功");
            break;
        case SIAPPurchNotArrow:
            YWLog(@"不允许程序内付费");
            break;
        default:
            break;
    }
#endif
    if(_handle){
        // 回调凭证数据
        _handle(type, data);
    }
}


#pragma mark - * * * * * 🍐delegate * * * * *
// 交易结束
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
    // Your application should implement these two methods.
    NSString * productIdentifier = transaction.payment.productIdentifier;
    // 解码购买凭证
    NSString *receipt = [transaction.transactionReceipt base64EncodedString];
    if ([productIdentifier length] > 0) {
        // 如果是真正需要内购,则在此处向自己的服务器验证购买凭证
        YWLog(@"购买凭证为:%@", receipt);
        // 验证成功与否都注销交易,否则会出现虚假凭证信息一直验证不通过,每次进程序都得输入苹果账号
        //[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
    }
    
    YWLog(@"支付交易结束,App向苹果发起验证");
    // App向苹果发起验证(这里因为苹果服务器不稳定,所以真正的内购 不是App向苹果发起验证,而是上面的向自己的应用服务器发起验证,由服务器向苹果验证)
    [self verifyPurchaseWithPaymentTransaction:transaction isTestServer:NO];
}

// 交易失败
- (void)failedTransaction:(SKPaymentTransaction *)transaction{
    if (transaction.error.code != SKErrorPaymentCancelled) {
        [self handleActionWithType:SIAPPurchFailed data:nil];
    }else{
        [self handleActionWithType:SIAPPurchCancle data:nil];
    }
    
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

- (void)verifyPurchaseWithPaymentTransaction:(SKPaymentTransaction *)transaction isTestServer:(BOOL)flag{
    //交易验证
    NSURL *recepitURL = [[NSBundle mainBundle] appStoreReceiptURL];
    NSData *receipt = [NSData dataWithContentsOfURL:recepitURL];
    
    if(!receipt){
        // 交易凭证为空验证失败
        [self handleActionWithType:SIAPPurchVerFailed data:nil];
        return;
    }
    // 购买成功将交易凭证发送给服务端进行再次校验
    [self handleActionWithType:SIAPPurchSuccess data:receipt];
    
    NSError *error;
    NSDictionary *requestContents = @{
                                      @"receipt-data": [receipt base64EncodedStringWithOptions:0]
                                      };
    NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                          options:0
                                                            error:&error];
    
    if (!requestData) { // 交易凭证为空验证失败
        [self handleActionWithType:SIAPPurchVerFailed data:nil];
        return;
    }
    
    //In the test environment, use https://sandbox.itunes.apple.com/verifyReceipt
    //In the real environment, use https://buy.itunes.apple.com/verifyReceipt
    // 正式服务器环境验证地址
    NSString *serverString = @"https://buy.itunes.apple.com/verifyReceipt";
    if (flag) {
        // 沙盒测试环境验证地址
        serverString = @"https://sandbox.itunes.apple.com/verifyReceipt";
    }
    NSURL *storeURL = [NSURL URLWithString:serverString];
    NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
    [storeRequest setHTTPMethod:@"POST"];
    [storeRequest setHTTPBody:requestData];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                               if (connectionError) {
                                   // 无法连接服务器,购买校验失败
                                   [self handleActionWithType:SIAPPurchVerFailed data:nil];
                               } else {
                                   NSError *error;
                                   NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
                                   if (!jsonResponse) {
                                       // 苹果服务器校验数据返回为空校验失败
                                       [self handleActionWithType:SIAPPurchVerFailed data:nil];
                                   }
                                   
                                   // 先验证正式服务器,如果正式服务器返回21007再去苹果测试服务器验证,沙盒测试环境苹果用的是测试服务器
                                   NSString *status = [NSString stringWithFormat:@"%@",jsonResponse[@"status"]];
                                   if (status && [status isEqualToString:@"21007"]) {
                                       [self verifyPurchaseWithPaymentTransaction:transaction isTestServer:YES];
                                   }else if(status && [status isEqualToString:@"0"]){
                                       [self handleActionWithType:SIAPPurchVerSuccess data:nil];
                                   }
                                   YWLog(@"----验证结果 %@",jsonResponse);
                               }
                           }];
    
    // 验证成功与否都注销交易,否则会出现虚假凭证信息一直验证不通过,每次进程序都得输入苹果账号
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

#pragma mark - * * * * * SKProductsRequestDelegate * * * * *
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    NSArray *product = response.products;
    if([product count] <= 0){
        YWLog(@"--------------没有商品------------------");
        return;
    }
    
    SKProduct *p = nil;
    for(SKProduct *pro in product){
        if([pro.productIdentifier isEqualToString:_purchID]){
            p = pro;
            break;
        }
    }
    
    YWLog(@"沙盒测试时为空productID:%@", response.invalidProductIdentifiers);
    YWLog(@"产品付费数量:%lu",(unsigned long)[product count]);
    YWLog(@"产品描述:%@",[p description]);
    YWLog(@"产品localizedTitle:%@",[p localizedTitle]);
    YWLog(@"产品localizedDescription:%@",[p localizedDescription]);
    YWLog(@"产品价格:%@",[p price]);
    YWLog(@"产品ID标志:%@",[p productIdentifier]);
    YWLog(@"向苹果发送购买请求");
    
    SKPayment *payment = [SKPayment paymentWithProduct:p];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

//请求失败
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
    YWLog(@"------------------请求错误-----------------:%@", error);
}

//请求完成
- (void)requestDidFinish:(SKRequest *)request{
    YWLog(@"------------请求完成,反馈信息结束-----------------");
}

#pragma mark - * * * * * SKPaymentTransactionObserver * * * * *
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions{
    for (SKPaymentTransaction *tran in transactions) {
        switch (tran.transactionState) {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:tran];
                break;
            case SKPaymentTransactionStatePurchasing:
                YWLog(@"商品添加进列表");
                break;
            case SKPaymentTransactionStateRestored:
                YWLog(@"已经购买过商品");
                // 消耗型不支持恢复购买
                [[SKPaymentQueue defaultQueue] finishTransaction:tran];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:tran];
                break;
            default:
                break;
        }
    }
}
@end
上一篇 下一篇

猜你喜欢

热点阅读