ASCheckoutPayManager.m 13 KB

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