ASCheckoutPayManager.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. [self requestVerifyPaypalToken:webData];
  131. };
  132. [vc xxx_dsWebLoadUrl:urlStr];
  133. vc.modalPresentationStyle = UIModalPresentationFullScreen;
  134. [tmpvc presentViewController:vc animated:YES completion:nil];
  135. }
  136. //paypal支付 Token验证
  137. - (void)requestVerifyPaypalToken:(NSDictionary *)dic {
  138. UIViewController *topVC = topViewController();
  139. NSDictionary *params = @{@"token":[dic objectForKey:@"token"]};
  140. [MBProgressHUD showHUDAddedTo:topVC.view animated:YES];
  141. K_WEAK_SELF;
  142. [ASNetTools.shared postWithPath:Chectout_Verify_PayToken param:params success:^(id _Nonnull json) {
  143. K_STRONG_SELF;
  144. [MBProgressHUD hideHUDForView:topVC.view animated:YES];
  145. NSDictionary *payReturnDic = (NSDictionary *)json;
  146. NSString *orderid = [payReturnDic objectForKey:@"id"];
  147. if (self.payFinishBlock) {
  148. self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
  149. }
  150. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  151. K_STRONG_SELF;
  152. [MBProgressHUD hideHUDForView:topVC.view animated:YES];
  153. if (self.payFinishBlock) {
  154. self.payFinishBlock(self.payType, 0, @{@"msg":msg});
  155. }
  156. }];
  157. }
  158. #pragma mark ----- klarna支付验证 -----
  159. //klarna支付验证
  160. -(void)tool_verifyOrderAutoken:(NSString *)authToken{
  161. UIViewController *topVC = topViewController();
  162. NSDictionary *params = @{@"authorization_token":authToken};
  163. [MBProgressHUD showHUDAddedTo:topVC.view animated:YES];
  164. K_WEAK_SELF;
  165. [ASNetTools.shared postWithPath:Chectout_Verify_KlarnaCart param:params success:^(id _Nonnull json) {
  166. K_STRONG_SELF;
  167. NSString *is_active = [(NSDictionary *)json objectForKey:@"is_active"];
  168. NSLog(@"=======%@", is_active);
  169. if ([is_active isEqualToString:@"1"]) {
  170. [self tool_creatOrderAutoken:authToken];
  171. } else {
  172. //异常状态、需要返回购物车刷新
  173. if (self.payFinishBlock) {
  174. self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment anomaly"});
  175. }
  176. }
  177. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  178. K_STRONG_SELF;
  179. [MBProgressHUD hideHUDForView:topVC.view animated:YES];
  180. if (self.payFinishBlock) {
  181. self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment anomaly"});
  182. }
  183. }];
  184. }
  185. //klarna支付
  186. -(void)tool_creatOrderAutoken:(NSString *)authToken{
  187. UIViewController *topVC = topViewController();
  188. self.client_token = authToken;
  189. NSDictionary *additional_data = @{@"authorization_token":authToken};
  190. NSDictionary *payType = @{@"method":@"klarna_pay_over_time",
  191. @"additional_data":additional_data};
  192. NSDictionary *params = @{@"paymentMethod":payType,
  193. @"cartId":self.cartId};
  194. // [MBProgressHUD showHUDAddedTo:topVC.view animated:YES];
  195. K_WEAK_SELF;
  196. [ASNetTools.shared postWithPath:Chectout_Verify_KlarnaToken param:params success:^(id _Nonnull json) {
  197. K_STRONG_SELF;
  198. [MBProgressHUD hideHUDForView:topVC.view animated:YES];
  199. NSString *orderid = (NSString *)json;
  200. NSLog(@"=======%@", orderid);
  201. if (self.payFinishBlock) {
  202. self.payFinishBlock(self.payType, 1, @{@"orderid":orderid});
  203. }
  204. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  205. K_STRONG_SELF;
  206. [MBProgressHUD hideHUDForView:topVC.view animated:YES];
  207. if (self.payFinishBlock) {
  208. self.payFinishBlock(self.payType, 0, @{@"msg":msg});
  209. }
  210. }];
  211. }
  212. #pragma mark - **************** KlarnaPaymentEventListener ****************
  213. - (void)klarnaInitializedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView { //init 成功
  214. NSLog(@"klarna---888---%@",self.client_token);
  215. //klarna_2 加载试图
  216. [paymentView loadWithJsonData:self.client_token];
  217. //klarna_3 授权支付操作 klarna_getClientTokenAurhorizePay
  218. [self.klarna_payV authorizeWithAutoFinalize:YES jsonData:self.client_token];
  219. }
  220. - (void)klarnaAuthorizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken finalizeRequired:(BOOL)finalizeRequired {
  221. NSLog(@"klarna---555---%@--approved---%d",authToken, approved);
  222. if (approved == true ){
  223. NSLog(@"klarna 授权支付成功");
  224. [self tool_verifyOrderAutoken:authToken];
  225. } else {
  226. if (self.payFinishBlock) {
  227. self.payFinishBlock(self.payType, 0, @{@"msg":@"Payment Failed"});
  228. }
  229. NSLog(@"klarna 授权支付失败");
  230. }
  231. }
  232. - (void)klarnaReauthorizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken {
  233. NSLog(@"klarna---666---%@ approved authToken:(NSString * _Nullable)authToken",paymentView);
  234. if (approved == true ){
  235. [self tool_creatOrderAutoken:authToken];
  236. }
  237. }
  238. #pragma mark - **************** KlarnaEventHandler ****************
  239. - (void)klarnaComponent:(id <KlarnaComponent> _Nonnull)klarnaComponent dispatchedEvent:(KlarnaProductEvent * _Nonnull)event{
  240. // NSLog(@"klarna---111---%@",event.qmui_allBindingKeys);
  241. }
  242. - (void)klarnaComponent:(id <KlarnaComponent> _Nonnull)klarnaComponent encounteredError:(KlarnaError * _Nonnull)error{
  243. // NSLog(@"klarna---222---error:%@",error);
  244. }
  245. - (void)klarnaResizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView to:(CGFloat)newHeight {
  246. // [self.klarna_payV mas_updateConstraints:^(MASConstraintMaker *make) {
  247. // make.height.mas_equalTo(newHeight);
  248. // }];
  249. }
  250. - (void)klarnaFailedInPaymentView:(KlarnaPaymentView * _Nonnull)paymentView withError:(KlarnaPaymentError * _Nonnull)error {
  251. // NSLog(@"klarna---777---%@",paymentView);
  252. }
  253. - (void)klarnaFinalizedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView approved:(BOOL)approved authToken:(NSString * _Nullable)authToken {
  254. }
  255. - (void)klarnaLoadedWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView {
  256. // NSLog(@"klarna---333---%@",paymentView);
  257. // self.bottomPaybtn.backgroundColor = [UIColor colorWithHexString:@"#B2000F"];
  258. // self.bottomPaybtn.userInteractionEnabled = YES;
  259. }
  260. - (void)klarnaLoadedPaymentReviewWithPaymentView:(KlarnaPaymentView * _Nonnull)paymentView {
  261. // NSLog(@"klarna---444---%@",paymentView);
  262. }
  263. #pragma mark - **************** lazy ****************
  264. -(KlarnaPaymentView *)klarna_payV{
  265. if(!_klarna_payV){
  266. _klarna_payV = [[KlarnaPaymentView alloc] initWithCategory:@"pay_over_time" eventListener:self];
  267. _klarna_payV.translatesAutoresizingMaskIntoConstraints = false;
  268. }
  269. return _klarna_payV;
  270. }
  271. @end