ASCheckoutPayManager.m 13 KB

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