Cart_CheckoutC.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. //
  2. // Cart_CheckoutC.m
  3. // Asteria
  4. //
  5. // Created by 王猛 on 2024/2/16.
  6. //
  7. #import "Cart_CheckoutC.h"
  8. #import "ASAddressListViewController.h"
  9. #import "ASPayFailedViewController.h"
  10. #import "ASOrderDetailsViewController.h"
  11. #import "ASCheckoutTopView.h"
  12. #import "ASCheckoutBottomView.h"
  13. #import "CartCheckTableV.h"
  14. #import "CartVM.h"
  15. #import "ASUserModel.h"
  16. #import "ASGoodsDetailsVM.h"
  17. #import "ASAddGiftGoodsView.h"
  18. #import "ASCheckoutPayManager.h"
  19. @interface Cart_CheckoutC ()<RY_baseVMprotocol>
  20. @property (nonatomic, strong) CartVM *VM;
  21. @property (nonatomic, strong) ASCheckoutTopView *topView;
  22. @property (nonatomic, strong) ASCheckoutBottomView *bottomView;
  23. @property (nonatomic, strong) CartCheckTableV *TableV;
  24. @property (nonatomic, strong) MyCartCouponCellData *couponCellM;
  25. @property (nonatomic, strong) MyCartGrandTotalCellData *totalCellM;
  26. @property (nonatomic, strong) ASCheckoutPointData *pointCellM;
  27. @property (nonatomic, strong) ASCheckoutAddressData *addressCellM;
  28. @property (nonatomic, strong) ASCheckoutShipMethodData *shipmethodCellM;
  29. @property (nonatomic, strong) ASCheckoutCommentData *commentCellM;
  30. //存放支付方式
  31. @property (nonatomic, strong) NSArray *paymentArray;
  32. //选中支付方式
  33. @property (nonatomic, strong) ASCheckoutPaymentModel *paymentModel;
  34. //手动添加赠品至购物车
  35. @property (nonatomic, strong) ASGoodsDetailsVM *goodsDetailsVM;
  36. @property (nonatomic, strong) ASAddGiftGoodsView *addGiftView;
  37. @property (nonatomic, strong) NSMutableArray *addGiftParamArr;
  38. @property (nonatomic, assign) int index;
  39. @property (nonatomic, assign) int giftShowCount;
  40. @end
  41. @implementation Cart_CheckoutC
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. self.title = @"Checkout";
  45. //获取支付方式
  46. [self requestPaymentMode];
  47. //获取运输方式
  48. [self requestGetShipMethodByAddress];
  49. //获取购物车信息(地址信息)
  50. [self requestCartInfo];
  51. if (!self.totalsM) {
  52. [self reqNet_Cart_cartsMineTotals];
  53. }
  54. }
  55. - (void)initSubviews {
  56. [super initSubviews];
  57. self.addGiftParamArr = [[NSMutableArray alloc] initWithCapacity:1];
  58. self.index = 0;
  59. self.giftShowCount = 0;
  60. IPhoneXHeigh
  61. [self setupTableV:[CartCheckTableV class] Frame:CGRectMake(0, securitytop_Y , KScreenWidth, KScreenHeight - securitytop_Y)];
  62. self.TableV.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
  63. self.TableV.tableHeaderView = self.topView;
  64. self.TableV.tableFooterView = self.bottomView;
  65. @weakify(self)
  66. self.TableV.tapClose = ^(NSInteger num, id data) {
  67. //num = 0 删除商品 ,1 修改数量 countV
  68. @strongify(self)
  69. if([data isKindOfClass:[CartTotalsItemsM class]]){
  70. switch (num) {
  71. case 0:
  72. [self reqNet_DEL_cartsMineItems:(CartTotalsItemsM *)data];
  73. break;
  74. case 1:
  75. [self reqnet_Cart_cartsMineItems_Post:(CartTotalsItemsM *)data];
  76. break;
  77. default:
  78. break;
  79. }
  80. }else if ([data isKindOfClass:[MyCartCouponCellData class]]){
  81. MyCartCouponCellData *couponM = (MyCartCouponCellData *)data;
  82. if(num ==0){ //使用优惠券
  83. [self reqNet_PUT_cartsMineCoupons:couponM.coupon_code];
  84. }else if(num == 1){//删除优惠券
  85. [self reqNet_DEL_cartsMineCoupons];
  86. }
  87. } else if ([data isKindOfClass:[ASCheckoutPointData class]]) {
  88. ASCheckoutPointData *pointData = (ASCheckoutPointData *)data;
  89. if(num ==0){ //使用积分
  90. [self requestUseLimitPoint:pointData.usePoint];
  91. }else if(num == 1){//取消使用积分
  92. [self requestCancelUsePoint];
  93. }
  94. } else if ([data isKindOfClass:[ASCheckoutCommentData class]]) {
  95. ASCheckoutCommentData *commentData = (ASCheckoutCommentData *)data;
  96. if(num ==0){ //使用积分
  97. self.commentCellM.commentStr = commentData.commentStr;
  98. }
  99. } else if ([data isKindOfClass:[ASCheckoutShipMethodData class]]) {
  100. //选择运输方式、、、、并设置
  101. ASCheckoutShipMethodData *shipMethodData = (ASCheckoutShipMethodData *)data;
  102. self.shipmethodCellM = shipMethodData;
  103. for (int i = 0; i < self.shipmethodCellM.shipMethodArray.count; i++) {
  104. ASCheckoutShipMethodModel *shipModel = [self.shipmethodCellM.shipMethodArray objectAtIndex:i];
  105. if (i == num) {
  106. shipModel.isSelect = YES;
  107. self.shipmethodCellM.shipMethodModel = shipModel;
  108. } else {
  109. shipModel.isSelect = NO;
  110. }
  111. }
  112. [self requestSetShipMethodByAddress];
  113. } else if ([data isKindOfClass:[MyCartGrandTotalCellData class]]){
  114. // MyCartGrandTotalCellData *totalData = (MyCartGrandTotalCellData *)data;
  115. //丢件险
  116. [self requestLostExtraFee:num];
  117. }
  118. };
  119. }
  120. - (void)ry_respnsData:(id)data
  121. parseAry:(NSMutableArray *)arry
  122. sucess:(BOOL)sucessOrFail
  123. mark:(NSString *)mark
  124. reqNetType:(ReqNetType)reqNetType{
  125. [MBProgressHUD hideHUDForView:self.view animated:YES];
  126. if([mark isEqualToString:Cart_cartsMineTotals]){
  127. if(sucessOrFail){
  128. CartTotalsM *model = [arry firstObject];
  129. self.totalsM = model;
  130. [self updateData];
  131. }
  132. } else if ([mark isEqualToString:Chectout_getPaymentMode]) {
  133. if(sucessOrFail){
  134. // 更新支付方式
  135. self.paymentArray = arry;
  136. if (self.paymentArray.count > 0) {
  137. self.paymentModel = self.paymentArray[0];
  138. [self.bottomView setBottomPayStyle:self.paymentModel.code];
  139. }
  140. //首次进入展示购物车数据UI
  141. [self updateData];
  142. }
  143. } else if ([mark isEqualToString:Chectout_addBillingAddress]) {
  144. if(sucessOrFail){
  145. }
  146. }else if ([mark isEqualToString:Cart_cartInfo]) {
  147. if(sucessOrFail){
  148. // 获取上次下单地址
  149. NSDictionary *dic = (NSDictionary *)data;
  150. NSArray *array = [[dic objectForKey:@"extension_attributes"] objectForKey:@"shipping_assignments"];
  151. if (array.count > 0) {
  152. NSDictionary *shipping_assignments = array[0];
  153. NSDictionary *shippingDic = [shipping_assignments objectForKey:@"shipping"];
  154. NSDictionary *addressDic = [shippingDic objectForKey:@"address"];
  155. // ASAddressModel *addressModel = [ASAddressModel defualtData];
  156. ASAddressReginModel *reginModel = [ASAddressReginModel mj_objectWithKeyValues:addressDic];
  157. ASAddressModel *addressModel = [ASAddressModel mj_objectWithKeyValues:addressDic];
  158. addressModel.region = reginModel;
  159. if (!NIL(addressModel.postcode)) {
  160. self.addressCellM.addressModel = addressModel;
  161. if (self.shipmethodCellM.shipMethodModel) {
  162. //默认设置第一个运输方式
  163. [self requestSetShipMethodByAddress];
  164. }
  165. [self requestAddBillingAddress];
  166. }
  167. }
  168. //首次进入展示购物车数据UI
  169. [self updateData];
  170. }
  171. } else if ([mark isEqualToString:Chectout_addShipAddress_id]) {
  172. if(sucessOrFail){
  173. NSArray *shipMethodArr = (NSArray *)data;
  174. if (shipMethodArr.count > 0) {
  175. self.shipmethodCellM.shipMethodArray = [ASCheckoutShipMethodModel mj_objectArrayWithKeyValuesArray:shipMethodArr];
  176. ASCheckoutShipMethodModel *firstModel = self.shipmethodCellM.shipMethodArray[0];
  177. firstModel.isSelect = YES;
  178. self.shipmethodCellM.shipMethodModel = firstModel;
  179. self.shipmethodCellM.shipmethodSymbol = self.totalsM.currency_symbol;
  180. //默认设置第一个运输方式
  181. [self requestSetShipMethodByAddress];
  182. }
  183. // [self reqNet_Cart_cartsMineTotals];
  184. }
  185. /// || [mark isEqualToString:Chectout_addBillingAddress]
  186. } else if ([mark isEqualToString:Chectout_getShipMethod]) {
  187. if(sucessOrFail){
  188. NSArray *shipMethodArr = (NSArray *)data;
  189. if (shipMethodArr.count > 0) {
  190. self.shipmethodCellM.shipMethodArray = [ASCheckoutShipMethodModel mj_objectArrayWithKeyValuesArray:shipMethodArr];
  191. ASCheckoutShipMethodModel *firstModel = self.shipmethodCellM.shipMethodArray[0];
  192. firstModel.isSelect = YES;
  193. self.shipmethodCellM.shipMethodModel = firstModel;
  194. if (self.addressCellM.addressModel) {
  195. //默认设置第一个运输方式
  196. [self requestSetShipMethodByAddress];
  197. }
  198. }
  199. }else{
  200. [self.view makeToast:(NSString *)data duration:2 position:CSToastPositionCenter];
  201. }
  202. [self updateData];
  203. } else if ([mark isEqualToString:Cart_cartsMineItems_Post]
  204. || [mark isEqualToString:DEL_cartsMineItems]
  205. || [mark isEqualToString:PUT_cartsMineCoupons]
  206. || [mark isEqualToString:Chectout_PUT_usePoints]
  207. || [mark isEqualToString:Chectout_DEL_cancelusePoints]
  208. || [mark isEqualToString:Chectout_setShipMethod]
  209. || [mark isEqualToString:Chectout_giftAddCart]
  210. || [mark isEqualToString:Chectout_lost_extrafee]){
  211. if(sucessOrFail){
  212. if ([mark isEqualToString:Chectout_giftAddCart]) {
  213. self.index --;
  214. if (self.index == 0) {
  215. [self reqNet_Cart_cartsMineTotals];
  216. }
  217. } else {
  218. [self reqNet_Cart_cartsMineTotals];
  219. }
  220. }else{
  221. [self.view makeToast:(NSString *)data duration:2 position:CSToastPositionCenter];
  222. }
  223. } else if ([mark isEqualToString:Goods_productGetProductsById]) {
  224. //请求赠品详情
  225. if(sucessOrFail) {
  226. if (!AS_Array_valid(arry)) {
  227. return;
  228. }
  229. GoodsInformationM *model = (GoodsInformationM *)[arry firstObject];
  230. NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:@{@"product_id":model.Id, @"isPromoItems":@"yes"}];
  231. if (model.options.count > 0) {
  232. NSMutableDictionary *optionDic = [NSMutableDictionary dictionary];
  233. for (OptionsModel *optionM in model.options) {
  234. OptionsValuesM *valuesM = optionM.values[0];
  235. NSString *tempStr =[NSString stringWithFormat:@"options[%@]",optionM.option_id];
  236. optionDic[tempStr] =valuesM.option_type_id;
  237. }
  238. [param addEntriesFromDictionary:optionDic];
  239. }
  240. [self.addGiftParamArr addObject:param];
  241. if (self.giftShowCount <= 1 && self.addGiftParamArr.count == 1 && self.addGiftView.hidden) {
  242. dispatch_async(dispatch_get_main_queue(), ^{
  243. self.addGiftView.hidden = NO;
  244. [self.addGiftView setGiftData:model.add_gooodsImgUrl];
  245. });
  246. }
  247. }
  248. }
  249. }
  250. - (void)updateData{
  251. [self.topView setTopViewData:AS_String_NotNull(self.totalsM.items_qty) price:[NSString stringWithFormat:@"%@%@",AS_String_NotNull(self.totalsM.currency_symbol),AS_String_NotNull(self.totalsM.subtotal)]];
  252. [self.TableV.infodata removeAllObjects];
  253. self.TableV.infodata = [NSMutableArray arrayWithArray:self.totalsM.items];
  254. //邮寄地址--邮寄方式
  255. [self.TableV.infodata addObject:self.addressCellM];
  256. //运输方式
  257. self.shipmethodCellM.shipmethodSymbol = self.totalsM.currency_symbol;
  258. [self.TableV.infodata addObject:self.shipmethodCellM];
  259. //优惠券
  260. self.couponCellM.couponAry =[NSMutableArray arrayWithArray: [ASNetTools shared].xxx_couponAry];
  261. self.couponCellM.coupon_code = self.totalsM.coupon_code;
  262. self.couponCellM.discount_amount = self.totalsM.discount_amount;
  263. self.couponCellM.currency_symbol = self.totalsM.currency_symbol;
  264. [self.TableV.infodata addObject:self.couponCellM];
  265. //积分
  266. NSDictionary *pointScaleDic = [Current_normalTool dicFromjsonStr:[self.totalsM.extension_attributes objectForKey:@"mw_earn_points_data"]];
  267. if (pointScaleDic.count > 0) {
  268. self.pointCellM.pointScale = [[pointScaleDic allValues] objectAtIndex:0];
  269. self.pointCellM.priceScale = [[pointScaleDic allKeys] objectAtIndex:0];
  270. }
  271. self.pointCellM.usePoint = [NSString stringWithFormat:@"%@", [self.totalsM.extension_attributes objectForKey:@"mw_rwrdpoints_amnt"]];
  272. self.pointCellM.usePrice = [NSString stringWithFormat:@"%@", [self.totalsM.extension_attributes objectForKey:@"mw_rwrdpoints_cur_amnt"]];
  273. self.pointCellM.pointBalance = [ASUserInfoManager shared].userPoints;
  274. self.pointCellM.priceSymbol = self.totalsM.currency_symbol;
  275. [self.TableV.infodata addObject:self.pointCellM];
  276. //备注
  277. [self.TableV.infodata addObject:self.commentCellM];
  278. //价格明细
  279. self.totalCellM.total_segments = [NSMutableArray arrayWithArray:self.totalsM.total_segments];
  280. self.totalCellM.currency_symbol = self.totalsM.currency_symbol;
  281. [self.TableV.infodata addObject:self.totalCellM];
  282. //支付方式数据
  283. [self.TableV.infodata addObjectsFromArray:self.paymentArray];
  284. //手动添加赠品功能
  285. [self.addGiftParamArr removeAllObjects];
  286. NSDictionary *freeData = (NSDictionary *)[[self.totalsM.extension_attributes objectForKey:@"free_data"] mj_JSONObject];
  287. BOOL is_show = [[freeData objectForKey:@"is_show"] boolValue];
  288. if (is_show && self.giftShowCount< 1) {//显示存在赠品
  289. NSArray *productArr = [freeData objectForKey:@"products"];
  290. if (productArr.count > 0) { //存在赠品
  291. for (int i = 0; i < productArr.count; i++) {
  292. NSDictionary *productDic = productArr[i];
  293. NSString *entity_id = productDic[@"entity_id"];
  294. [self reqNet_GoodsDetails_productGetProduct:entity_id];
  295. }
  296. self.giftShowCount++;
  297. }
  298. }
  299. [self.TableV reloadData];
  300. }
  301. - (void)tapcellTriggereventIndex:(NSIndexPath *)index model:(id)model{
  302. if([model isKindOfClass:[CartTotalsItemsM class]]){
  303. CartTotalsItemsM *itemM = (CartTotalsItemsM *)model;
  304. if(itemM.isGift){
  305. return;
  306. }
  307. UIViewController *viewController = [[CTMediator sharedInstance] Goods_GoodsDetailsC:@{@"entity_id":itemM.product_id}];
  308. [self.navigationController pushViewController:viewController animated:YES];
  309. } else if ([model isKindOfClass:[ASCheckoutPaymentModel class]]) {
  310. ASCheckoutPaymentModel *paymentModel = (ASCheckoutPaymentModel *)model;
  311. if (paymentModel.isSelect) {
  312. return;
  313. }
  314. for (int i = 0; i < self.TableV.infodata.count; i++) {
  315. id itemModel = self.TableV.infodata[i];
  316. if ([itemModel isKindOfClass:[ASCheckoutPaymentModel class]]) {
  317. ASCheckoutPaymentModel *tempModel = (ASCheckoutPaymentModel *)itemModel;
  318. if (index.row == i) {
  319. tempModel.isSelect = YES;
  320. self.paymentModel = tempModel;
  321. [self.bottomView setBottomPayStyle:tempModel.code];
  322. } else {
  323. tempModel.isSelect = NO;
  324. }
  325. }
  326. }
  327. [self.TableV reloadData];
  328. } else if ([model isKindOfClass:[ASCheckoutAddressData class]]) {
  329. ASAddressListViewController *vc = [ASAddressListViewController new];
  330. vc.isSelMode = true;
  331. vc.sel_Id = self.addressCellM.addressModel.Id;
  332. @weakify(self)
  333. vc.selectAddressBlock = ^(ASAddressModel * _Nonnull addressM) {
  334. @strongify(self)
  335. self.addressCellM.addressModel = addressM;
  336. [self requestAddShipAddress];
  337. [self requestAddBillingAddress];
  338. };
  339. [self.navigationController pushViewController:vc animated:true];
  340. }
  341. }
  342. #pragma mark - **************** reqNet ****************
  343. //获取支付方式
  344. - (void)requestPaymentMode {
  345. [self.VM ry_requestGetApi:Chectout_getPaymentMode param:@{}];
  346. }
  347. //添加邮寄地址
  348. - (void)requestAddShipAddress {
  349. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  350. [self.VM ry_requestPostApi:Chectout_addShipAddress_id param:@{@"addressId":self.addressCellM.addressModel.Id}];
  351. }
  352. - (NSDictionary *)getAddressParam:(ASAddressModel *)addressModel {
  353. NSDictionary *param = @{};
  354. NSArray *street = addressModel.street;
  355. NSDictionary *shipping_address = @{@"region_code":addressModel.region_id,
  356. @"country_id":addressModel.country_id,
  357. @"postcode":addressModel.postcode,
  358. @"lastname":addressModel.lastname,
  359. @"firstname":addressModel.firstname,
  360. @"city":addressModel.city,
  361. @"region":addressModel.region.region,
  362. @"region_id":addressModel.region.region_id,
  363. @"telephone":addressModel.telephone,
  364. @"street":street,
  365. @"email":ASUserInfoManager.shared.userInfo.email};
  366. return shipping_address;
  367. // @{@"addressInformation":shipping_address}
  368. }
  369. //添加账单地址
  370. - (void)requestAddBillingAddress {
  371. // [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  372. NSDictionary *addressInformation = [self getAddressParam:self.addressCellM.addressModel];
  373. NSDictionary *params = @{@"address":addressInformation, @"useForShipping":@"true"};
  374. [self.VM ry_requestPostApi:Chectout_addBillingAddress param:params];
  375. }
  376. //根据邮寄地址获取邮寄方式
  377. - (void)requestGetShipMethodByAddress {
  378. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  379. [self.VM ry_requestGetApi:Chectout_getShipMethod param:@{}];
  380. }
  381. //设置邮寄方式
  382. - (void)requestSetShipMethodByAddress {
  383. if (NIL(self.shipmethodCellM.shipMethodModel.method_code) || NIL(self.shipmethodCellM.shipMethodModel.carrier_code)) {
  384. [self.view makeToast:self.shipmethodCellM.shipMethodModel.error_message];
  385. return;
  386. }
  387. NSDictionary *shipping_address = [self getAddressParam:self.addressCellM.addressModel];
  388. NSDictionary *params = @{@"shipping_address":shipping_address,
  389. @"shipping_method_code":self.shipmethodCellM.shipMethodModel.method_code,
  390. @"shipping_carrier_code":self.shipmethodCellM.shipMethodModel.carrier_code};
  391. NSDictionary *finalDic = @{@"addressInformation":params};
  392. NSLog(@"finalDic=====%@", [finalDic mj_JSONString]);
  393. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  394. [self.VM ry_requestPostApi:Chectout_setShipMethod param:finalDic];
  395. }
  396. //获取购物车信息(地址信息)
  397. - (void)requestCartInfo {
  398. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  399. [self.VM ry_requestGetApi:Cart_cartInfo param:@{}];
  400. }
  401. ///获取购物车信息(报价整合)
  402. -(void)reqNet_Cart_cartsMineTotals{
  403. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  404. [self.VM ry_requestGetApi:Cart_cartsMineTotals param:@{}];
  405. }
  406. ///修改商品的数量
  407. -(void)reqnet_Cart_cartsMineItems_Post:(CartTotalsItemsM *)itemM{
  408. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  409. NSDictionary *cartItemDic = @{
  410. @"item_id":itemM.item_id,
  411. @"qty":itemM.qty,
  412. @"quote_id":self.totalsM.cart_id
  413. };
  414. NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:@{
  415. @"cartItem":cartItemDic
  416. }];
  417. [self.VM ry_requestPostApi:Cart_cartsMineItems_Post param:param];
  418. }
  419. ///删除商品
  420. -(void)reqNet_DEL_cartsMineItems:(CartTotalsItemsM *)itemM{
  421. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  422. [self.VM ry_requestDeleteApi:DEL_cartsMineItems paramStr:itemM.item_id];
  423. }
  424. -(void)reqNet_PUT_cartsMineCoupons:(NSString *)coupon{
  425. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  426. self.giftShowCount = 0;
  427. [self.VM ry_requestPutApi:PUT_cartsMineCoupons param:coupon];
  428. }
  429. -(void)reqNet_DEL_cartsMineCoupons{
  430. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  431. [self.VM ry_requestDeleteApi:DEL_cartsMineCoupons paramStr:@""];
  432. }
  433. //使用指定积分
  434. - (void)requestUseLimitPoint:(NSString *)point {
  435. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  436. [self.VM ry_requestPutApi:Chectout_PUT_usePoints param:point];
  437. }
  438. //取消使用积分
  439. - (void)requestCancelUsePoint {
  440. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  441. [self.VM ry_requestDeleteApi:Chectout_DEL_cancelusePoints paramStr:@""];
  442. }
  443. //下单
  444. -(void)requestSureOrder{
  445. NSString *paymentCode = self.paymentModel.code;
  446. [ASCheckoutPayManager sharedInstance].cartId = self.totalsM.cart_id;
  447. [ASCheckoutPayManager sharedInstance].addressModel = self.addressCellM.addressModel;
  448. [[ASCheckoutPayManager sharedInstance] checkoutPayWithMethodType:paymentCode param:@{@"price":self.totalsM.grand_total, @"currency":self.totalsM.currency_symbol}];
  449. K_WEAK_SELF;
  450. [ASCheckoutPayManager sharedInstance].payFinishBlock = ^(NSString * _Nonnull payMethod, NSUInteger isSucess, id _Nonnull payData) {
  451. K_STRONG_SELF;
  452. NSLog(@"payData======%@", payData);
  453. NSDictionary *payDic = (NSDictionary *)payData;
  454. if (isSucess == 1) {
  455. [self.view makeToast:@"Payment success" duration:0.5 position:CSToastPositionCenter];
  456. K_WEAK_SELF;
  457. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  458. K_STRONG_SELF;
  459. NSString *orderidStr = [NSString stringWithFormat:@"%@", [payDic objectForKey:@"orderid"]];
  460. [self paymentResultStatus:orderidStr];
  461. // if ([payMethod isEqualToString:@"paypal_express"]) {
  462. // [self paymentResultStatus:orderidStr];
  463. // } else if ([payMethod isEqualToString:@"stripe_payments"]) {//visa
  464. // [self paymentResultStatus:orderidStr];
  465. // } else if ([payMethod isEqualToString:@"afterpay_payment"]) {//afterpay
  466. //
  467. // } else if ([payMethod isEqualToString:@"klarna_kco"]) {//klarna
  468. //// NSString *orderidStr = [NSString stringWithFormat:@"%@", [payDic objectForKey:@"orderid"]];
  469. //
  470. // }
  471. });
  472. } else {
  473. NSString *msg = [NSString stringWithFormat:@"%@", [payDic objectForKey:@"msg"]];
  474. if ([msg isEqualToString:@"Payment anomaly"] && [payMethod isEqualToString:@"klarna_kco"]) {
  475. [self.view makeToast:msg duration:0.5 position:CSToastPositionCenter];
  476. K_WEAK_SELF;
  477. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  478. K_STRONG_SELF;
  479. [self reqNet_Cart_cartsMineTotals];
  480. });
  481. return;
  482. }
  483. [self.view makeToast:@"Payment Failed" duration:0.5 position:CSToastPositionCenter];
  484. K_WEAK_SELF;
  485. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  486. K_STRONG_SELF;
  487. //支付失败
  488. [self paymentResultStatus:@""];
  489. });
  490. }
  491. };
  492. }
  493. - (void)paymentResultStatus:(NSString *)orderid {
  494. if (AS_String_valid(orderid)) {
  495. ASOrderDetailsViewController *paySuccessVC = [[ASOrderDetailsViewController alloc] init];
  496. paySuccessVC.orderId = orderid;
  497. paySuccessVC.isPay = YES;
  498. [self.navigationController pushViewController:paySuccessVC animated:YES];
  499. } else {
  500. ASPayFailedViewController *payFailedVC = [[ASPayFailedViewController alloc] init];
  501. [self.navigationController pushViewController:payFailedVC animated:YES];
  502. }
  503. }
  504. //请求赠品商品详情
  505. -(void)reqNet_GoodsDetails_productGetProduct:(NSString *)enterId{
  506. NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
  507. [params setObject:enterId forKey:@"productId"];
  508. [self.goodsDetailsVM ry_requestGetApi:Goods_productGetProductsById param:params];
  509. }
  510. //赠品加车
  511. - (void)requestGiftAddCart {
  512. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  513. for (int i = 0; i < self.addGiftParamArr.count; i++) {
  514. NSDictionary *param = self.addGiftParamArr[i];
  515. self.index ++;
  516. self.giftShowCount = 1;
  517. [self.VM ry_formDataRequestPostApi:Chectout_giftAddCart param:param];
  518. }
  519. }
  520. //丢件险
  521. - (void)requestLostExtraFee:(BOOL)isOpen {
  522. NSDictionary *address = @{@"countryId":AS_String_valid(self.addressCellM.addressModel.country_id) ? AS_String_NotNull(self.addressCellM.addressModel.country_id) : @"US",
  523. @"region":AS_String_NotNull(self.addressCellM.addressModel.region.region),
  524. @"postcode":AS_String_NotNull(self.addressCellM.addressModel.postcode),
  525. @"city":AS_String_NotNull(self.addressCellM.addressModel.city)};
  526. NSDictionary *addressInformation = @{@"address":address,
  527. @"shipping_method_code":AS_String_valid(self.shipmethodCellM.shipMethodModel.method_code) ? self.shipmethodCellM.shipMethodModel.method_code : @"bestway",
  528. @"shipping_carrier_code":AS_String_valid(self.shipmethodCellM.shipMethodModel.carrier_code) ? self.shipmethodCellM.shipMethodModel.carrier_code : @"tablerate"};
  529. NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithCapacity:1];
  530. if(isOpen ==0){ //取消丢件险
  531. NSDictionary *information = @{@"fee_id":@(1), @"options_ids":@[]};
  532. [params addEntriesFromDictionary:@{@"information":information}];
  533. }else if(isOpen == 1){//打开丢件险
  534. NSDictionary *information = @{@"fee_id":@(1), @"options_ids":@[@"1"]};
  535. [params addEntriesFromDictionary:@{@"information":information}];
  536. }
  537. [params setValue:addressInformation forKey:@"addressInformation"];
  538. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  539. [self.VM ry_requestPostApi:Chectout_lost_extrafee param:params];
  540. }
  541. #pragma mark --- 懒加载 ---
  542. - (void)ucm_bindvmmodel{
  543. self.VM = [[CartVM alloc] initDelegate:self];
  544. self.goodsDetailsVM = [[ASGoodsDetailsVM alloc] initDelegate:self];
  545. }
  546. - (MyCartCouponCellData *)couponCellM {
  547. if (!_couponCellM) {
  548. _couponCellM = [[MyCartCouponCellData alloc] init];
  549. }
  550. return _couponCellM;
  551. }
  552. - (MyCartGrandTotalCellData *)totalCellM {
  553. if (!_totalCellM) {
  554. _totalCellM = [[MyCartGrandTotalCellData alloc] init];
  555. }
  556. return _totalCellM;
  557. }
  558. - (ASCheckoutPointData *)pointCellM {
  559. if (!_pointCellM) {
  560. _pointCellM = [[ASCheckoutPointData alloc] init];
  561. }
  562. return _pointCellM;
  563. }
  564. - (ASCheckoutAddressData *)addressCellM {
  565. if (!_addressCellM) {
  566. _addressCellM = [[ASCheckoutAddressData alloc] init];
  567. }
  568. return _addressCellM;
  569. }
  570. - (ASCheckoutShipMethodData *)shipmethodCellM {
  571. if (!_shipmethodCellM) {
  572. _shipmethodCellM = [[ASCheckoutShipMethodData alloc] init];
  573. }
  574. return _shipmethodCellM;
  575. }
  576. - (ASCheckoutCommentData *)commentCellM {
  577. if (!_commentCellM) {
  578. _commentCellM = [[ASCheckoutCommentData alloc] init];
  579. _commentCellM.commentStr = @"";
  580. }
  581. return _commentCellM;
  582. }
  583. - (ASCheckoutTopView *)topView {
  584. if (!_topView) {
  585. // IPhoneXHeigh
  586. _topView = [[ASCheckoutTopView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 60)];
  587. @weakify(self)
  588. _topView.isFlodBlock = ^(BOOL isFlod) {
  589. @strongify(self)
  590. self.TableV.isFlod = isFlod;
  591. [self.TableV reloadData];
  592. };
  593. }
  594. return _topView;
  595. }
  596. - (ASCheckoutBottomView *)bottomView {
  597. if (!_bottomView) {
  598. _bottomView = [[ASCheckoutBottomView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 100)];
  599. @weakify(self)
  600. _bottomView.bottomBlock = ^{
  601. @strongify(self)
  602. // [self.view makeToast:@"调起支付"];
  603. [self requestSureOrder];
  604. };
  605. }
  606. return _bottomView;
  607. }
  608. - (ASAddGiftGoodsView *)addGiftView {
  609. if (!_addGiftView) {
  610. _addGiftView = [[ASAddGiftGoodsView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
  611. @weakify(self)
  612. _addGiftView.addBlock = ^(int type) {
  613. @strongify(self)
  614. self.addGiftView.hidden = YES;
  615. if (type == 1) {
  616. [self requestGiftAddCart];
  617. } else {
  618. self.giftShowCount ++;
  619. }
  620. };
  621. _addGiftView.hidden = YES;
  622. [[UIApplication sharedApplication].windows.firstObject addSubview:_addGiftView];
  623. }
  624. return _addGiftView;
  625. }
  626. #pragma mark - **************** reqNet ****************
  627. @end