加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【支付】Cocos2d-x IOS内购(IAP支付)

发布时间:2020-12-14 17:13:52 所属栏目:百科 来源:网络整理
导读:【说明】 游戏开发中,集成支付是一个重要的环节,AppStore审核指南规定,App内虚拟物品必须使用IAP支付。这篇文章主要记录我在集成IAP的过程,我参考了泰然网的一篇文章,对其进行了封装和扩展,并对结构和使用流程进行了简单的介绍,仅供自己学习使用。 关


【说明】

游戏开发中,集成支付是一个重要的环节,AppStore审核指南规定,App内虚拟物品必须使用IAP支付。这篇文章主要记录我在集成IAP的过程,我参考了泰然网的一篇文章,对其进行了封装和扩展,并对结构和使用流程进行了简单的介绍,仅供自己学习使用。

关于iTunes Connect上的商品配置,可以参考泰然网的文章,这里只对代码进行讨论。

更新:加入内购恢复接口,一键恢复购买过的非消耗型商品。(苹果规定费消耗型商品必须要有恢复购买)

【参考】

泰然网:http://www.tairan.com/archives/5515

完整代码:http://download.csdn.NET/detail/ldpjay/8949991

【封装】

1. 结构介绍:

结构和核心代码还是参照泰然网的那篇文章,我主要是在其基础上封装做了一些封装和强化,更加方便使用。

>IOSiAP:支付核心类,实现各种请求,直接调用API的接口。(在mm文件中会实现一些API的回调函数)

IOSiAPDelegate:代理类(抽象类),方法会在API返回结果时调用,在IOSiAP_Birdge中实现,以达到将结果传递出去的目的。

>IOSiAP_Bridge:起到桥梁作用的中间类,主要功能是调用IOAiAP中的各种请求接口,并且IOSiAPDelegate中的结果会返回到此类中(通过代理)。

GamePayment:单例类,纯粹是为了方便使用,因为我把所有支付方式接口都统一到此类管理。(目前只有支付宝和IAP)

2. 支付流程:

如果完全按照我使用的流程说,会比较纠结,因为封装层数太多容易混淆,这里抛开单例那一层,直接从IOSiAP_Birdge这层开始。以下是一次正常支付的流程,都是从IOSiAP_Bridge开始请求直到其收到返回结果为止。

请求商品信息:(主动)调用IOSiAP_Bridge的requestProducts方法,其会调用IOSiAP的requestProducts方法,调用API接口处理请求。

返回商品信息:返回在iAPProductsRequestDelegate(.mm文件中)的对应回调函数,如果成功,进入requestDidFinish,如果失败进入didFailWithEror,在这里会分别调用IOSiAP_Bridge的onRequestProductsFinsih或者onRequestProductsError函数,即通过代理将结果传递到IOSiAP_Birdge中。

请求支付商品:(主动)调用IOSiAP_Birdge的requestPayment方法,其会调用IOSiAP的paymentWithProduct方法,调用API接口处理请求。

返回支付结果:方式与商品信息返回一样,但是会返回在iAPTransactionObserver(.mm文件中)中的updatedTransactions中,注意,这里会接收各种支付请求相关的事件,包括后面要说的回复购买的事件。此处直接通过代理将参数传到IOSiAP_Bridge中的onPaymentEvent进行分析处理。

到这里,一次完整的请求支付并回调就完成了,下面再说下回复购买的流程。

请求恢复购买:(主动)调用IOSiAP_Birdge的requestRestore方法,其会调用IOSiAP的restorePayment方法,调用API接口处理请求。

恢复购买结果:此时updateTransactions会受到已经购买的商品信息(返回支付结果那里),如果有多个就会返回多次,与支付结果一样会调用IOSiAP_Birdge的onPaymentEvent处理。此时我们可以处理这些信息,完成恢复操作。

恢复购买完成:当上一步信息全部都返回完毕,在paymentQueueRestoreCompletedTransactionsFinsished会收到返回结果,如果上一步没有内购,即没有上一步的返回,会直接在restoreComletedTransactionsFailedWithError中返回。这两个返回都会传到IOSiAP_Birdge的onRestoreFinished中处理。

好了所有流程就这些了,看起来有点乱,不明白可以直接看代码,下面有完整版下载地址。

3. 核心代码:

这里只贴出了最核心的部分,完整版见文章后面下载地址。

>IOSiAP

