const { DynamoDB } = require ( 'aws-sdk' ) ; export { } ; exports . handler = async function ( event ) { console . log ( "request:" , JSON . stringify ( event , undefined , 2 ) ) ; let flightBookingID = '' ; if ( typeof event . ReserveFlightResult !== 'undefined' ) { flightBookingID = event . ReserveFlightResult . Payload . booking_id ; } let hotelBookingID = '' ; if ( typeof event . ReserveHotelResult !== 'undefined' ) { hotelBookingID = event . ReserveHotelResult . Payload . booking_id ; } let paymentID = hashCode ( '' + event . trip_id + hotelBookingID + flightBookingID ) ; if ( event . run_type === 'failPayment' ) { throw new Error ( 'Failed to book the flights' ) ; } const dynamo = new DynamoDB ( ) ; var params = { TableName : process . env . TABLE_NAME , Item : { 'pk' : { S : event . trip_id } , 'sk' : { S : 'PAYMENT#' + paymentID } , 'type' : { S : 'Payment' } , 'trip_id' : { S : event . trip_id } , 'id' : { S : paymentID } , 'amount' : { S : "450.00" } , 'currency' : { S : "USD" } , 'transaction_status' : { S : "confirmed" } } } ; let result = await dynamo . putItem ( params ) . promise ( ) . catch ( ( error ) => { throw new Error ( error ) ; } ) ; console . log ( 'Payment Taken Successfully:' ) ; console . log ( result ) ; return { status : "ok" , payment_id : paymentID } ; } ; function hashCode ( s ) { let h ; for ( let i = 0 ; i < s . length ; i ++ ) { h = Math . imul ( 31 , h ) + s . charCodeAt ( i ) | 0 ; } return '' + Math . abs ( h ) ; }