From 5824e377ea2d92437094c4530959004edfc42251 Mon Sep 17 00:00:00 2001 From: Tony Cordova Date: Wed, 1 May 2019 13:33:48 -0700 Subject: [PATCH] optimize payload-builder.js --- .ask/config | 28 +++++++- lambda/custom/config.js | 47 +++++++------ lambda/custom/index.js | 4 +- lambda/custom/package.json | 2 +- lambda/custom/payload-builder.js | 116 ++++++++++++++----------------- 5 files changed, 109 insertions(+), 88 deletions(-) diff --git a/.ask/config b/.ask/config index 8aad80e..0148ac4 100644 --- a/.ask/config +++ b/.ask/config @@ -1,11 +1,35 @@ { "deploy_settings": { "default": { - "skill_id": "", + "skill_id": "amzn1.ask.skill.7b00fd1c-b906-4dc6-8d9c-c4cbe1ef69fc", "was_cloned": false, "merge": { "manifest": {} + }, + "resources": { + "manifest": { + "eTag": "3517b94184e7b2fce7b7f48ab23b2b92" + }, + "interactionModel": { + "en-US": { + "eTag": "18f1e29eab906e15a7e9db8f36fb48c4" + } + }, + "lambda": [ + { + "alexaUsage": [ + "custom/default" + ], + "arn": "arn:aws:lambda:us-east-1:859669077786:function:no-nicks", + "awsRegion": "us-east-1", + "codeUri": "lambda/custom", + "functionName": "no-nicks", + "handler": "index.handler", + "revisionId": "ef2f82bd-b5c9-4052-9be9-cfed3fe6eddb", + "runtime": "nodejs8.10" + } + ] } } } -} \ No newline at end of file +} diff --git a/lambda/custom/config.js b/lambda/custom/config.js index 05ab0a2..f01f644 100644 --- a/lambda/custom/config.js +++ b/lambda/custom/config.js @@ -1,5 +1,5 @@ /** - To run the skill, the minimum values you need configure are: sellerId, sandboxCustomerEmailId, and bucketName + To run this skill, the minimum values you need configure are in INIT: bucketName, sandboxCustomerEmailId, and sellerId A detailed list of attribute descriptions can be found here: https://developer.amazon.com/docs/amazon-pay/amazon-pay-apis-for-alexa.html @@ -9,38 +9,42 @@ const utilities = require( 'utilities' ); -// Setup & Charge Payload attributes -const GENERAL = { - VERSION: '2.0', // Required; - needAmazonShippingAddress: true, // Optional; Must be boolean +// You must specify these values to run this skill +const INIT = { + bucketName: 'no-nicks', // Required; Used for skill state management + sandboxCustomerEmailId: 'tcordov+pay.sandbox.buyer.2@amazon.com', // Required*; If sandboxMode equals true; + sellerId: 'AVX6ERM5QT1ZB', // Required; Amazon Pay seller ID +}; + +// These attributes are used globally across US, EU, and JP +const GLOBAL = { + amount: '0.01', // Required; Max $150,000.00 USD; Intentionally set to $.01 in the demo for testing purposes paymentAction: 'AuthorizeAndCapture', // Required; 'Authorize' or 'AuthorizeAndCapture' + sandboxMode: true, // Required*; Must be true for sandbox testing; Must be false to submit to certification & production + version: '2', // Required; + setupType: 'SetupAmazonPayRequest', // Required; + chargeType: 'ChargeAmazonPayRequest', // Required; + needAmazonShippingAddress: true, // Optional; Must be boolean transactionTimeout: 0, // Optional; The default and recommended value for Alexa transactions is 0 - bucketName: 'INSERT-YOUR-S3-BUCKET-NAME' // Required; Used for skill state management }; +// These attributes will change based on your region ( US, EU, or JP ) const REGIONAL = { 'en-US': { - sellerId: 'INSERT-YOUR-AMAZON-PAY-SELLER-ID', // Required; Amazon Pay seller ID - checkoutLanguage: 'en_US', // Optional; US must be en_US countryOfEstablishment: 'US', // Required; - ledgerCurrency: 'USD', // Required; - sandboxMode: true, // Optional; Must be false for certification & production; Must be true for sandbox testing - sandboxCustomerEmailId: 'INSERT-YOUR-SANDBOX-EMAIL-ADDRESS', // Optional; Required if sandboxMode equals true; Must setup Amazon Pay test account first - sellerAuthorizationNote: utilities.getSimulationString( '' ), // Optional; Max 255 chars - softDescriptor: 'No Nicks', // Optional; Max 16 chars; This value is visible on customers credit card statements - amount: '0.01', // Required; Max $150,000.00 USD; Intentionally set to $.01 in the demo for testing purposes. currencyCode: 'USD', // Required; - - // SELLER ORDER ATTRIBUTES + ledgerCurrency: 'USD', // Required; + checkoutLanguage: 'en_US', // Optional; US must be en_US customInformation: '', // Optional; Max 1024 chars + sellerAuthorizationNote: utilities.getSimulationString( '' ), // Optional; Max 255 chars; In sandbox mode you can pass simulation strings. See utilities.js sellerNote: 'Thanks for shaving with No Nicks', // Optional; Max 1024 chars, visible on confirmation mails to buyers sellerStoreName: 'No Nicks', // Optional; Documentation calls this out as storeName not sellerStoreName + softDescriptor: 'No Nicks', // Optional; Max 16 chars; This value is visible on customers credit card statements } }; /** - The following strings DO NOT interact with Amazon Pay - They are here to augment the skill + The following strings DO NOT interact with Amazon Pay, they are here to augment the skill Order Summary, Order Confirmation, Cancel and Refund Custom Intents are required for certification: https://developer.amazon.com/docs/amazon-pay/certify-skill-with-amazon-pay.html @@ -110,7 +114,6 @@ const REGIONAL = { The following strings are used to output errors to test the skill **/ - // ERROR RESPONSE STRINGS const scope = 'payments:autopay_consent'; // Required; Used request permissions for Amazon Pay const enablePermission = 'To make purchases in this skill, you need to enable Amazon Pay and turn on voice purchasing. To help, I sent a card to your Alexa app.'; @@ -126,8 +129,10 @@ const REGIONAL = { module.exports = { - 'GENERAL': GENERAL, - 'REGIONAL': REGIONAL, + // PAYLOAD ATTRIBUTES + 'INIT': INIT, + 'GLOBAL': GLOBAL, + 'REGIONAL': REGIONAL, // INTENT RESPONSE STRINGS 'launchRequestWelcomeTitle': launchRequestWelcomeTitle, diff --git a/lambda/custom/index.js b/lambda/custom/index.js index 2cb644f..3b9b5da 100644 --- a/lambda/custom/index.js +++ b/lambda/custom/index.js @@ -523,7 +523,7 @@ function amazonPayCharge ( handlerInput ) { const sellerOrderId = utilities.generateRandomString( 6 ); const locale = handlerInput.requestEnvelope.request.locale; const token = utilities.generateRandomString( 12 ); - const amount = config.REGIONAL[locale].amount; + const amount = config.GLOBAL.amount; // Set the Charge payload and send the request directive const chargePayload = payloadBuilder.chargePayload(billingAgreementId, authorizationReferenceId, sellerOrderId, amount, locale); @@ -621,7 +621,7 @@ exports.handler = askSDK.SkillBuilders .addRequestInterceptors( PersistenceRequestInterceptor ) .addResponseInterceptors( PersistenceResponseInterceptor ) .withPersistenceAdapter( persistence = new s3Adapter( - { bucketName: config.GENERAL.bucketName } ) ) + { bucketName: config.INIT.bucketName } ) ) .addErrorHandlers( ErrorHandler ) .lambda( ); \ No newline at end of file diff --git a/lambda/custom/package.json b/lambda/custom/package.json index d9c23f7..4e8df26 100644 --- a/lambda/custom/package.json +++ b/lambda/custom/package.json @@ -9,4 +9,4 @@ "ask-sdk-core": "^2.4.0", "ask-sdk-s3-persistence-adapter": "^2.4.0" } -} \ No newline at end of file +} diff --git a/lambda/custom/payload-builder.js b/lambda/custom/payload-builder.js index 88ceb82..1f94328 100644 --- a/lambda/custom/payload-builder.js +++ b/lambda/custom/payload-builder.js @@ -1,84 +1,76 @@ -const utilities = require( 'utilities' ); -let config = require( 'config' ); +const utilities = require( 'utilities' ); +const config = require( 'config' ); +const initConfig = config.INIT; +const globalConfig = config.GLOBAL; -const setupPayloadVersioning = { - type: 'SetupAmazonPayRequest', - version: '2' -}; +function setupPayload( language ) { + const regionalConfig = config.REGIONAL[ language ]; -const processPayloadVersioning = { - type: 'ChargeAmazonPayRequest', - version: '2' -}; - -var setupPayload = function( language ) { - console.log( language ); - const regionalConfig = config.REGIONAL[ language ]; - const generalConfig = config.GENERAL; - var payload = { - '@type': setupPayloadVersioning.type, - '@version': setupPayloadVersioning.version, - 'sellerId': regionalConfig.sellerId, - 'countryOfEstablishment': regionalConfig.countryOfEstablishment, - 'ledgerCurrency': regionalConfig.ledgerCurrency, - 'checkoutLanguage': language, - 'sandboxCustomerEmailId': regionalConfig.sandboxCustomerEmailId, - 'sandboxMode': regionalConfig.sandboxMode, - 'needAmazonShippingAddress': generalConfig.needAmazonShippingAddress, + let payload = { + '@type': globalConfig.setupType, + '@version': globalConfig.version, + 'sellerId': initConfig.sellerId, + 'countryOfEstablishment': regionalConfig.countryOfEstablishment, + 'ledgerCurrency': regionalConfig.ledgerCurrency, + 'checkoutLanguage': language, + 'sandboxCustomerEmailId': initConfig.sandboxCustomerEmailId, + 'sandboxMode': globalConfig.sandboxMode, + 'needAmazonShippingAddress': globalConfig.needAmazonShippingAddress, 'billingAgreementAttributes': { - '@type': 'BillingAgreementAttributes', - '@version': '2', - 'sellerNote': regionalConfig.sellerNote, - 'platformId': generalConfig.platformId, + '@type': 'BillingAgreementAttributes', + '@version': globalConfig.version, + 'sellerNote': regionalConfig.sellerNote, + 'platformId': globalConfig.platformId, 'sellerBillingAgreementAttributes': { - '@type': 'SellerBillingAgreementAttributes', - '@version': '2', - //'sellerBillingAgreementId': SOME RANDOM STRING, - 'storeName': generalConfig.sellerStoreName, - 'customInformation': regionalConfig.customInformation + '@type': 'SellerBillingAgreementAttributes', + '@version': globalConfig.version, + 'sellerBillingAgreementId': utilities.generateRandomString( 6 ), + 'storeName': globalConfig.sellerStoreName, + 'customInformation': regionalConfig.customInformation } } }; return payload; -}; -var chargePayload = function( billingAgreementId, authorizationReferenceId, sellerOrderId, amount, language ) { +} + +function chargePayload ( billingAgreementId, authorizationReferenceId, sellerOrderId, amount, language ) { + const regionalConfig = config.REGIONAL[ language ]; - const regionalConfig = config.REGIONAL[ language ]; - const generalConfig = config.GENERAL; - var payload = { - '@type': processPayloadVersioning.type, - '@version': processPayloadVersioning.version, - 'sellerId': regionalConfig.sellerId, - 'billingAgreementId': billingAgreementId, - 'paymentAction': generalConfig.paymentAction, + let payload = { + '@type': globalConfig.chargeType, + '@version': globalConfig.version, + 'sellerId': initConfig.sellerId, + 'billingAgreementId': billingAgreementId, + 'paymentAction': globalConfig.paymentAction, 'authorizeAttributes': { '@type': 'AuthorizeAttributes', - '@version': '2', - 'authorizationReferenceId': authorizationReferenceId, + '@version': globalConfig.version, + 'authorizationReferenceId': authorizationReferenceId, 'authorizationAmount': { - '@type': 'Price', - '@version': '2', - 'amount': amount.toString( ), - 'currencyCode': regionalConfig.ledgerCurrency + '@type': 'Price', + '@version': globalConfig.version, + 'amount': amount, + 'currencyCode': regionalConfig.ledgerCurrency }, - 'transactionTimeout': generalConfig.transactionTimeout, - 'sellerAuthorizationNote': regionalConfig.sellerAuthorizationNote, // util.getSimulationString('AmazonRejected'), - 'softDescriptor': regionalConfig.softDescriptor + 'transactionTimeout': globalConfig.transactionTimeout, + 'sellerAuthorizationNote': regionalConfig.sellerAuthorizationNote, + 'softDescriptor': regionalConfig.softDescriptor }, 'sellerOrderAttributes': { - '@type': 'SellerOrderAttributes', - '@version': '2', - // 'sellerOrderId': sellerOrderId, - 'storeName': regionalConfig.sellerStoreName, - 'customInformation': regionalConfig.customInformation, - 'sellerNote': regionalConfig.sellerNote + '@type': 'SellerOrderAttributes', + '@version': globalConfig.version, + 'sellerOrderId': sellerOrderId, + 'storeName': regionalConfig.sellerStoreName, + 'customInformation': regionalConfig.customInformation, + 'sellerNote': regionalConfig.sellerNote } }; + return payload; -}; +} module.exports = { - 'setupPayload': setupPayload, - 'chargePayload': chargePayload + 'setupPayload': setupPayload, + 'chargePayload': chargePayload }; \ No newline at end of file