ASCheckoutPayManager.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //
  2. // ASCheckoutPayManager.m
  3. // Asteria
  4. //
  5. // Created by xingyu on 2024/5/14.
  6. //
  7. #import "ASCheckoutPayManager.h"
  8. #import <KlarnaMobileSDK/KlarnaMobileSDK-Swift.h>
  9. #import "Asteria-Swift.h"
  10. #import "CartVM.h"
  11. //Asteria-Bridging-Header
  12. @interface ASCheckoutPayManager()<KlarnaEventHandler,KlarnaPaymentEventListener>
  13. @property (nonatomic, copy) NSString *payType;
  14. @property (nonatomic, strong) KlarnaPaymentView *klarna_payV;
  15. @property (nonatomic, strong) NSString *client_token;
  16. @property (nonatomic, strong) UIViewController *topVC;
  17. @end
  18. @implementation ASCheckoutPayManager
  19. static ASCheckoutPayManager *_instance = nil;
  20. + (instancetype)sharedInstance {
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. _instance = [[ASCheckoutPayManager alloc] init];
  24. });
  25. return _instance;
  26. }
  27. - (void)checkoutPayWithMethodType:(NSString *)payType param:(NSDictionary *)payParam {
  28. self.payType = payType;
  29. self.topVC = topViewController();
  30. if ([payType isEqualToString:@"paypal_express"]) {//paypal
  31. K_WEAK_SELF;
  32. [MBProgressHUD showHUDAddedTo:self.topVC.view animated:YES];
  33. [ASNetTools.shared getWithPath:Chectout_Pay_Paypal_Url param:@{} success:^(id _Nonnull json) {
  34. K_STRONG_SELF;
  35. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  36. NSDictionary *dataDic = (NSDictionary *)json;
  37. NSString *urlStr = AS_String_NotNull(dataDic[@"url"]);
  38. dispatch_async(dispatch_get_main_queue(), ^{
  39. [self tool_gotoWebPay:urlStr withVC:self.topVC];
  40. });
  41. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  42. // K_STRONG_SELF;
  43. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  44. [self.topVC.view makeToast:msg duration:2 position:CSToastPositionCenter];
  45. if (self.payFinishBlock) {
  46. self.payFinishBlock(self.payType, 0, @{@"msg":@""});
  47. }
  48. }];
  49. } else if ([payType isEqualToString:@"stripe_payments"]) {//visa
  50. double totalPay = [[payParam objectForKey:@"price"] doubleValue];
  51. ToolStripePayment *payment = [[ToolStripePayment alloc]init];
  52. NSString *price_unit_code = ASCurrencyManager.shared.currentCur;
  53. if (price_unit_code.isEmpty) {
  54. price_unit_code = @"USD";
  55. }
  56. [payment stripedidTapCheckoutButtonWithCurrentvc:[Current_normalTool topViewController] publishableKey:PayStripepublishableKey reqbaseUrl:BaseRequestrUrl(@"carts/mine/order") amount:totalPay*100 currency:price_unit_code addressModel:self.addressModel completion:nil];
  57. payment.payFinishBlock = ^(NSString *status, NSString *orderid) {
  58. int statusInt = [status intValue];
  59. if (statusInt == 0) {
  60. if (self.payFinishBlock) {
  61. self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
  62. }
  63. } else if (statusInt == 1) {
  64. //二次验证
  65. [self stripePayCreateOrderid:orderid];
  66. } else if (statusInt == 2) {
  67. // [topVC.view makeToast:@"您 已取消支付"];
  68. } else {
  69. if (self.payFinishBlock) {
  70. self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment Failed"});
  71. }
  72. }
  73. };
  74. } else if ([payType isEqualToString:@"afterpay_payment"]) {//afterpay
  75. } else if ([payType isEqualToString:@"klarna_kco"]) {//klarna
  76. K_WEAK_SELF;
  77. [MBProgressHUD showHUDAddedTo:self.topVC.view animated:YES];
  78. [ASNetTools.shared getWithPath:Chectout_Pay_Klarna_Url param:@{} success:^(id _Nonnull json) {
  79. K_STRONG_SELF;
  80. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  81. NSDictionary *dataDic = (NSDictionary *)json;
  82. self.client_token = AS_String_NotNull(dataDic[@"client_token"]);
  83. //klarna_1 初始化 如果不经历接口创建订单,永远无法更新 client_token 的订单信息
  84. [self.klarna_payV initializeWithClientToken:self.client_token returnUrl:[NSURL URLWithString:@"alipearlKlarna://"]];
  85. NSLog(@"=====%@", json);
  86. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  87. /*K_STRONG_SELF*/;
  88. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  89. [self.topVC.view makeToast:msg duration:2 position:CSToastPositionCenter];
  90. if (self.payFinishBlock) {
  91. self.payFinishBlock(self.payType, 0, @{@"msg":@""});
  92. }
  93. }];
  94. } else {
  95. //货到付款
  96. [self stripePayCreateOrderid:@""];
  97. }
  98. }
  99. #pragma mark ----- stripe支付 、 货到付款方式-----
  100. //stripe支付(二次校验后生成订单)
  101. - (void)stripePayCreateOrderid:(NSString *)payid {
  102. NSDictionary *requestParams = nil;
  103. if ([payid isEqualToString:@""]) {//货到付款方式
  104. requestParams = @{@"paymentMethod":@{@"method":self.payType}};
  105. } else {//stripe支付
  106. NSDictionary *methodDic = @{@"payment_method":payid,
  107. @"manual_authentication":@"card",
  108. @"payment_element":@(YES)};
  109. NSDictionary *param = @{@"method":self.payType, @"additional_data":methodDic};
  110. requestParams = @{@"paymentMethod":param};
  111. }
  112. [MBProgressHUD showHUDAddedTo:self.topVC.view animated:YES];
  113. K_WEAK_SELF;
  114. [ASNetTools.shared putWithPath:Chectout_PUT_sureOrder param:requestParams success:^(id _Nonnull json) {
  115. K_STRONG_SELF;
  116. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  117. NSString *orderid = (NSString *)json;
  118. if (self.payFinishBlock) {
  119. self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
  120. }
  121. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  122. K_STRONG_SELF;
  123. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  124. [self.topVC.view makeToast:msg duration:2 position:CSToastPositionCenter];
  125. if (self.payFinishBlock) {
  126. self.payFinishBlock(self.payType, 0, @{@"msg":@""});
  127. }
  128. }];
  129. }
  130. #pragma mark ----- PayPal支付 -----
  131. //paypal支付
  132. - (void)tool_gotoWebPay:(NSString *)urlStr withVC:(UIViewController *)tmpvc {
  133. XXX_BaseWebC *vc = [[XXX_BaseWebC alloc] init];
  134. vc.isPayType = YES;
  135. K_WEAK_SELF;
  136. vc.WebViewBlock = ^(NSUInteger status, id _Nonnull webData) {
  137. K_STRONG_SELF;
  138. if (status == 1) {
  139. [self requestVerifyPaypalToken:webData];
  140. } else {
  141. [tmpvc.view makeToast:[(NSDictionary *)webData objectForKey:@"title"] duration:2 position:CSToastPositionCenter];
  142. if (self.payFinishBlock) {
  143. self.payFinishBlock(self.payType, 0, @{@"msg":@""});
  144. }
  145. }
  146. };
  147. [vc xxx_dsWebLoadUrl:urlStr];
  148. vc.modalPresentationStyle = UIModalPresentationFullScreen;
  149. [tmpvc presentViewController:vc animated:YES completion:nil];
  150. }
  151. //paypal支付 Token验证
  152. - (void)requestVerifyPaypalToken:(NSDictionary *)dic {
  153. NSDictionary *params = @{@"token":[dic objectForKey:@"token"]};
  154. [MBProgressHUD showHUDAddedTo:self.topVC.view animated:YES];
  155. K_WEAK_SELF;
  156. [ASNetTools.shared getWithPath:Chectout_Verify_PayToken param:params success:^(id _Nonnull json) {
  157. K_STRONG_SELF;
  158. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  159. NSDictionary *payReturnDic = (NSDictionary *)json;
  160. NSString *orderid = [payReturnDic objectForKey:@"incrementId"];
  161. if (self.payFinishBlock) {
  162. self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
  163. }
  164. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  165. K_STRONG_SELF;
  166. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  167. if (self.payFinishBlock) {
  168. self.payFinishBlock(self.payType, 0, @{@"msg":msg});
  169. }
  170. }];
  171. // [ASNetTools.shared postWithPath:Chectout_Verify_PayToken param:params success:^(id _Nonnull json) {
  172. // K_STRONG_SELF;
  173. // [MBProgressHUD hideHUDForView:topVC.view animated:YES];
  174. //
  175. //
  176. // NSDictionary *payReturnDic = (NSDictionary *)json;
  177. // NSString *orderid = [payReturnDic objectForKey:@"id"];
  178. // if (self.payFinishBlock) {
  179. // self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
  180. // }
  181. //
  182. // } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  183. // K_STRONG_SELF;
  184. // [MBProgressHUD hideHUDForView:topVC.view animated:YES];
  185. // if (self.payFinishBlock) {
  186. // self.payFinishBlock(self.payType, 0, @{@"msg":msg});
  187. // }
  188. // }];
  189. }
  190. #pragma mark ----- klarna支付验证 -----
  191. //klarna支付验证
  192. -(void)tool_verifyOrderAutoken:(NSString *)authToken{
  193. NSDictionary *params = @{@"authorization_token":authToken};
  194. [MBProgressHUD showHUDAddedTo:self.topVC.view animated:YES];
  195. K_WEAK_SELF;
  196. [ASNetTools.shared postWithPath:Chectout_Verify_KlarnaCart param:params success:^(id _Nonnull json) {
  197. K_STRONG_SELF;
  198. NSString *is_active = [NSString stringWithFormat:@"%@", [(NSDictionary *)json objectForKey:@"is_active"]];
  199. NSLog(@"=======%@", is_active);
  200. if ([is_active isEqualToString:@"1"]) {
  201. [self tool_creatOrderAutoken:authToken];
  202. } else {
  203. //异常状态、需要返回购物车刷新
  204. if (self.payFinishBlock) {
  205. self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment anomaly"});
  206. }
  207. }
  208. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  209. K_STRONG_SELF;
  210. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  211. if (self.payFinishBlock) {
  212. self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment anomaly"});
  213. }
  214. }];
  215. }
  216. //klarna支付
  217. -(void)tool_creatOrderAutoken:(NSString *)authToken{
  218. self.client_token = authToken;
  219. NSDictionary *additional_data = @{@"authorization_token":authToken};
  220. NSDictionary *payType = @{@"method":@"klarna_pay_over_time",
  221. @"additional_data":additional_data};
  222. NSDictionary *params = @{@"paymentMethod":payType,
  223. @"cartId":self.cartId};
  224. // [MBProgressHUD showHUDAddedTo:topVC.view animated:YES];
  225. K_WEAK_SELF;
  226. [ASNetTools.shared postWithPath:Chectout_Verify_KlarnaToken param:params success:^(id _Nonnull json) {
  227. K_STRONG_SELF;
  228. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  229. NSString *orderid = (NSString *)json;
  230. NSLog(@"=======%@", orderid);
  231. if (self.payFinishBlock) {
  232. self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
  233. }
  234. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  235. K_STRONG_SELF;
  236. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  237. if (self.payFinishBlock) {
  238. self.payFinishBlock(self.payType, 0, @{@"msg":msg});
  239. }
  240. }];
  241. }
  242. #pragma mark - **************** KlarnaPaymentEventListener ****************
  243. - (void)klarnaInitializedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView { //init 成功
  244. NSLog(@"klarna---888---%@",self.client_token);
  245. //klarna_2 加载试图
  246. [paymentView loadWithJsonData:self.client_token];
  247. //klarna_3 授权支付操作 klarna_getClientTokenAurhorizePay
  248. [self.klarna_payV authorizeWithAutoFinalize:YES jsonData:self.client_token];
  249. }
  250. - (void)klarnaAuthorizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken finalizeRequired:(BOOL)finalizeRequired {
  251. NSLog(@"klarna---555---%@--approved---%d",authToken, approved);
  252. if (approved == true ){
  253. NSLog(@"klarna 授权支付成功");
  254. [self tool_verifyOrderAutoken:authToken];
  255. } else {
  256. if (self.payFinishBlock) {
  257. self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment Failed/Cancel"});
  258. }
  259. NSLog(@"klarna 授权支付失败");
  260. }
  261. }
  262. - (void)klarnaReauthorizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken {
  263. NSLog(@"klarna---666---%@ approved authToken:(NSString * _Nullable)authToken",paymentView);
  264. if (approved == true ){
  265. [self tool_creatOrderAutoken:authToken];
  266. }
  267. }
  268. #pragma mark - **************** KlarnaEventHandler ****************
  269. - (void)klarnaComponent:(id <KlarnaComponent> _Nonnull)klarnaComponent dispatchedEvent:(KlarnaProductEvent * _Nonnull)event{
  270. // NSLog(@"klarna---111---%@",event.qmui_allBindingKeys);
  271. }
  272. - (void)klarnaComponent:(id <KlarnaComponent> _Nonnull)klarnaComponent encounteredError:(KlarnaError * _Nonnull)error{
  273. // NSLog(@"klarna---222---error:%@",error);
  274. }
  275. - (void)klarnaResizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView to:(CGFloat)newHeight {
  276. // [self.klarna_payV mas_updateConstraints:^(MASConstraintMaker *make) {
  277. // make.height.mas_equalTo(newHeight);
  278. // }];
  279. }
  280. - (void)klarnaFailedInPaymentView:(KlarnaPaymentView * _Nonnull)paymentView withError:(KlarnaPaymentError * _Nonnull)error {
  281. // NSLog(@"klarna---777---%@",paymentView);
  282. }
  283. - (void)klarnaFinalizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken {
  284. }
  285. - (void)klarnaLoadedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView {
  286. // NSLog(@"klarna---333---%@",paymentView);
  287. // self.bottomPaybtn.backgroundColor = [UIColor colorWithHexString:@"#B2000F"];
  288. // self.bottomPaybtn.userInteractionEnabled = YES;
  289. }
  290. - (void)klarnaLoadedPaymentReviewWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView {
  291. // NSLog(@"klarna---444---%@",paymentView);
  292. }
  293. #pragma mark - **************** lazy ****************
  294. -(KlarnaPaymentView *)klarna_payV{
  295. if(!_klarna_payV){
  296. _klarna_payV = [[KlarnaPaymentView alloc] initWithCategory:@"pay_over_time" eventListener:self];
  297. _klarna_payV.translatesAutoresizingMaskIntoConstraints = false;
  298. }
  299. return _klarna_payV;
  300. }
  301. @end