iOS内购掉单处理教程

作者: 凯文Kevin21 | 来源:发表于2021-01-28 11:52 被阅读0次

    1.前言

    最近公司的项目需要接入苹果内购支付。看了下项目里面,内购这个模块的功能之前就已经写好了,然后就跟后台调试了一下,苹果后台里添加内购的商品id.沙盒测试里添加测试账号,一堆操作猛如虎。调试通了就上线了,好家伙。上线第二天客服那边就反馈了问题: 有用户反应账号已经充值成功了,也扣钱了,商品没有发货。
    定位问题:掉单,肯定是掉单!马上打开项目,看了下这块的逻辑根本没有处理,老夫大意了。心里默默诅咒之前的ios同事,挖坑埋我???好家伙。

    2.内购支付时序图

    在解决问题之前,先通过一张图来了解一下苹果的内购流程。


    内购流程.png

    3.掉单的概念

    用户选定商品支付完成后,服务器不能正确及时的获取支付状态,导致这笔已支付的订单未能发货。

    4.掉单的原因

    1. 手机网络情况复杂多变。
    2. 苹果服务器在境外连通性不确定。
      以上两个客观原因,我们无法改变,会导致如下情况:
      1. 用户支付完成之后,苹果服务器将支付票据返回给客户端,客户端发送票据到游戏服务器。(可能会断网,未能提交到游戏服务器)
      2. 游戏服务器拿到支付票据,请求苹果服务器票据验证。(游戏服务器连接苹果服务器超时,未能验证票据)。

    5.优化方案

    掉单的原因目前已经找到了,那么需要从客户端和服务器两个方面做优化,彻底解决掉iOS支付掉单问题。

    5.1 客户端需要做什么优化

    客户端在拿到苹果支付票据后,一定要先将支付票据和用户账号做映射,标记为未验证,保存到本地数据库中,然后把票据提交到游戏服务器,在确保得到游戏服务器的反馈,在将本地数据库中该条记录删除,确保游戏服务器收到该票据。

    5.2 服务器端做什么优化?

    服务器接收到客户端的票据以及验证信息时,先将票据存储到数据库中,然后请求苹果服务器验证票据,如果因为连接苹果服务器超时或者其他网络情况,标记该票据为验证证状态,后续交给定时任务处理,确保能够正确验证票据结果。

    6. 客户端优化关键代码

    //
    //  XMIAPManager.h
    #import <Foundation/Foundation.h>
    
    typedef NS_ENUM(NSUInteger,IAPResultType) {
        IAPResultSuccess = 0,       // 购买成功
        IAPResultFailed = 1,        // 购买失败
        IAPResultCancle = 2,        // 取消购买
        IAPResultVerFailed = 3,     // 订单校验失败
        IAPResultVerSuccess = 4,    // 订单校验成功
        IAPResultNotArrow = 5,      // 不允许内购
        IAPResultIDError = 6,       // 项目ID错误
    };
    
    
    
    typedef void(^IAPCompletionHandle)(IAPResultType type, NSData *data);
    
    @interface KDIAPManager : NSObject
    
    + (instancetype)shareIAPManager;
    
    /** 检测客户端与服务器漏单情况处理*/
    + (void)checkOrderStatus;
    
    /**
     开启内购
     
     @param productID 内购项目的产品ID
     @param handle 内购的结果回调
     */
    - (void)startIAPWithOrderId:(NSString *)orderId productID:(NSString *)productID completeHandle: (IAPCompletionHandle)handle;
    
    @end
    
    //
    //  KDIAPManager.m
    //  KDChat
    //
    //  Created by JYJ on 2019/6/14.
    //  Copyright © 2019 dcjf. All rights reserved.
    //
    
    #import "KDIAPManager.h"
    #import <StoreKit/StoreKit.h>
    //#import "KDMainPresenter.h"
    #import "XSNetwork.h"
    
    @interface KDIAPManager() <SKPaymentTransactionObserver,SKProductsRequestDelegate> {
        IAPCompletionHandle _handle;
    }
    
    @property (nonatomic, strong) NSString* productId;
    
    @property (nonatomic, strong) NSString* orderId;
    
    /** presenter */
    //请求网络的.
    //@property (nonatomic, strong) KDMainPresenter *presenter;
    
    @end
    
    
    @implementation KDIAPManager
    
    /**
     单例模式
     
     @return HZIAPManager
     */
    + (instancetype)shareIAPManager {
        static KDIAPManager *IAPManager = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            IAPManager = [[KDIAPManager alloc] init];
        });
        return IAPManager;
    }
    
    - (instancetype)init {
        if (self = [super init]) {
            [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
        }
        return self;
    }
    - (void)dealloc {
        [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
    }
    
    #pragma mark -- Method
    /** 检测客户端与服务器漏单情况处理*/
    + (void)checkOrderStatus {
        NSDictionary *orderInfo = [KDIAPManager getReceiptData];
        
        if (orderInfo != nil) {
            NSString *orderId = orderInfo[@"orderId"];
            id receipt = orderInfo[@"receipt"];
            [[KDIAPManager shareIAPManager] verifyPurchaseForServiceWithOrderId:orderId receipt:receipt];
        }
    }
    
    #pragma mark -- 结束上次未完成的交易
    - (void)removeAllUncompleteTransactionsBeforeNewPurchase {
        NSArray* transactions = [SKPaymentQueue defaultQueue].transactions;
        if (transactions.count >= 1) {
            for (SKPaymentTransaction* transaction in transactions) {
                if (transaction.transactionState == SKPaymentTransactionStatePurchased ||
                    transaction.transactionState == SKPaymentTransactionStateRestored) {
                    [[SKPaymentQueue defaultQueue]finishTransaction:transaction];
                }
            }
        } else {
            NSLog(@"没有历史未消耗订单");
        }
    }
    
    - (void)startIAPWithOrderId:(NSString *)orderId  productID:(NSString *)productID completeHandle: (IAPCompletionHandle)handle {
        _handle = handle;
        if(productID && productID.length > 0) {
            if ([SKPaymentQueue canMakePayments]) {
                [self removeAllUncompleteTransactionsBeforeNewPurchase];
                self.orderId = orderId;
                // 允许内购
                self.productId = productID;
                NSSet *set = [NSSet setWithObjects:productID, nil];
                SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
                request.delegate = self;
                // 获取内购项目信息
                [request start];
            } else {
                // 不允许内购
                [self handleActionWithType:IAPResultNotArrow data:nil];
            }
        } else {
            NSLog(@"内购项目ID错误");
            [XSNetwork errorWithCode:-3 message:@"产品暂时不可用,请检查苹果后台"];
            [self handleActionWithType:IAPResultIDError data:nil];
        }
        
    }
    - (void)handleActionWithType:(IAPResultType)type data:(NSData *)data{
        switch (type) {
            case IAPResultSuccess:
                NSLog(@"购买成功");
                break;
            case IAPResultFailed:
                NSLog(@"购买失败");
                break;
            case IAPResultCancle:
                NSLog(@"用户取消购买");
                break;
            case IAPResultVerFailed:
                NSLog(@"订单校验失败");
                break;
            case IAPResultVerSuccess:
                NSLog(@"订单校验成功");
                break;
            case IAPResultNotArrow:
                NSLog(@"不允许程序内付费");
                break;
            default:
                break;
        }
        if(_handle){
            _handle(type, data);
        }
    }
    
    #pragma mark --  SKProductsRequestDelegate
    /**
     收到产品信息的回调
     
     @param request  请求的信息
     @param response 返回的产品信息
     */
    - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
        
        // 商品所在数组
        NSArray *productArr = response.products;
        if (productArr.count > 0) {
    //        SKProduct *product = nil;
            for (SKProduct *product in productArr) {
                if ([product.productIdentifier isEqualToString:self.productId]) {
    //                product = p;
                    SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
                    payment.quantity = 1;
    //                payment.applicationUsername
                    
    //                payment.applicationUsername = _orderId;
                    // 发起内购
                    [[SKPaymentQueue defaultQueue] addPayment:payment];
                    break;
                }
            }
        } else {
            [self handleActionWithType:IAPResultIDError data:nil];
        }
        
        
    //    if ([response.invalidProductIdentifiers containsObject:self.productId]) {
    //        self.callback(nil, [XSNetwork errorWithCode:-3 message:@"产品暂时不可用,请检查苹果后台"]);
    //        return;
    //    }
    //
    //    for (SKProduct* product in response.products) {
    //
    //        SKMutablePayment* payment = [SKMutablePayment paymentWithProduct:product];;
    //        payment.applicationUsername = self.orderId;
    //        [SKPaymentQueue.defaultQueue addPayment:payment];
    //    }
        
        
    }
    
    
    #pragma mark --  SKPaymentTransactionObserver
    - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
        // 获取结果
        // 验证成功与否都注销交易,否则会出现虚假凭证信息一直验证不通过,每次进程序都得输入苹果账号
        for (SKPaymentTransaction *trans in transactions) {
            switch (trans.transactionState) {
                case SKPaymentTransactionStatePurchasing:
                    NSLog(@"商品添加进列表");
                    break;
                case SKPaymentTransactionStatePurchased:
                    NSLog(@"交易完成");
                    [self completeTransaction:trans];
                    [[SKPaymentQueue defaultQueue] finishTransaction:trans];
                    break;
                case SKPaymentTransactionStateFailed:
                    NSLog(@"交易失败");
                    [self failedTransaction:trans];
                    [[SKPaymentQueue defaultQueue] finishTransaction:trans];
                    break;
                case SKPaymentTransactionStateRestored:
                    NSLog(@"已经购买过商品");
                    [[SKPaymentQueue defaultQueue] finishTransaction:trans]; //消耗型商品不用写
                    break;
                case SKPaymentTransactionStateDeferred:
                    
                    break;
                default:
                    break;
            }
        }
    }
    /**
     内购完成
     
     @param transaction 内购项目体
     */
    - (void)completeTransaction:(SKPaymentTransaction *)transaction {
        NSString *productIdentifier = transaction.payment.productIdentifier;
        NSURL *recepitURL = [[NSBundle mainBundle] appStoreReceiptURL];
        NSData *data = [NSData dataWithContentsOfURL:recepitURL];
        id  receiptString =  [data base64EncodedStringWithOptions:0];
        
        if ([productIdentifier length] > 0 && data) {
            
            if (receiptString && self.orderId.length > 0) {
                //1.保存订单号和报文到本地.
                [self saveReceiptData:@{@"receipt":receiptString,
                                        @"orderId":self.orderId ?:@""}];
                
                //2.向自己的服务器验证购买凭证.
                [self verifyPurchaseForServiceWithOrderId:self.orderId
                                                  receipt:receiptString];
            }
            
        } else {
            [self handleActionWithType:IAPResultVerFailed data:nil];
        }   
    }
    
    ///服务器验证购买凭证.
    - (void)verifyPurchaseForServiceWithOrderId:(NSString *)orderId
                                        receipt:(id)receiptString {
        if (orderId == nil && !receiptString) {
            [self handleActionWithType:IAPResultIDError data:nil];
            return;
        }
      
        //这里就是服务器校验,参数:receiptString, orderId
        [XSNetwork payWithReceipt:receiptString orderId:orderId complete:^(id object, NSError *error) {
            
            //TTOO:网络异常,服务器校验receiptString失败,就会出现掉单问题.需要保存订单号到本地,下次启动app,继续校验服务器.
            
            if (error != nil) {
                //订单校验失败.
                [self handleActionWithType:IAPResultVerFailed data:nil];
            }else{
                
                //订单校验成功,删除本地保存的订单号和苹果返回的data凭证.
                [self handleActionWithType:IAPResultSuccess data:nil];
                [self removeLocReceiptData];
            }
        }];
    }
    
    
    /**
     交易失败
     
     @param transaction  内购项目体
     */
    - (void)failedTransaction:(SKPaymentTransaction *)transaction {
        if (transaction.error.code != SKErrorPaymentCancelled) {
            [self handleActionWithType:IAPResultFailed data:nil];
        } else {
            [self handleActionWithType:IAPResultCancle data:nil];
        }
    }
    
    
    #pragma mark -- 本地保存一次支付凭证
    static NSString *const kSaveReceiptData = @"kSaveReceiptData";
    
    - (void)saveReceiptData:(NSDictionary *)receiptData {
        [[NSUserDefaults standardUserDefaults] setValue:receiptData forKey:kSaveReceiptData];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
    
    + (NSDictionary *)getReceiptData {
        return [[NSUserDefaults standardUserDefaults] valueForKey:kSaveReceiptData];
    }
    
    - (void)removeLocReceiptData {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kSaveReceiptData];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    @end
    
    

    使用例子:

     [[KDIAPManager shareIAPManager] startIAPWithOrderId:orderId productID:productId completeHandle:^(IAPResultType type, NSData *data) {
                if (type == IAPResultSuccess) {
                    [XSNetwork showHudSuccess:@"购买成功!"];
                }else if (type == IAPResultVerFailed){
                    [XSNetwork showHudSuccess:@"订单校验失败!"];
                }else{
                    [XSNetwork showHudFailure:@"购买失败!"];
                }
            }];
    

    OK! 亲测有效, 有问题可以私信我(⊙o⊙)…。
    参考博客iOS内购掉单完美解决方案-转载 Keep 的方案
    网页3剑客.iOS内购掉单问题处理方法

    相关文章

      网友评论

        本文标题:iOS内购掉单处理教程

        本文链接:https://www.haomeiwen.com/subject/jskttltx.html