Files
yinmeng-ios/xplan-ios/Base/Tool/IAPHelper/XPIAPHelper.m
2021-10-25 12:12:23 +08:00

98 lines
3.4 KiB
Objective-C

//
// XPIAPHelper.m
// xplan-ios
//
// Created by 冯硕 on 2021/9/26.
//
#import "XPIAPHelper.h"
#import <IAPHelper/IAPHelper.h>
#import <IAPShare.h>
@implementation XPIAPHelper
+ (instancetype)shareHelper {
static dispatch_once_t onceToken;
static XPIAPHelper * helper;
dispatch_once(&onceToken, ^{
helper = [[XPIAPHelper alloc] init];
});
return helper;
}
- (void)buyAppProductWithAppProductId:(NSString *)appProductId {
NSSet* dataSet = [[NSSet alloc] initWithObjects:appProductId, nil];
[IAPShare sharedHelper].iap = [[IAPHelper alloc] initWithProductIdentifiers:dataSet];
[[IAPShare sharedHelper].iap requestProductsWithCompletion:^(SKProductsRequest *request, SKProductsResponse *response) {
if (response != nil && response.products.count) {
///有这个内购的产品
} else if (!response.products.count) {
///请求内购产品失败,无产品数量
#warning to do 此处埋点一个 没有内购产品
if (self.delegate && [self.delegate respondsToSelector:@selector(rechargeProcessStatus:)]) {
[self.delegate rechargeProcessStatus:PaymentStatus_Failed];
}
return;
} else if (!response) {
#warning to do 此处埋点一个 请求内购没有响应
///请求内购产品失败,内购无响应
if (self.delegate && [self.delegate respondsToSelector:@selector(rechargeProcessStatus:)]) {
[self.delegate rechargeProcessStatus:PaymentStatus_Failed];
}
return;
} else {
#warning to do 此处埋点一个 异常 发起付款失败
///异常 发起付款失败
if (self.delegate && [self.delegate respondsToSelector:@selector(rechargeProcessStatus:)]) {
[self.delegate rechargeProcessStatus:PaymentStatus_Failed];
}
return;
}
if (response.products.firstObject) {
[[IAPShare sharedHelper].iap buyProduct:response.products.firstObject onCompletion:^(SKPaymentTransaction *transcation) {
NSLog(@"%@",transcation.error.description);
switch(transcation.transactionState) {
case SKPaymentTransactionStatePurchased: {
NSLog(@"付款完成状态, 要做出相关的处理");
[[SKPaymentQueue defaultQueue] finishTransaction:transcation];
if (self.delegate && [self.delegate respondsToSelector:@selector(rechargeProcessStatus:)]) {
[self.delegate rechargeProcessStatus:PaymentStatus_Purchased];
}
if (self.delegate && [self.delegate respondsToSelector:@selector(rechargeSuccess:)]) {
[self.delegate rechargeSuccess:transcation.transactionIdentifier];
}
//同步返回购买成功后,需要请求服务器二次校验
}
break;
case SKPaymentTransactionStateFailed: {
[[SKPaymentQueue defaultQueue] finishTransaction:transcation];
if (self.delegate && [self.delegate respondsToSelector:@selector(rechargeProcessStatus:)]) {
[self.delegate rechargeProcessStatus:PaymentStatus_Failed];
}
}
break;
case SKPaymentTransactionStatePurchasing: {
NSLog(@"正在购买中");
if (self.delegate && [self.delegate respondsToSelector:@selector(rechargeProcessStatus:)]) {
[self.delegate rechargeProcessStatus:PaymentStatus_Purchasing];
}
}
break;
default: {
[[SKPaymentQueue defaultQueue] finishTransaction:transcation];
if (self.delegate && [self.delegate respondsToSelector:@selector(rechargeProcessStatus:)]) {
[self.delegate rechargeProcessStatus:PaymentStatus_Deferred];
}
NSLog(@"其它");
}
}
}];
}
}];
}
@end