Cart_CheckoutC.m 32 KB

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