{"id":1184,"date":"2020-02-21T11:28:28","date_gmt":"2020-02-21T10:28:28","guid":{"rendered":"https:\/\/blog.besharp.it\/?p=1184"},"modified":"2021-03-24T12:44:04","modified_gmt":"2021-03-24T11:44:04","slug":"how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda","status":"publish","type":"post","link":"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/","title":{"rendered":"How to create a serverless payment system using Stripe and AWS Lambda"},"content":{"rendered":"

We are in the online shopping epoch and the implementation of the online payment methods into cloud-native apps<\/strong> is becoming an increasing need for the market.<\/span><\/p>\n

As we can guess, managing payments into our business flow requires a secure and reliable infrastructure, that can guarantee the privacy<\/strong> and the consistency of data and transactions<\/strong>.<\/span><\/p>\n

The integration to more and more numerous payment circuits involves a considerable effort in development and maintainability.<\/span><\/p>\n

Today we are introducing to you a fully serverless solution based on the famous service Stripe<\/a>, a payment middleware<\/strong> that provides to its users a back office dashboard and a REST interface.<\/span><\/p>\n

A fully-managed service – like Stripe is – can help, but each payment flow has its own features depending on different business requirements. <\/span>It is strongly recommended to write server-side code in order to keep this information secret so that it is possible to avoid any sensitive data spread.<\/span><\/p>\n

As a scalable and fully-managed<\/strong> service, Stripe allows us to build high performing applications. Anyway, to get the most out of this service it is important to build an equally scalable and agile back-end able to adapt the best way possible. To do so, Serverless technologies<\/strong> come to help.\u00a0<\/span><\/p>\n

In particular, in this article, we are focusing on the use of AWS Lambda<\/strong>, a serverless computing service provided by Amazon web services.<\/span><\/p>\n

For the beginners, Lambdas are stateless serverless functions. The developer is able to work in an environment where he can write code without worry about host hardware and you pay only what you use.<\/span><\/p>\n

Getting started with Stripe<\/h2>\n

Let\u2019s deep dive into Stripe. How to use it?\u00a0<\/span><\/p>\n

First of all, sign up to Stripe.<\/a><\/span><\/p>\n

Signing up is free and you\u2019ll pay only for what you use. For pricing details check this page<\/a>.\u00a0<\/span><\/p>\n

\"\"<\/p>\n

In this article, we are not going to explain every single feature of Stripe (form more details, see the official documentation<\/a><\/span>). Instead, we are going to integrate an AWS Lambda serverless application with the API of our just-created Stripe account.<\/span><\/p>\n

One of the main components of Stripe is the dashboard<\/strong>: it offers users the possibility to create and manage resources like subscriptions and products.<\/span><\/p>\n

\"\"<\/p>\n

As you can see from the dashboard, we can choose between two different kinds of APIs:<\/strong><\/span><\/p>\n

keypair test APIs<\/strong> through which you will be able to create test data (note: they will be visible only if the “Viewing test data” is checked) and live API key<\/strong>, used to create real transactions (usually for production environment).<\/span><\/p>\n

Both of the keys must be stored in a secure place.<\/strong> The most reliable tool to store those kinds of information is AWS Secret Manager<\/a>, a key-value database used only to store credentials, access keys or other kinds of data that can be considered sensible.<\/span><\/p>\n

\"\"<\/p>\n

To save new information, click on “Store a new secret” and select the secret’s type<\/span><\/p>\n

\"\"<\/p>\n

In this case, we need to create only raw key-value data without any kind of integration.<\/span><\/p>\n

And now let’s start with Lambda!<\/span><\/h2>\n

Finally, it’s time to create the Lambda function<\/strong> which will manage the payment.<\/span><\/p>\n

In this example, we will use Python 3.6<\/strong><\/span><\/p>\n

NOTE: <\/b>Be sure to have attached to the Lambda a LambdaLayer containing pip, stripe, and boto3 packages.<\/span><\/p>\n

Through the algorithm implemented in the example below, we are creating a new subscription instance<\/strong> attached to a user, starting from a subscription model created previously from the dashboard. To use this code you will need the secret manager ARN and plan_id.<\/span><\/p>\n

import stripe\r\nimport boto3\r\nimport json\r\n\r\nclient = boto3.client('secretsmanager')\r\nkeys = json.loads(client.get_secret_value(\r\n  SecretId = 'arn:aws:secretsmanager:eu-central-1:169954988972:secret:stripe-AiFSZg',\r\n)['SecretString'])\r\npublic_key = keys['stripe-public']\r\nsecret_key = keys['stripe-secret']\r\nstripe.api_key = secret_key\r\n\r\ndef signup(event, context):\r\n  card_data = event.get('cardData')\r\n  email = event.get('email')\r\n  attributes = event.get('attributes')\r\n  create_stripe_customer(email, attributes, card_data)\r\n  return event\r\n\r\ndef create_stripe_customer(email, user_data, payment_info):\r\n  customer_id = stripe.Customer.create(email = email, metadata = user_data)['id']\r\n  payment_method_id = create_payment_method(payment_info)\r\n  stripe.PaymentMethod.attach(\r\n    payment_method_id,\r\n    customer = customer_id\r\n  )\r\n  return {\r\n    \"customer_id\": customer_id,\r\n    \"plan\": create_stripe_plan(customer_id)\r\n  }\r\n\r\ndef create_payment_method(payment_info):\r\n  return stripe.PaymentMethod.create(\r\n    type = \"card\",\r\n    card = {\r\n      \"number\": payment_info.get('cardNumber'),\r\n      \"exp_month\": payment_info.get('expirationMonth'),\r\n      \"exp_year\": payment_info.get('expirationYear'),\r\n      \"cvc\": payment_info.get('ccv'),\r\n    }).get('id')\r\n\r\ndef create_stripe_plan(customer_id):\r\n  return stripe.Subscription.create(\r\n    customer = customer_id,\r\n    items = [{\r\n      \"plan\": \"plan_idxxxxxxx\"\r\n    }]\r\n  ).get(\"id\")\r\n<\/pre>\n

As you can see, this Lambda gets the values from the event payload; it will change based on the service integrated to the Lambda.<\/p>\n

There are several ways to use your Lambda: it can be used as a trigger in an SQS queue, as a resource in an API Gateway or it can invoke directly from your client.<\/p>\n

Congratulation!<\/strong><\/p>\n

By following these steps, you have successfully created your serverless payment system. Now you are ready to handle millions of users containing and optimizing infrastructural costs. It\u2019s time to try it in a production environment!<\/p>\n

Still curious about Stripe or AWS Lambda? Contact us<\/a> to have a chat with our Cloud Expert.<\/p>\n

See you in the next article!<\/p>\n","protected":false},"excerpt":{"rendered":"

We are in the online shopping epoch and the implementation of the online payment methods into cloud-native apps is becoming […]<\/p>\n","protected":false},"author":10,"featured_media":1182,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[478],"tags":[264,268,304],"yoast_head":"\nHow to create a serverless payment system using Stripe and AWS Lambda - Proud2beCloud Blog<\/title>\n<meta name=\"description\" content=\"Let's build a serverless payment solution based on the famous service Stripe and AWS Lambda.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Building a serverless payment system using Stripe and AWS Lambda\" \/>\n<meta property=\"og:description\" content=\"Let's build a serverless payment solution based on the famous service Stripe and AWS Lambda.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/\" \/>\n<meta property=\"og:site_name\" content=\"Proud2beCloud Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-21T10:28:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-03-24T11:44:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.besharp.it\/wp-content\/uploads\/2020\/02\/copertine-blog-Stripe-26-26.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1668\" \/>\n\t<meta property=\"og:image:height\" content=\"1251\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Paolo Di Ciaula\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Building a serverless payment system using Stripe and AWS Lambda\" \/>\n<meta name=\"twitter:description\" content=\"Let's build a serverless payment solution based on the famous service Stripe and AWS Lambda.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/blog.besharp.it\/wp-content\/uploads\/2020\/02\/copertine-blog-Stripe-26-26.png\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paolo Di Ciaula\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/\",\"url\":\"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/\",\"name\":\"How to create a serverless payment system using Stripe and AWS Lambda - Proud2beCloud Blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.besharp.it\/#website\"},\"datePublished\":\"2020-02-21T10:28:28+00:00\",\"dateModified\":\"2021-03-24T11:44:04+00:00\",\"author\":{\"@id\":\"https:\/\/blog.besharp.it\/#\/schema\/person\/a984367e499bce5e12199e34d2c4181c\"},\"description\":\"Let's build a serverless payment solution based on the famous service Stripe and AWS Lambda.\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.besharp.it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to create a serverless payment system using Stripe and AWS Lambda\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.besharp.it\/#website\",\"url\":\"https:\/\/blog.besharp.it\/\",\"name\":\"Proud2beCloud Blog\",\"description\":\"il blog di beSharp\",\"alternateName\":\"Proud2beCloud Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.besharp.it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.besharp.it\/#\/schema\/person\/a984367e499bce5e12199e34d2c4181c\",\"name\":\"Paolo Di Ciaula\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.besharp.it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7a3395eed9ec6267a1c136c3cf0a3903?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7a3395eed9ec6267a1c136c3cf0a3903?s=96&d=mm&r=g\",\"caption\":\"Paolo Di Ciaula\"},\"description\":\"DevOps Engineer, Frontend Developer, and Mobile App Developer @ beSharp. I divide my free time between good music, development, and... beer (sometimes mixed for better results ;D)\",\"url\":\"https:\/\/blog.besharp.it\/author\/paolo-di-ciaula\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to create a serverless payment system using Stripe and AWS Lambda - Proud2beCloud Blog","description":"Let's build a serverless payment solution based on the famous service Stripe and AWS Lambda.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/","og_locale":"en_US","og_type":"article","og_title":"Building a serverless payment system using Stripe and AWS Lambda","og_description":"Let's build a serverless payment solution based on the famous service Stripe and AWS Lambda.","og_url":"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/","og_site_name":"Proud2beCloud Blog","article_published_time":"2020-02-21T10:28:28+00:00","article_modified_time":"2021-03-24T11:44:04+00:00","og_image":[{"width":1668,"height":1251,"url":"https:\/\/blog.besharp.it\/wp-content\/uploads\/2020\/02\/copertine-blog-Stripe-26-26.png","type":"image\/png"}],"author":"Paolo Di Ciaula","twitter_card":"summary_large_image","twitter_title":"Building a serverless payment system using Stripe and AWS Lambda","twitter_description":"Let's build a serverless payment solution based on the famous service Stripe and AWS Lambda.","twitter_image":"https:\/\/blog.besharp.it\/wp-content\/uploads\/2020\/02\/copertine-blog-Stripe-26-26.png","twitter_misc":{"Written by":"Paolo Di Ciaula","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/","url":"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/","name":"How to create a serverless payment system using Stripe and AWS Lambda - Proud2beCloud Blog","isPartOf":{"@id":"https:\/\/blog.besharp.it\/#website"},"datePublished":"2020-02-21T10:28:28+00:00","dateModified":"2021-03-24T11:44:04+00:00","author":{"@id":"https:\/\/blog.besharp.it\/#\/schema\/person\/a984367e499bce5e12199e34d2c4181c"},"description":"Let's build a serverless payment solution based on the famous service Stripe and AWS Lambda.","breadcrumb":{"@id":"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.besharp.it\/how-to-create-a-serverless-payment-system-using-stripe-and-aws-lambda\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.besharp.it\/"},{"@type":"ListItem","position":2,"name":"How to create a serverless payment system using Stripe and AWS Lambda"}]},{"@type":"WebSite","@id":"https:\/\/blog.besharp.it\/#website","url":"https:\/\/blog.besharp.it\/","name":"Proud2beCloud Blog","description":"il blog di beSharp","alternateName":"Proud2beCloud Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.besharp.it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.besharp.it\/#\/schema\/person\/a984367e499bce5e12199e34d2c4181c","name":"Paolo Di Ciaula","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.besharp.it\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7a3395eed9ec6267a1c136c3cf0a3903?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7a3395eed9ec6267a1c136c3cf0a3903?s=96&d=mm&r=g","caption":"Paolo Di Ciaula"},"description":"DevOps Engineer, Frontend Developer, and Mobile App Developer @ beSharp. I divide my free time between good music, development, and... beer (sometimes mixed for better results ;D)","url":"https:\/\/blog.besharp.it\/author\/paolo-di-ciaula\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/posts\/1184"}],"collection":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/comments?post=1184"}],"version-history":[{"count":0,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/posts\/1184\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/media\/1182"}],"wp:attachment":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/media?parent=1184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/categories?post=1184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/tags?post=1184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}