payment.js 656 B

123456789101112131415161718192021222324252627282930313233343536
  1. const router = require('express').Router()
  2. const moment = require('moment')
  3. const SHIPMENT = {
  4. normal: {
  5. price: 13.98,
  6. days: 7
  7. },
  8. fast: {
  9. price: 49.98,
  10. days: 3
  11. }
  12. }
  13. function shipmentPrice (shipmentOption) {
  14. let estimated = moment().add(shipmentOption.days, 'd').format('dddd MMMM Do')
  15. return {
  16. estimated,
  17. price: shipmentOption.price
  18. }
  19. }
  20. router.post('/shipment', (req, res) => {
  21. let shipment;
  22. if (req.body.shipment === 'normal') {
  23. shipment = shipmentPrice(SHIPMENT.normal)
  24. } else {
  25. shipment = shipmentPrice(SHIPMENT.fast)
  26. }
  27. res.json({
  28. success: true,
  29. shipment
  30. })
  31. })
  32. module.exports = router