// // ASCheckoutPayManager.m // Asteria // // Created by xingyu on 2024/5/14. // #import "ASCheckoutPayManager.h" #import #import "Asteria-Swift.h" #import "CartVM.h" //Asteria-Bridging-Header @interface ASCheckoutPayManager() @property (nonatomic, copy) NSString *payType; @property (nonatomic, strong) KlarnaPaymentView *klarna_payV; @property (nonatomic, strong) NSString *client_token; @end @implementation ASCheckoutPayManager static ASCheckoutPayManager *_instance = nil; + (instancetype)sharedInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[ASCheckoutPayManager alloc] init]; }); return _instance; } - (void)checkoutPayWithMethodType:(NSString *)payType param:(NSDictionary *)payParam { self.payType = payType; UIViewController *topVC = topViewController(); if ([payType isEqualToString:@"paypal_express"]) {//paypal K_WEAK_SELF; [MBProgressHUD showHUDAddedTo:topVC.view animated:YES]; [ASNetTools.shared getWithPath:Chectout_Pay_Paypal_Url param:@{} success:^(id _Nonnull json) { K_STRONG_SELF; [MBProgressHUD hideHUDForView:topVC.view animated:YES]; NSDictionary *dataDic = (NSDictionary *)json; NSString *urlStr = AS_String_NotNull(dataDic[@"url"]); dispatch_async(dispatch_get_main_queue(), ^{ [self tool_gotoWebPay:urlStr withVC:topVC]; }); } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) { // K_STRONG_SELF; [MBProgressHUD hideHUDForView:topVC.view animated:YES]; [topVC.view makeToast:msg duration:2 position:CSToastPositionCenter]; }]; } else if ([payType isEqualToString:@"stripe_payments"]) {//visa double totalPay = [[payParam objectForKey:@"price"] doubleValue]; ToolStripePayment *payment = [[ToolStripePayment alloc]init]; NSString *price_unit_code = ASCurrencyManager.shared.currentCur; if (price_unit_code.isEmpty) { price_unit_code = @"USD"; } [payment stripedidTapCheckoutButtonWithCurrentvc:[Current_normalTool topViewController] publishableKey:PayStripepublishableKey reqbaseUrl:BaseRequestrUrl(@"carts/mine/order") amount:totalPay*100 currency:price_unit_code addressModel:self.addressModel completion:nil]; payment.payFinishBlock = ^(NSString *status, NSString *orderid) { int statusInt = [status intValue]; if (statusInt == 0) { if (self.payFinishBlock) { self.payFinishBlock(self.payType, 1, @{@"orderid":orderid}); } } else if (statusInt == 1) { //二次验证 [self stripePayCreateOrderid:orderid]; } else if (statusInt == 2) { // [topVC.view makeToast:@"您 已取消支付"]; } else { if (self.payFinishBlock) { self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment Failed"}); } } }; } else if ([payType isEqualToString:@"afterpay_payment"]) {//afterpay } else if ([payType isEqualToString:@"klarna_kco"]) {//klarna K_WEAK_SELF; [MBProgressHUD showHUDAddedTo:topVC.view animated:YES]; [ASNetTools.shared getWithPath:Chectout_Pay_Klarna_Url param:@{} success:^(id _Nonnull json) { K_STRONG_SELF; [MBProgressHUD hideHUDForView:topVC.view animated:YES]; NSDictionary *dataDic = (NSDictionary *)json; self.client_token = AS_String_NotNull(dataDic[@"client_token"]); //klarna_1 初始化 如果不经历接口创建订单,永远无法更新 client_token 的订单信息 [self.klarna_payV initializeWithClientToken:self.client_token returnUrl:[NSURL URLWithString:@"alipearlKlarna://"]]; NSLog(@"=====%@", json); } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) { /*K_STRONG_SELF*/; [MBProgressHUD hideHUDForView:topVC.view animated:YES]; }]; } else { //货到付款 [self stripePayCreateOrderid:@""]; } } #pragma mark ----- stripe支付 、 货到付款方式----- //stripe支付(二次校验后生成订单) - (void)stripePayCreateOrderid:(NSString *)payid { UIViewController *topVC = topViewController(); NSDictionary *requestParams = nil; if ([payid isEqualToString:@""]) {//货到付款方式 requestParams = @{@"paymentMethod":@{@"method":self.payType}}; } else {//stripe支付 NSDictionary *methodDic = @{@"payment_method":payid, @"manual_authentication":@"card", @"payment_element":@(YES)}; NSDictionary *param = @{@"method":self.payType, @"additional_data":methodDic}; requestParams = @{@"paymentMethod":param}; } [MBProgressHUD showHUDAddedTo:topVC.view animated:YES]; K_WEAK_SELF; [ASNetTools.shared putWithPath:Chectout_PUT_sureOrder param:requestParams success:^(id _Nonnull json) { K_STRONG_SELF; [MBProgressHUD hideHUDForView:topVC.view animated:YES]; NSString *orderid = (NSString *)json; if (self.payFinishBlock) { self.payFinishBlock(self.payType, 1, @{@"orderid":orderid}); } } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) { K_STRONG_SELF; [MBProgressHUD hideHUDForView:topVC.view animated:YES]; if (self.payFinishBlock) { self.payFinishBlock(self.payType, 0, @{@"msg":msg}); } }]; } #pragma mark ----- PayPal支付 ----- //paypal支付 - (void)tool_gotoWebPay:(NSString *)urlStr withVC:(UIViewController *)tmpvc { XXX_BaseWebC *vc = [[XXX_BaseWebC alloc] init]; vc.isPayType = YES; K_WEAK_SELF; vc.WebViewBlock = ^(NSUInteger status, id _Nonnull webData) { K_STRONG_SELF; if (status == 1) { [self requestVerifyPaypalToken:webData]; } else { [tmpvc.view makeToast:[(NSDictionary *)webData objectForKey:@"title"] duration:2 position:CSToastPositionCenter]; } }; [vc xxx_dsWebLoadUrl:urlStr]; vc.modalPresentationStyle = UIModalPresentationFullScreen; [tmpvc presentViewController:vc animated:YES completion:nil]; } //paypal支付 Token验证 - (void)requestVerifyPaypalToken:(NSDictionary *)dic { UIViewController *topVC = topViewController(); NSDictionary *params = @{@"token":[dic objectForKey:@"token"]}; [MBProgressHUD showHUDAddedTo:topVC.view animated:YES]; K_WEAK_SELF; [ASNetTools.shared postWithPath:Chectout_Verify_PayToken param:params success:^(id _Nonnull json) { K_STRONG_SELF; [MBProgressHUD hideHUDForView:topVC.view animated:YES]; NSDictionary *payReturnDic = (NSDictionary *)json; NSString *orderid = [payReturnDic objectForKey:@"id"]; if (self.payFinishBlock) { self.payFinishBlock(self.payType, 1, @{@"orderid":orderid}); } } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) { K_STRONG_SELF; [MBProgressHUD hideHUDForView:topVC.view animated:YES]; if (self.payFinishBlock) { self.payFinishBlock(self.payType, 0, @{@"msg":msg}); } }]; } #pragma mark ----- klarna支付验证 ----- //klarna支付验证 -(void)tool_verifyOrderAutoken:(NSString *)authToken{ UIViewController *topVC = topViewController(); NSDictionary *params = @{@"authorization_token":authToken}; [MBProgressHUD showHUDAddedTo:topVC.view animated:YES]; K_WEAK_SELF; [ASNetTools.shared postWithPath:Chectout_Verify_KlarnaCart param:params success:^(id _Nonnull json) { K_STRONG_SELF; NSString *is_active = [NSString stringWithFormat:@"%@", [(NSDictionary *)json objectForKey:@"is_active"]]; NSLog(@"=======%@", is_active); if ([is_active isEqualToString:@"1"]) { [self tool_creatOrderAutoken:authToken]; } else { //异常状态、需要返回购物车刷新 if (self.payFinishBlock) { self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment anomaly"}); } } } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) { K_STRONG_SELF; [MBProgressHUD hideHUDForView:topVC.view animated:YES]; if (self.payFinishBlock) { self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment anomaly"}); } }]; } //klarna支付 -(void)tool_creatOrderAutoken:(NSString *)authToken{ UIViewController *topVC = topViewController(); self.client_token = authToken; NSDictionary *additional_data = @{@"authorization_token":authToken}; NSDictionary *payType = @{@"method":@"klarna_pay_over_time", @"additional_data":additional_data}; NSDictionary *params = @{@"paymentMethod":payType, @"cartId":self.cartId}; // [MBProgressHUD showHUDAddedTo:topVC.view animated:YES]; K_WEAK_SELF; [ASNetTools.shared postWithPath:Chectout_Verify_KlarnaToken param:params success:^(id _Nonnull json) { K_STRONG_SELF; [MBProgressHUD hideHUDForView:topVC.view animated:YES]; NSString *orderid = (NSString *)json; NSLog(@"=======%@", orderid); if (self.payFinishBlock) { self.payFinishBlock(self.payType, 1, @{@"orderid":orderid}); } } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) { K_STRONG_SELF; [MBProgressHUD hideHUDForView:topVC.view animated:YES]; if (self.payFinishBlock) { self.payFinishBlock(self.payType, 0, @{@"msg":msg}); } }]; } #pragma mark - **************** KlarnaPaymentEventListener **************** - (void)klarnaInitializedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView { //init 成功 NSLog(@"klarna---888---%@",self.client_token); //klarna_2 加载试图 [paymentView loadWithJsonData:self.client_token]; //klarna_3 授权支付操作 klarna_getClientTokenAurhorizePay [self.klarna_payV authorizeWithAutoFinalize:YES jsonData:self.client_token]; } - (void)klarnaAuthorizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken finalizeRequired:(BOOL)finalizeRequired { NSLog(@"klarna---555---%@--approved---%d",authToken, approved); if (approved == true ){ NSLog(@"klarna 授权支付成功"); [self tool_verifyOrderAutoken:authToken]; } else { if (self.payFinishBlock) { self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment Failed"}); } NSLog(@"klarna 授权支付失败"); } } - (void)klarnaReauthorizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken { NSLog(@"klarna---666---%@ approved authToken:(NSString * _Nullable)authToken",paymentView); if (approved == true ){ [self tool_creatOrderAutoken:authToken]; } } #pragma mark - **************** KlarnaEventHandler **************** - (void)klarnaComponent:(id _Nonnull)klarnaComponent dispatchedEvent:(KlarnaProductEvent * _Nonnull)event{ // NSLog(@"klarna---111---%@",event.qmui_allBindingKeys); } - (void)klarnaComponent:(id _Nonnull)klarnaComponent encounteredError:(KlarnaError * _Nonnull)error{ // NSLog(@"klarna---222---error:%@",error); } - (void)klarnaResizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView to:(CGFloat)newHeight { // [self.klarna_payV mas_updateConstraints:^(MASConstraintMaker *make) { // make.height.mas_equalTo(newHeight); // }]; } - (void)klarnaFailedInPaymentView:(KlarnaPaymentView * _Nonnull)paymentView withError:(KlarnaPaymentError * _Nonnull)error { // NSLog(@"klarna---777---%@",paymentView); } - (void)klarnaFinalizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken { } - (void)klarnaLoadedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView { // NSLog(@"klarna---333---%@",paymentView); // self.bottomPaybtn.backgroundColor = [UIColor colorWithHexString:@"#B2000F"]; // self.bottomPaybtn.userInteractionEnabled = YES; } - (void)klarnaLoadedPaymentReviewWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView { // NSLog(@"klarna---444---%@",paymentView); } #pragma mark - **************** lazy **************** -(KlarnaPaymentView *)klarna_payV{ if(!_klarna_payV){ _klarna_payV = [[KlarnaPaymentView alloc] initWithCategory:@"pay_over_time" eventListener:self]; _klarna_payV.translatesAutoresizingMaskIntoConstraints = false; } return _klarna_payV; } @end