CartController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Themes\Pro\modunawa\app\Http\Controllers\Shop;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\CallApiService;
  5. class CartController extends Controller
  6. {
  7. private $callApiService;
  8. public function __construct(CallApiService $callApiService)
  9. {
  10. $this->callApiService = $callApiService;
  11. }
  12. public function store()
  13. {
  14. if (empty(request('item_id'))) {
  15. notify()->error('상품을 선택해주세요', 'Error', 'bottomRight');
  16. return redirect()->back();
  17. }
  18. if ((int)request('cart_qty') < 1) {
  19. notify()->error('수량 에러', 'Error', 'bottomRight');
  20. return redirect()->back();
  21. }
  22. $item = $this->callApiService->callApi([
  23. 'url' => 'item-pick',
  24. 'data' => [
  25. 'Page' => [
  26. ['Id' => (int)request('item_id') ]
  27. ]
  28. ],
  29. ])['Page'][0];
  30. $cartPick = $this->callApiService->callApi([
  31. 'url' => 'cart-pick',
  32. 'data' => [
  33. 'Page' => [
  34. ['ItemId' => (int)request('item_id'), 'MemberBuyerId' => session('member')['MemberBuyerId'] ]
  35. ]
  36. ],
  37. ]);
  38. if ($this->callApiService->verifyApiError($cartPick)) {
  39. $id = 0;
  40. $cartQty = (int)request('cart_qty');
  41. } else {
  42. $cart = $cartPick['Page'][0];
  43. $id = $cart['Id'];
  44. $cartQty = (int)$cart['CartQty'] + request('cart_qty');
  45. }
  46. $cartAmt = $item['SalesPrc'] * $cartQty;
  47. $response = $this->callApiService->callApi([
  48. 'url' => 'cart-act',
  49. 'data' => [
  50. 'Page' => [
  51. [
  52. 'Id' => $id,
  53. 'ItemId' => $item['Id'],
  54. 'Checked' => '0',
  55. 'CartQty' => (string)$cartQty,
  56. 'CartPrc' => (string)$item['SalesPrc'],
  57. 'CartAmt' => (string)$cartAmt,
  58. ]
  59. ]
  60. ],
  61. ]);
  62. // dd($response);
  63. if ($this->callApiService->verifyApiError($response)) {
  64. notify()->error($response['body'], 'Error', 'bottomRight');
  65. return redirect()->back();
  66. }
  67. notify()->success(_e('Action completed'), 'Success', 'bottomRight');
  68. if (request('purchase')) {
  69. return redirect()->route('checkout.index');
  70. }
  71. return redirect()->back();
  72. }
  73. }