{
    "info":  {
                 "_postman_id":  "d3cd6e33-648b-46d1-9bea-32f7fbdf03e5",
                 "name":  "Checkout - Final Step",
                 "description":  "Final checkout: order summary, delivery, coupon, payment-method selection, and order completion.",
                 "schema":  "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
             },
    "variable":  [
                     {
                         "key":  "baseUrl",
                         "value":  "http://localhost:8000/api/v1"
                     },
                     {
                         "key":  "accessToken",
                         "value":  ""
                     },
                     {
                         "key":  "addressId",
                         "value":  ""
                     },
                     {
                         "key":  "deliveryMethodId",
                         "value":  ""
                     },
                     {
                         "key":  "paymentMethodId",
                         "value":  ""
                     },
                     {
                         "key":  "couponCode",
                         "value":  ""
                     }
                 ],
    "auth":  {
                 "type":  "bearer",
                 "bearer":  [
                                {
                                    "key":  "token",
                                    "value":  "{{accessToken}}",
                                    "type":  "string"
                                }
                            ]
             },
    "item":  [
                 {
                     "name":  "Final checkout",
                     "item":  [
                                  {
                                      "name":  "1. Get checkout data",
                                      "event":  [
                                                    {
                                                        "listen":  "test",
                                                        "script":  {
                                                                       "type":  "text/javascript",
                                                                       "exec":  [
                                                                                    "pm.test(\u0027Request succeeded\u0027, () =\u003e pm.response.to.have.status(200));",
                                                                                    "const body = pm.response.json();",
                                                                                    "pm.test(\u0027Has cart, addresses, and payment methods\u0027, () =\u003e {",
                                                                                    "  pm.expect(body.data).to.have.property(\u0027cart\u0027);",
                                                                                    "  pm.expect(body.data).to.have.property(\u0027addresses\u0027);",
                                                                                    "  pm.expect(body.data).to.have.property(\u0027payment_methods\u0027);",
                                                                                    "});",
                                                                                    "const defaultAddress = body.data.addresses.find((address) =\u003e address.is_default);",
                                                                                    "if (defaultAddress) pm.collectionVariables.set(\u0027addressId\u0027, defaultAddress.id);",
                                                                                    "const cashMethods = body.data.payment_methods.cash || [];",
                                                                                    "const installmentMethods = body.data.payment_methods.installment || [];",
                                                                                    "const method = cashMethods[0] || installmentMethods[0];",
                                                                                    "if (method) pm.collectionVariables.set(\u0027paymentMethodId\u0027, method.id);"
                                                                                ]
                                                                   }
                                                    }
                                                ],
                                      "request":  {
                                                      "method":  "GET",
                                                      "header":  [
                                                                     {
                                                                         "key":  "Accept",
                                                                         "value":  "application/json"
                                                                     }
                                                                 ],
                                                      "url":  "{{baseUrl}}/cart-checkout"
                                                  }
                                  },
                                  {
                                      "name":  "2. Get delivery methods",
                                      "event":  [
                                                    {
                                                        "listen":  "test",
                                                        "script":  {
                                                                       "type":  "text/javascript",
                                                                       "exec":  [
                                                                                    "pm.test(\u0027Request succeeded\u0027, () =\u003e pm.response.to.have.status(200));",
                                                                                    "const methods = pm.response.json().data.delivery_methods || [];",
                                                                                    "if (methods.length) pm.collectionVariables.set(\u0027deliveryMethodId\u0027, methods[0].id);"
                                                                                ]
                                                                   }
                                                    }
                                                ],
                                      "request":  {
                                                      "method":  "GET",
                                                      "header":  [
                                                                     {
                                                                         "key":  "Accept",
                                                                         "value":  "application/json"
                                                                     }
                                                                 ],
                                                      "url":  {
                                                                  "raw":  "{{baseUrl}}/cart-checkout/delivery-methods?address_id={{addressId}}",
                                                                  "host":  [
                                                                               "{{baseUrl}}"
                                                                           ],
                                                                  "path":  [
                                                                               "cart-checkout",
                                                                               "delivery-methods"
                                                                           ],
                                                                  "query":  [
                                                                                {
                                                                                    "key":  "address_id",
                                                                                    "value":  "{{addressId}}"
                                                                                }
                                                                            ]
                                                              }
                                                  }
                                  },
                                  {
                                      "name":  "3. Check coupon and refresh summary",
                                      "event":  [
                                                    {
                                                        "listen":  "test",
                                                        "script":  {
                                                                       "type":  "text/javascript",
                                                                       "exec":  [
                                                                                    "pm.test(\u0027Request succeeded\u0027, () =\u003e pm.response.to.have.status(200));",
                                                                                    "const data = pm.response.json().data;",
                                                                                    "pm.test(\u0027Coupon response has refreshed summary\u0027, () =\u003e {",
                                                                                    "  pm.expect(data).to.have.property(\u0027items\u0027);",
                                                                                    "  pm.expect(data).to.have.property(\u0027address\u0027);",
                                                                                    "  pm.expect(data).to.have.property(\u0027coupon\u0027);",
                                                                                    "  pm.expect(data).to.have.property(\u0027summary\u0027);",
                                                                                    "  pm.expect(data.summary).to.have.property(\u0027payable_total\u0027);",
                                                                                    "});"
                                                                                ]
                                                                   }
                                                    }
                                                ],
                                      "request":  {
                                                      "method":  "POST",
                                                      "header":  [
                                                                     {
                                                                         "key":  "Accept",
                                                                         "value":  "application/json"
                                                                     },
                                                                     {
                                                                         "key":  "Content-Type",
                                                                         "value":  "application/json"
                                                                     }
                                                                 ],
                                                      "body":  {
                                                                   "mode":  "raw",
                                                                   "options":  {
                                                                                   "raw":  {
                                                                                               "language":  "json"
                                                                                           }
                                                                               },
                                                                   "raw":  "{\n  \"address_id\": {{addressId}},\n  \"coupon_code\": \"{{couponCode}}\"\n}"
                                                               },
                                                      "url":  "{{baseUrl}}/cart-checkout/coupon"
                                                  }
                                  },
                                  {
                                      "name":  "4. Complete checkout",
                                      "request":  {
                                                      "method":  "POST",
                                                      "header":  [
                                                                     {
                                                                         "key":  "Accept",
                                                                         "value":  "application/json"
                                                                     },
                                                                     {
                                                                         "key":  "Content-Type",
                                                                         "value":  "application/json"
                                                                     }
                                                                 ],
                                                      "body":  {
                                                                   "mode":  "raw",
                                                                   "options":  {
                                                                                   "raw":  {
                                                                                               "language":  "json"
                                                                                           }
                                                                               },
                                                                   "raw":  "{\n  \"address_id\": {{addressId}},\n  \"delivery_method_id\": {{deliveryMethodId}},\n  \"coupon_code\": \"{{couponCode}}\",\n  \"payment_method_id\": {{paymentMethodId}},\n  \"note\": \"\"\n}"
                                                               },
                                                      "url":  "{{baseUrl}}/checkout/complete"
                                                  }
                                  }
                              ]
                 }
             ]
}