[cpp] view plain copy
  1. classIOSiAP
  2. {
  3. public:
  4. IOSiAP();
  5. ~IOSiAP();
  6. voidrequestProducts(std::vector<std::string>&productIdentifiers);//请求商品
  7. IOSProduct*iOSProductByIdentifier(std::string&identifier);
  8. voidpaymentWithProduct(IOSProduct*iosProduct,intquantity=1);//请求支付
  9. voidrestorePayment();//恢复购买
  10. IOSiAPDelegate*delegate;
  11. //===internaluseforobject-cclass===
  12. void*skProducts;//object-cSKProduct
  13. void*skTransactionObserver;//object-cTransactionObserver
  14. std::vector<IOSProduct*>iOSProducts;
  15. };
> IOSiAPDelegate

copy

    classIOSiAPDelegate
  1. virtual~IOSiAPDelegate(){}
  2. //forrequestproduct
  3. virtualvoidonRequestProductsFinish(void)=0;
  4. voidonRequestProductsError(intcode)=0;
  5. //forpaymentevent(alsoforrestoreevent)
  6. voidonPaymentEvent(std::string&identifier,IOSiAPPaymentEventevent,87); background-color:inherit; font-weight:bold">intquantity)=0;
  7. //forrestorefinished
  8. voidonRestoreFinished(boolsucceed)=0;
  9. >>iAPProductsRequestDelegate

    copy

      @interfaceiAPProductsRequestDelegate:NSObject<SKProductsRequestDelegate>
    1. @property(nonatomic,assign)IOSiAP*iosiap;
    2. @end
    3. @implementationiAPProductsRequestDelegate
    4. -(void)productsRequest:(SKProductsRequest*)request
    5. didReceiveResponse:(SKProductsResponse*)response
    6. {
    7. ......
    8. }
    9. void)requestDidFinish:(SKRequest*)request
    10. _iosiap->delegate->onRequestProductsFinish();
    11. [request.delegaterelease];
    12. [requestrelease];
    13. }
    14. -(void)request:(SKRequest*)requestdidFailWithError:(NSError*)error
    15. _iosiap->delegate->onRequestProductsError([errorcode]);
    16. @end
    >iAPTransactionObserver

    copy

      @interfaceiAPTransactionObserver:NSObject<SKPaymentTransactionObserver>
    1. @implementationiAPTransactionObserver
    2. void)paymentQueue:(SKPaymentQueue*)queueupdatedTransactions:(NSArray*)transactions
    3. ......
    4. _iosiap->delegate->onPaymentEvent(identifier,event,transaction.payment.quantity);
    5. void)paymentQueue:(SKPaymentQueue*)queuerestoreCompletedTransactionsFailedWithError:(NSError*)error
    6. _iosiap->delegate->onRestoreFinished(false);
    7. void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue*)queue
    8. _iosiap->delegate->onRestoreFinished(true);
    9. >IOSiAP_Bridge

      copy

        classIOSiAP_Bridge:publicIOSiAPDelegate
      1. //[请求]获取商品信息
      2. voidrequestProducts(std::string&identifier,iapProductCallbackcallback);
      3. //[请求]付款请求
      4. voidrequestPayment(intquantity,iapPaymentCallbackcallback);
      5. //[请求]恢复购买
      6. voidrequestRestore(iapRestoreCallbackrestoreCallback,iapRestoreFinishCallbackfinishCallback);
      7. IOSiAP_Bridge();
      8. ~IOSiAP_Bridge();
      9. //[回调]请求商品信息回调
      10. void);
      11. intcode);
      12. //[回调]付款结果回调(恢复流程也走这里)
      13. intquantity);
      14. //[回调]恢复购买完成回调
      15. boolsucceed);
      16. private:
      17. IOSiAP*iap;//IOSiAp实例
      18. std::string_identifier;//商品编号(获取请求)
      19. int_quantity;//商品数量(购买请求)
      20. IOSProduct*_product;//商品信息(返回信息)
      21. iapProductCallback_productCallback;//外部回调(商品信息)
      22. iapPaymentCallback_paymentCallback;//外部回调(购买结果)
      23. iapRestoreCallback_restoreCallback;//外部回调(恢复购买);
      24. iapRestoreFinishCallback_restoreFinishCallback;//外部回调(恢复完成)
      25. >GamePayment

        copy
        1. classGamePayment
        2. staticGamePayment*getInstance();
        3. protected:
        4. GamePayment();
        5. //IAP内购(IOS)
        6. #if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
        7. //请求商品信息
        8. voidreq_iap(std::string&identifier,0); background-color:inherit">//购买请求
        9. voidpay_iap(//恢复购买
        10. voidrestore_iap(iapRestoreCallbackrestoreCallback,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> IOSiAP_Bridge_iap;
        11. #endif
        1. }

        (编辑:李大同)

        【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读