in src/aws-lambda/alexa-skill-lambda/alexa-skill-lambda.py [0:0]
def handle(self, handler_input):
"""
Handle this intent. Results are provided using response_builder.
Args:
handler_input (HandlerInput): The handler_input dict used to call the intent handler.
Returns:
Response
"""
logger.info(f"Calling AmazonPaySetupResponseHandler with input "
f"{json.dumps(handler_input.request_envelope.to_dict(), default=str, indent=2)}")
action_response_payload = handler_input.request_envelope.request.payload
action_response_status_code = handler_input.request_envelope.request.status.code
correlation_token = handler_input.request_envelope.request.token
if int(action_response_status_code) != 200:
message = handler_input.request_envelope.request.status.message
logstr = f"Not an OK return status from Amazon Pay Setup: {action_response_status_code} " \
f"with payload {action_response_payload} and message {message} "
speak_output = f"There was a problem with Amazon Pay Setup: {message} "
try:
speak_output += ' More detail: ' + action_response_payload.error_message
logstr += ' More detail: ' + action_response_payload.error_message
except:
pass
logger.error(logstr)
return (
handler_input.response_builder
.speak(speak_output)
.set_should_end_session(True)
.response
)
if len(AMAZON_PAY_MERCHANT_ID) == 0:
speak_output = "This demo has no merchant set up! We hope you had fun."
return (
handler_input.response_builder
.speak(speak_output)
.set_should_end_session(True)
.response
)
else:
# SetupAmazonPayResult()
billing_agreement_details = action_response_payload['billingAgreementDetails']
billing_agreement_id = billing_agreement_details['billingAgreementId']
# Because we handed over to Amazon Pay we lost our session and with it the attributes, but Pay allows
# us to send a token, which we used to save these. Alternatively, we could have saved them in the backend,
# keyed by, for example, seller_billing_agreement_id (sellerBillingAgreementId).
handler_input.attributes_manager.session_attributes = json.loads(correlation_token)
basket_total = get_cart_total(handler_input)
cart = get_cart(handler_input)
if basket_total == 0:
return (
handler_input.response_builder
.speak('Your basket is empty. Thank you for playing!')
.set_should_end_session(True)
.response
)
"""If we wanted to we could add more information to our charge:""
seller_order_attributes = SellerOrderAttributes(
version="2",
seller_order_id=user_details['username'] + '-' + get_basket_id(handler_input),
store_name="Retail Demo Store",
custom_information="A Demo Transaction For Retail Demo Store",
seller_note="Congratulations on your purchase via Alexa and Amazon Pay at the C-Store demo!"
)
"""
authorization_amount = Price(
version="2",
amount=f"{basket_total:0.2f}",
currency_code="USD"
)
authorize_attributes = AuthorizeAttributes(
version="2",
authorization_reference_id=cart['id'],
authorization_amount=authorization_amount,
seller_authorization_note="Retail Demo Store Sandbox Transaction",
)
payment_action = PaymentAction('AuthorizeAndCapture')
charge_request = ChargeAmazonPayRequest(
version="2",
seller_id=AMAZON_PAY_MERCHANT_ID,
billing_agreement_id=billing_agreement_id,
payment_action=payment_action,
authorize_attributes=authorize_attributes,
# This is where we would add extra information: seller_order_attributes=seller_order_attributes
)
charge_directive = SendRequestDirective(
name='Charge',
token=correlation_token,
payload=charge_request
)
return (
handler_input.response_builder
.add_directive(charge_directive)
.set_should_end_session(True)
.response
)