Cart_CheckoutC.m 30 KB

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