@@ -5,7 +5,7 @@ const { HTTPError } = require('./http-error.js');
55const { calculateShipping, calculateCardFee, calculateTaxes, getTotal } = require ( './stripe-utils.js' ) ;
66const { currency } = require ( './stripe-consts.js' ) ;
77
8- async function getDisplayItems ( cart , { signal } = { } ) {
8+ async function createDisplayItems ( cart , { signal } = { } ) {
99 const { getProducts } = require ( './store.js' ) ;
1010 const products = await getProducts ( cart . map ( ( { id } ) => id , { signal } ) ) ;
1111
@@ -35,56 +35,8 @@ async function getDisplayItems(cart, { signal } = {}) {
3535 }
3636 } ) ;
3737}
38- async function getPaymentRequest ( displayItems ) {
39- if ( ! Array . isArray ( displayItems ) || displayItems . length === 0 ) {
40- throw new TypeError ( '`displayItems` invalid' ) ;
41- } else {
42- const req = {
43- details : {
44- displayItems,
45- modifiers : {
46- additionalDisplayItems : [ {
47- label : 'Taxes' ,
48- amount : {
49- value : await calculateTaxes ( { details : { displayItems } } ) ,
50- currency,
51- }
52- } , {
53- label : 'Shipping' ,
54- amount : {
55- value : await calculateShipping ( { displayItems } ) ,
56- currency,
57- } ,
58- } ]
59- }
60- } ,
61- options : {
62- requestShipping : true ,
63- }
64- } ;
65-
66- req . details . modifiers . additionalDisplayItems . push ( {
67- label : 'Processing Fee' ,
68- amount : {
69- value : await calculateCardFee ( req ) ,
70- currency,
71- }
72- } ) ;
73-
74- req . details . total = {
75- label : 'Total' ,
76- amount : {
77- value : getTotal ( req ) ,
78- currency,
79- }
80- } ;
81-
82- return req ;
83- }
84- }
85- async function createPaymentRequest ( query ) {
86- const { createDisplayItems } = require ( './store.js' ) ;
87- const displayItems = await createDisplayItems ( query ) ;
38+ async function createPaymentRequest ( items , { signal } = { } ) {
39+ const displayItems = await createDisplayItems ( items , { signal } ) ;
8840
8941 if ( ! Array . isArray ( displayItems ) || displayItems . length === 0 ) {
9042 throw new TypeError ( '`displayItems` invalid' ) ;
@@ -96,7 +48,7 @@ async function createPaymentRequest(query) {
9648 additionalDisplayItems : [ {
9749 label : 'Taxes' ,
9850 amount : {
99- value : await calculateTaxes ( { displayItems } ) ,
51+ value : await calculateTaxes ( { details : { displayItems } } ) ,
10052 currency,
10153 }
10254 } , {
@@ -137,15 +89,15 @@ exports.handler = async function(event) {
13789 try {
13890 switch ( event . httpMethod ) {
13991 case 'GET' :
140- if ( typeof event . queryStringParameters . query !== 'string' ) {
141- throw new HTTPError ( 'Missing required query param' , { status : 400 } ) ;
92+ if ( typeof event . queryStringParameters . id !== 'string' ) {
93+ throw new HTTPError ( 'Missing required id param' , { status : 400 } ) ;
14294 } else {
143- const req = await createPaymentRequest ( event . queryStringParameters . query ) ;
144-
95+ const { getOrder } = require ( './stripe-utils.js' ) ;
96+ const { paymentRequest , paymentIntent } = await getOrder ( event . queryStringParameters . id ) ;
14597 return {
14698 statusCode : 200 ,
14799 headers,
148- body : JSON . stringify ( req ) ,
100+ body : JSON . stringify ( { paymentRequest , paymentIntent } ) ,
149101 } ;
150102 }
151103 case 'POST' :
@@ -157,8 +109,7 @@ exports.handler = async function(event) {
157109 if ( ! Array . isArray ( items ) || items . length === 0 ) {
158110 throw new HTTPError ( 'Invalid request body' , { status : 400 } ) ;
159111 } else {
160- const displayItems = await getDisplayItems ( items ) ;
161- const paymentRequest = await getPaymentRequest ( displayItems ) ;
112+ const paymentRequest = await createPaymentRequest ( items ) ;
162113 const { createPaymentIntent, createOrder } = require ( './stripe-utils.js' ) ;
163114 const paymentIntent = await createPaymentIntent ( paymentRequest . details . total . amount . value ) ;
164115 paymentRequest . details . id = paymentIntent . id ;
@@ -176,6 +127,7 @@ exports.handler = async function(event) {
176127 }
177128 } catch ( err ) {
178129 console . error ( err ) ;
130+
179131 if ( err instanceof HTTPError ) {
180132 return err . send ( { Options : methods . join ( ',' ) } ) ;
181133 } else {
0 commit comments