123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- //
- // ASCheckoutPayManager.m
- // Asteria
- //
- // Created by xingyu on 2024/5/14.
- //
- #import "ASCheckoutPayManager.h"
- #import <KlarnaMobileSDK/KlarnaMobileSDK-Swift.h>
- #import "Asteria-Swift.h"
- #import "CartVM.h"
- //Asteria-Bridging-Header
- @interface ASCheckoutPayManager()<KlarnaEventHandler,KlarnaPaymentEventListener>
- @property (nonatomic, copy) NSString *payType;
- @property (nonatomic, strong) KlarnaPaymentView *klarna_payV;
- @property (nonatomic, strong) NSString *client_token;
- @property (nonatomic, strong) UIViewController *topVC;
- @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;
-
- self.topVC = topViewController();
-
- if ([payType isEqualToString:@"paypal_express"]) {//paypal
- K_WEAK_SELF;
- [MBProgressHUD showHUDAddedTo:self.topVC.view animated:YES];
- [ASNetTools.shared getWithPath:Chectout_Pay_Paypal_Url param:@{} success:^(id _Nonnull json) {
- K_STRONG_SELF;
- [MBProgressHUD hideHUDForView:self.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:self.topVC];
- });
- } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
- // K_STRONG_SELF;
- [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
- [self.topVC.view makeToast:msg duration:2 position:CSToastPositionCenter];
- if (self.payFinishBlock) {
- self.payFinishBlock(self.payType, 0, @{@"msg":@""});
- }
- }];
-
- } 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:self.topVC.view animated:YES];
- [ASNetTools.shared getWithPath:Chectout_Pay_Klarna_Url param:@{} success:^(id _Nonnull json) {
- K_STRONG_SELF;
- [MBProgressHUD hideHUDForView:self.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:@"asteriaKlarna://"]];
-
- NSLog(@"=====%@", json);
- } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
- /*K_STRONG_SELF*/;
- [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
- [self.topVC.view makeToast:msg duration:2 position:CSToastPositionCenter];
-
- if (self.payFinishBlock) {
- self.payFinishBlock(self.payType, 0, @{@"msg":@""});
- }
- }];
- } else {
- //货到付款
- [self stripePayCreateOrderid:@""];
- }
- }
- #pragma mark ----- stripe支付 、 货到付款方式-----
- //stripe支付(二次校验后生成订单)
- - (void)stripePayCreateOrderid:(NSString *)payid {
-
- 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:self.topVC.view animated:YES];
- K_WEAK_SELF;
- [ASNetTools.shared putWithPath:Chectout_PUT_sureOrder param:requestParams success:^(id _Nonnull json) {
- K_STRONG_SELF;
- [MBProgressHUD hideHUDForView:self.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:self.topVC.view animated:YES];
- [self.topVC.view makeToast:msg duration:2 position:CSToastPositionCenter];
- if (self.payFinishBlock) {
- self.payFinishBlock(self.payType, 0, @{@"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];
- if (self.payFinishBlock) {
- self.payFinishBlock(self.payType, 0, @{@"msg":@""});
- }
- }
- };
- [vc xxx_dsWebLoadUrl:urlStr];
- vc.modalPresentationStyle = UIModalPresentationFullScreen;
- [tmpvc presentViewController:vc animated:YES completion:nil];
- }
- //paypal支付 Token验证
- - (void)requestVerifyPaypalToken:(NSDictionary *)dic {
-
- NSDictionary *params = @{@"token":[dic objectForKey:@"token"]};
- [MBProgressHUD showHUDAddedTo:self.topVC.view animated:YES];
- K_WEAK_SELF;
- [ASNetTools.shared getWithPath:Chectout_Verify_PayToken param:params success:^(id _Nonnull json) {
- K_STRONG_SELF;
- [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
-
-
- NSDictionary *payReturnDic = (NSDictionary *)json;
-
- NSString *orderid = [payReturnDic objectForKey:@"id"];
- NSString *orderNum = [payReturnDic objectForKey:@"incrementId"];
- if (self.payFinishBlock) {
- self.payFinishBlock(self.payType, 1, @{@"orderid":orderid, @"orderNum":orderNum});
- }
- } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
- K_STRONG_SELF;
- [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
- if (self.payFinishBlock) {
- self.payFinishBlock(self.payType, 0, @{@"msg":msg});
- }
- }];
-
-
- // [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{
-
- NSDictionary *params = @{@"authorization_token":authToken};
-
- [MBProgressHUD showHUDAddedTo:self.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:self.topVC.view animated:YES];
-
- if (self.payFinishBlock) {
- self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment anomaly"});
- }
- }];
- }
- //klarna支付
- -(void)tool_creatOrderAutoken:(NSString *)authToken{
- 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:self.topVC.view animated:YES];
-
- NSString *orderid = (NSString *)json;
- // NSString *orderNum = [payReturnDic objectForKey:@"incrementId"];
- if (self.payFinishBlock) {
- self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
- }
- NSLog(@"=======%@", orderid);
- if (self.payFinishBlock) {
- self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
- }
-
- } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
- K_STRONG_SELF;
- [MBProgressHUD hideHUDForView:self.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/Cancel"});
- }
- 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 <KlarnaComponent> _Nonnull)klarnaComponent dispatchedEvent:(KlarnaProductEvent * _Nonnull)event{
- // NSLog(@"klarna---111---%@",event.qmui_allBindingKeys);
- }
- - (void)klarnaComponent:(id <KlarnaComponent> _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
|