Topwo博客
首页
博客
项目
您现在的位置是:
Topwo博客
>>
IOS
文章
【支付】Cocos2d-x IOS内购(IAP支付)
发布时间:2021-09-26
作者:Topwo
来源:原创
点击:358
### 1. 结构介绍: - IOSiAP:支付核心类,实现各种请求,直接调用API的接口。(在mm文件中会实现一些API的回调函数) - IOSiAPDelegate:代理类(抽象类),方法会在API返回结果时调用,在IOSiAP_Birdge中实现,以达到将结果传递出去的目的。 - IOSiAP_Bridge:起到桥梁作用的中间类,主要功能是调用IOAiAP中的各种请求接口,并且IOSiAPDelegate中的结果会返回到此类中(通过代理)。 - GamePayment:单例类,纯粹是为了方便使用,因为我把所有支付方式接口都统一到此类管理。(目前只有支付宝和IAP) ### 2. 支付流程: 以下是一次正常支付的流程,都是从IOSiAP_Bridge开始请求直到其收到返回结果为止。 1. 请求商品信息:(主动)调用IOSiAP_Bridge的requestProducts方法,其会调用IOSiAP的requestProducts方法,调用API接口处理请求。 2. 返回商品信息:返回在iAPProductsRequestDelegate(.mm文件中)的对应回调函数,如果成功,进入requestDidFinish,如果失败进入didFailWithEror,在这里会分别调用IOSiAP_Bridge的onRequestProductsFinsih或者onRequestProductsError函数,即通过代理将结果传递到IOSiAP_Birdge中。 3. 请求支付商品:(主动)调用IOSiAP_Birdge的requestPayment方法,其会调用IOSiAP的paymentWithProduct方法,调用API接口处理请求。 4. 返回支付结果:方式与商品信息返回一样,但是会返回在iAPTransactionObserver(.mm文件中)中的updatedTransactions中,注意,这里会接收各种支付请求相关的事件,包括后面要说的回复购买的事件。此处直接通过代理将参数传到IOSiAP_Bridge中的onPaymentEvent进行分析处理。 5. 到这里,一次完整的请求支付并回调就完成了,下面再说下回复购买的流程。 6. 请求恢复购买:(主动)调用IOSiAP_Birdge的requestRestore方法,其会调用IOSiAP的restorePayment方法,调用API接口处理请求。 7. 恢复购买结果:此时updateTransactions会受到已经购买的商品信息(返回支付结果那里),如果有多个就会返回多次,与支付结果一样会调用IOSiAP_Birdge的onPaymentEvent处理。此时我们可以处理这些信息,完成恢复操作。 8. 恢复购买完成:当上一步信息全部都返回完毕,在paymentQueueRestoreCompletedTransactionsFinsished会收到返回结果,如果上一步没有内购,即没有上一步的返回,会直接在restoreComletedTransactionsFailedWithError中返回。这两个返回都会传到IOSiAP_Birdge的onRestoreFinished中处理。 好了所有流程就这些了,看起来有点乱,不明白可以直接看代码。 ### 3. 核心代码: IOSiAP ```cpp class IOSiAP { public: IOSiAP(); ~IOSiAP(); void requestProducts(std::vector <std::string> &productIdentifiers); // 请求商品 IOSProduct *iOSProductByIdentifier(std::string &identifier); void paymentWithProduct(IOSProduct *iosProduct, int quantity = 1); // 请求支付 void restorePayment(); // 恢复购买 IOSiAPDelegate *delegate; // === internal use for object-c class === void *skProducts;// object-c SKProduct void *skTransactionObserver;// object-c TransactionObserver std::vector<IOSProduct *> iOSProducts; }; ``` IOSiAPDelegate ```cpp class IOSiAPDelegate { public: virtual ~IOSiAPDelegate() {} // for request product virtual void onRequestProductsFinish(void) = 0; virtual void onRequestProductsError(int code) = 0; // for payment event (also for restore event) virtual void onPaymentEvent(std::string &identifier, IOSiAPPaymentEvent event, int quantity) = 0; // for restore finished virtual void onRestoreFinished(bool succeed) = 0; }; ``` iAPProductsRequestDelegate ```cpp @interface iAPProductsRequestDelegate : NSObject<SKProductsRequestDelegate> @property (nonatomic, assign) IOSiAP *iosiap; @end @implementation iAPProductsRequestDelegate - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response { ...... } - (void)requestDidFinish:(SKRequest *)request { _iosiap->delegate->onRequestProductsFinish(); [request.delegate release]; [request release]; } - (void)request:(SKRequest *)request didFailWithError:(NSError *)error { _iosiap->delegate->onRequestProductsError([error code]); } @end ``` iAPTransactionObserver ```cpp @interface iAPTransactionObserver : NSObject<SKPaymentTransactionObserver> @property (nonatomic, assign) IOSiAP *iosiap; @end @implementation iAPTransactionObserver - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { ...... _iosiap->delegate->onPaymentEvent(identifier, event, transaction.payment.quantity); } - (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error { _iosiap->delegate->onRestoreFinished(false); } - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue { _iosiap->delegate->onRestoreFinished(true); } @end ``` IOSiAP_Bridge ```cpp class IOSiAP_Bridge : public IOSiAPDelegate { public: // [请求]获取商品信息 void requestProducts(std::string &identifier, iapProductCallback callback); // [请求]付款请求 void requestPayment(int quantity, iapPaymentCallback callback); // [请求]恢复购买 void requestRestore(iapRestoreCallback restoreCallback, iapRestoreFinishCallback finishCallback); public: IOSiAP_Bridge(); ~IOSiAP_Bridge(); // [回调]请求商品信息回调 virtual void onRequestProductsFinish(void); virtual void onRequestProductsError(int code); // [回调]付款结果回调(恢复流程也走这里) virtual void onPaymentEvent(std::string &identifier, IOSiAPPaymentEvent event, int quantity); // [回调]恢复购买完成回调 void onRestoreFinished(bool succeed); private: IOSiAP *iap; // IOSiAp实例 std::string _identifier; // 商品编号(获取请求) int _quantity; // 商品数量(购买请求) IOSProduct *_product; // 商品信息(返回信息) iapProductCallback _productCallback; // 外部回调(商品信息) iapPaymentCallback _paymentCallback; // 外部回调(购买结果) iapRestoreCallback _restoreCallback; // 外部回调(恢复购买); iapRestoreFinishCallback _restoreFinishCallback; // 外部回调(恢复完成) }; ``` GamePayment ```cpp class GamePayment { public: static GamePayment *getInstance(); protected: GamePayment(); // IAP内购(IOS) #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) public: // 请求商品信息 void req_iap(std::string &identifier, iapProductCallback callback); // 购买请求 void pay_iap(int quantity, iapPaymentCallback callback); // 恢复购买 void restore_iap(iapRestoreCallback restoreCallback, iapRestoreFinishCallback finishCallback); private: IOSiAP_Bridge _iap; #endif } ```
上一篇:
iOS真机测试包
下一篇:
制作苹果推送通知APNS服务器证书文件