ASCheckoutPayManager.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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:@"asteriaKlarna://"]];
  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:@"id"];
  161. NSString *orderNum = [payReturnDic objectForKey:@"incrementId"];
  162. if (self.payFinishBlock) {
  163. self.payFinishBlock(self.payType, 1, @{@"orderid":orderid, @"orderNum":orderNum});
  164. }
  165. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  166. K_STRONG_SELF;
  167. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  168. if (self.payFinishBlock) {
  169. self.payFinishBlock(self.payType, 0, @{@"msg":msg});
  170. }
  171. }];
  172. // [ASNetTools.shared postWithPath:Chectout_Verify_PayToken param:params success:^(id _Nonnull json) {
  173. // K_STRONG_SELF;
  174. // [MBProgressHUD hideHUDForView:topVC.view animated:YES];
  175. //
  176. //
  177. // NSDictionary *payReturnDic = (NSDictionary *)json;
  178. // NSString *orderid = [payReturnDic objectForKey:@"id"];
  179. // if (self.payFinishBlock) {
  180. // self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
  181. // }
  182. //
  183. // } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  184. // K_STRONG_SELF;
  185. // [MBProgressHUD hideHUDForView:topVC.view animated:YES];
  186. // if (self.payFinishBlock) {
  187. // self.payFinishBlock(self.payType, 0, @{@"msg":msg});
  188. // }
  189. // }];
  190. }
  191. #pragma mark ----- klarna支付验证 -----
  192. //klarna支付验证
  193. -(void)tool_verifyOrderAutoken:(NSString *)authToken{
  194. NSDictionary *params = @{@"authorization_token":authToken};
  195. [MBProgressHUD showHUDAddedTo:self.topVC.view animated:YES];
  196. K_WEAK_SELF;
  197. [ASNetTools.shared postWithPath:Chectout_Verify_KlarnaCart param:params success:^(id _Nonnull json) {
  198. K_STRONG_SELF;
  199. NSString *is_active = [NSString stringWithFormat:@"%@", [(NSDictionary *)json objectForKey:@"is_active"]];
  200. NSLog(@"=======%@", is_active);
  201. if ([is_active isEqualToString:@"1"]) {
  202. [self tool_creatOrderAutoken:authToken];
  203. } else {
  204. //异常状态、需要返回购物车刷新
  205. if (self.payFinishBlock) {
  206. self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment anomaly"});
  207. }
  208. }
  209. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  210. K_STRONG_SELF;
  211. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  212. if (self.payFinishBlock) {
  213. self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment anomaly"});
  214. }
  215. }];
  216. }
  217. //klarna支付
  218. -(void)tool_creatOrderAutoken:(NSString *)authToken{
  219. self.client_token = authToken;
  220. NSDictionary *additional_data = @{@"authorization_token":authToken};
  221. NSDictionary *payType = @{@"method":@"klarna_pay_over_time",
  222. @"additional_data":additional_data};
  223. NSDictionary *params = @{@"paymentMethod":payType,
  224. @"cartId":self.cartId};
  225. // [MBProgressHUD showHUDAddedTo:topVC.view animated:YES];
  226. K_WEAK_SELF;
  227. [ASNetTools.shared postWithPath:Chectout_Verify_KlarnaToken param:params success:^(id _Nonnull json) {
  228. K_STRONG_SELF;
  229. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  230. NSString *orderid = (NSString *)json;
  231. // NSString *orderNum = [payReturnDic objectForKey:@"incrementId"];
  232. if (self.payFinishBlock) {
  233. self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
  234. }
  235. NSLog(@"=======%@", orderid);
  236. if (self.payFinishBlock) {
  237. self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
  238. }
  239. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  240. K_STRONG_SELF;
  241. [MBProgressHUD hideHUDForView:self.topVC.view animated:YES];
  242. if (self.payFinishBlock) {
  243. self.payFinishBlock(self.payType, 0, @{@"msg":msg});
  244. }
  245. }];
  246. }
  247. #pragma mark - **************** KlarnaPaymentEventListener ****************
  248. - (void)klarnaInitializedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView { //init 成功
  249. NSLog(@"klarna---888---%@",self.client_token);
  250. //klarna_2 加载试图
  251. [paymentView loadWithJsonData:self.client_token];
  252. //klarna_3 授权支付操作 klarna_getClientTokenAurhorizePay
  253. [self.klarna_payV authorizeWithAutoFinalize:YES jsonData:self.client_token];
  254. }
  255. - (void)klarnaAuthorizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken finalizeRequired:(BOOL)finalizeRequired {
  256. NSLog(@"klarna---555---%@--approved---%d",authToken, approved);
  257. if (approved == true ){
  258. NSLog(@"klarna 授权支付成功");
  259. [self tool_verifyOrderAutoken:authToken];
  260. } else {
  261. if (self.payFinishBlock) {
  262. self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment Failed/Cancel"});
  263. }
  264. NSLog(@"klarna 授权支付失败");
  265. }
  266. }
  267. - (void)klarnaReauthorizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken {
  268. NSLog(@"klarna---666---%@ approved authToken:(NSString * _Nullable)authToken",paymentView);
  269. if (approved == true ){
  270. [self tool_creatOrderAutoken:authToken];
  271. }
  272. }
  273. #pragma mark - **************** KlarnaEventHandler ****************
  274. - (void)klarnaComponent:(id <KlarnaComponent> _Nonnull)klarnaComponent dispatchedEvent:(KlarnaProductEvent * _Nonnull)event{
  275. // NSLog(@"klarna---111---%@",event.qmui_allBindingKeys);
  276. }
  277. - (void)klarnaComponent:(id <KlarnaComponent> _Nonnull)klarnaComponent encounteredError:(KlarnaError * _Nonnull)error{
  278. // NSLog(@"klarna---222---error:%@",error);
  279. }
  280. - (void)klarnaResizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView to:(CGFloat)newHeight {
  281. // [self.klarna_payV mas_updateConstraints:^(MASConstraintMaker *make) {
  282. // make.height.mas_equalTo(newHeight);
  283. // }];
  284. }
  285. - (void)klarnaFailedInPaymentView:(KlarnaPaymentView * _Nonnull)paymentView withError:(KlarnaPaymentError * _Nonnull)error {
  286. // NSLog(@"klarna---777---%@",paymentView);
  287. }
  288. - (void)klarnaFinalizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken {
  289. }
  290. - (void)klarnaLoadedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView {
  291. // NSLog(@"klarna---333---%@",paymentView);
  292. // self.bottomPaybtn.backgroundColor = [UIColor colorWithHexString:@"#B2000F"];
  293. // self.bottomPaybtn.userInteractionEnabled = YES;
  294. }
  295. - (void)klarnaLoadedPaymentReviewWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView {
  296. // NSLog(@"klarna---444---%@",paymentView);
  297. }
  298. #pragma mark - **************** lazy ****************
  299. -(KlarnaPaymentView *)klarna_payV{
  300. if(!_klarna_payV){
  301. _klarna_payV = [[KlarnaPaymentView alloc] initWithCategory:@"pay_over_time" eventListener:self];
  302. _klarna_payV.translatesAutoresizingMaskIntoConstraints = false;
  303. }
  304. return _klarna_payV;
  305. }
  306. @end