135 lines
5.4 KiB
Objective-C
135 lines
5.4 KiB
Objective-C
//
|
|
// PayVM.m
|
|
//
|
|
|
|
#import "PayVM.h"
|
|
#import "KBNetworkManager.h"
|
|
#import "KBAPI.h"
|
|
#import "KBHUD.h"
|
|
#import "KBBizCode.h"
|
|
#import "KBPayProductModel.h"
|
|
#import <MJExtension/MJExtension.h>
|
|
@implementation PayVM
|
|
|
|
- (void)applePayReqWithParams:(NSDictionary *)params
|
|
needShow:(BOOL)needShow
|
|
completion:(KBPayCompletion)completion {
|
|
// if (needShow) { [KBHUD show]; }
|
|
[KBHUD showWithStatus:@"Please wait"];
|
|
[[KBNetworkManager shared] POST:API_VALIDATE_RECEIPT
|
|
jsonBody:params
|
|
headers:nil
|
|
autoShowBusinessError:false
|
|
completion:^(NSDictionary *json,
|
|
NSURLResponse *response,
|
|
NSError *error) {
|
|
if (needShow) { [KBHUD dismiss]; }
|
|
|
|
if (error) {
|
|
if ([error.domain isEqualToString:KBNetworkErrorDomain] &&
|
|
error.code == KBNetworkErrorBusiness) {
|
|
NSNumber *codeNum = error.userInfo[@"code"];
|
|
NSInteger bizCode = codeNum.integerValue; // 这里就是底层附带的业务 code
|
|
// 根据 bizCode 做处理,比如透传给上层 completion
|
|
if (completion) completion(bizCode, error.localizedDescription ?: KBLocalized(@"Network error"));
|
|
return;
|
|
}
|
|
|
|
// if (completion) completion(ERROR_CODE, error.localizedDescription ?: KBLocalized(@"Network error"));
|
|
return;
|
|
}
|
|
|
|
// if (completion) completion(KBBizCodeSuccess, @"JWS-ok");
|
|
if (completion) completion(KBBizCodeSuccess, nil);
|
|
|
|
}];
|
|
}
|
|
|
|
- (void)fetchInAppProductsNeedShow:(BOOL)needShow
|
|
completion:(KBPayProductsCompletion)completion {
|
|
NSDictionary *params = @{ @"type": @"in-app-purchase" };
|
|
[self fetchProductListWithPath:API_INAPP_PRODUCT_LIST
|
|
params:params
|
|
needShow:needShow
|
|
completion:completion];
|
|
}
|
|
|
|
- (void)fetchSubscriptionProductsNeedShow:(BOOL)needShow
|
|
completion:(KBPayProductsCompletion)completion {
|
|
NSDictionary *params = @{ @"type": @"subscription" };
|
|
[self fetchProductListWithPath:API_SUBSCRIPTION_PRODUCT_LIST
|
|
params:params
|
|
needShow:needShow
|
|
completion:completion];
|
|
}
|
|
|
|
#pragma mark - Helpers
|
|
|
|
- (void)fetchProductListWithPath:(NSString *)path
|
|
params:(NSDictionary *)params
|
|
needShow:(BOOL)needShow
|
|
completion:(KBPayProductsCompletion)completion {
|
|
if (needShow) { [KBHUD show]; }
|
|
[[KBNetworkManager shared] GET:path
|
|
parameters:params
|
|
headers:nil
|
|
autoShowBusinessError:NO
|
|
completion:^(NSDictionary * _Nullable json,
|
|
NSURLResponse * _Nullable response,
|
|
NSError * _Nullable error) {
|
|
if (needShow) { [KBHUD dismiss]; }
|
|
if (error) {
|
|
if (completion) {
|
|
NSInteger code = (error.code == 0 ? KBBizCodeSystemError : error.code);
|
|
completion(code, error.localizedDescription ?: KBLocalized(@"Network error"), nil);
|
|
}
|
|
return;
|
|
}
|
|
id dataObj = json[KBData] ?: json[@"data"];
|
|
if (![dataObj isKindOfClass:[NSArray class]]) {
|
|
if (completion) {
|
|
NSString *msg = [self.class extractMessageFromResponseObject:json] ?: KBLocalized(@"Invalid response");
|
|
completion(KBBizCodeSystemError, msg, nil);
|
|
}
|
|
return;
|
|
}
|
|
NSArray<KBPayProductModel *> *models = [KBPayProductModel mj_objectArrayWithKeyValuesArray:(NSArray *)dataObj];
|
|
if (completion) {
|
|
completion(KBBizCodeSuccess, nil, models ?: @[]);
|
|
}
|
|
}];
|
|
}
|
|
|
|
//+ (NSInteger)extractStatusFromResponseObject:(id)obj response:(NSURLResponse *)resp {
|
|
// // 优先从 JSON 提取 code/status/success
|
|
// if ([obj isKindOfClass:NSDictionary.class]) {
|
|
// NSDictionary *d = (NSDictionary *)obj;
|
|
// id code = d[@"code"] ?: d[@"status"] ?: d[@"retcode"];
|
|
// if ([code isKindOfClass:NSNumber.class]) {
|
|
// return [((NSNumber *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE;
|
|
// }
|
|
// if ([code isKindOfClass:NSString.class]) {
|
|
// // 常见:"0" 视为成功
|
|
// return [((NSString *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE;
|
|
// }
|
|
// id success = d[@"success"] ?: d[@"ok"];
|
|
// if ([success respondsToSelector:@selector(boolValue)]) {
|
|
// return [success boolValue] ? SUCCESS_CODE : ERROR_CODE;
|
|
// }
|
|
// }
|
|
// // 无明显字段,按 HTTP 2xx 视为成功
|
|
// NSInteger http = 0;
|
|
// if ([resp isKindOfClass:NSHTTPURLResponse.class]) { http = ((NSHTTPURLResponse *)resp).statusCode; }
|
|
// return (http >= 200 && http < 300) ? SUCCESS_CODE : ERROR_CODE;
|
|
//}
|
|
|
|
+ (NSString *)extractMessageFromResponseObject:(id)obj {
|
|
if (![obj isKindOfClass:NSDictionary.class]) return nil;
|
|
NSDictionary *d = (NSDictionary *)obj;
|
|
NSString *msg = d[@"message"] ?: d[@"msg"] ?: d[@"error"];
|
|
if (![msg isKindOfClass:NSString.class]) msg = nil;
|
|
return msg;
|
|
}
|
|
|
|
@end
|