Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stripe): added new winglib for stripe #279

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/canary.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .github/workflows/pull-request-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
simtools
slack
sns
stripe
tf
tsoa
vite
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/stripe-pull.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions .github/workflows/stripe-release.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .mergify.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ publishing them for you.
| [simtools](./simtools) | [@winglibs/simtools](https://www.npmjs.com/package/@winglibs/simtools) | sim |
| [slack](./slack) | [@winglibs/slack](https://www.npmjs.com/package/@winglibs/slack) | sim, tf-aws |
| [sns](./sns) | [@winglibs/sns](https://www.npmjs.com/package/@winglibs/sns) | awscdk, sim, tf-aws |
| [stripe](./stripe) | [@winglibs/stripe](https://www.npmjs.com/package/@winglibs/stripe) | sim |
| [tf](./tf) | [@winglibs/tf](https://www.npmjs.com/package/@winglibs/tf) | sim, tf-aws |
| [tsoa](./tsoa) | [@winglibs/tsoa](https://www.npmjs.com/package/@winglibs/tsoa) | sim |
| [vite](./vite) | [@winglibs/vite](https://www.npmjs.com/package/@winglibs/vite) | sim, tf-aws |
Expand Down
2 changes: 2 additions & 0 deletions stripe/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
node_modules/
21 changes: 21 additions & 0 deletions stripe/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Wing

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
89 changes: 89 additions & 0 deletions stripe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Stripe

An [Stripe](https://stripe.com) library for Winglang.

> This is an initial version of this library which currently exposes a very small subset of the
> Stripe API.

## Prerequisites

* [winglang](https://winglang.io).
* [Stripe Account](https://stripe.com)
* [Stripe Webhook Secret and Stripe API Secret](https://docs.stripe.com/webhooks)


## Installation

```sh
npm i @winglibs/stripe
```

## Example

```js
bring cloud;
bring "./lib/stripe.w" as Stripe;

let stripeSecretKey = new cloud.Secret(name: "STRIPE_API_TOKEN") as "Stripe Secret";
let webhookSecret = new cloud.Secret(name: "STRIPE_ENDPOINT_SECRET") as "Webhook Secret";
let stripe = new Stripe.Webhook({ webhookSecret: webhookSecret, secretKey: stripeSecretKey }) as "Stripe integration";
boyney123 marked this conversation as resolved.
Show resolved Hide resolved

// Listen to any stripe event
stripe.onEvent("customer.created", inflight (ctx, event) => {
// New customer event triggered
log("Event: {event["type"]}");
});

```

## Usage

```js
new Stripe.Webhook();
```

* `webhookKeySecret` - a `cloud.Secret` with the Stripe webhook secret (required)
* `secretKeySecret` - a `cloud.Secret` with the Stripe secret key (required)

You can also specify clear text values through `webhookKey` and `secretKey`, but make sure not to commit these values to a repository :warning:.

`Webhook()` creates an [API cloud resource](https://www.winglang.io/docs/standard-library/cloud/api) with a `/webhook` endpoint, [Queue](https://www.winglang.io/docs/standard-library/cloud/queue) (with a configured Dead-letter queue) and [Function resource](https://www.winglang.io/docs/standard-library/cloud/function) to handle the registered events.

The `/webhook` receives events from Stripe. The event is verified through HTTP signatures.

The event is then put onto a Queue.

Messages on the queue are then processed by a Function. This function then calls your registered functions.

If messages cannot be processed they are put on the configured DLQ.

Methods:

* `inflight onEvent()` - Register a listener for an event on the API.

### Running locally

Create an .env file in your Wing project and add these values.

```sh
STRIPE_API_TOKEN={API_TOKEN}
STRIPE_ENDPOINT_SECRET={WEBHOOK_API_TOKEN}
```

When running the [Wing Cloud Simulator](https://www.winglang.io/docs/platforms/sim) the secrets are used from the `.env` file.

With the Wing console you can also expose your generated URL using Wing tunnels. This can be valuable for testing your Stripe integrations.

## Roadmap

* [ ] Support the rest of the Stripe API
* [ ] Add more examples
* [ ] Add more tests

## Maintainers

* [David Boyne](https://github.com/boyney123)

## License

Licensed under the [MIT License](/LICENSE).
12 changes: 12 additions & 0 deletions stripe/example.main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bring cloud;
bring "./lib.w" as Stripe;

let secretKeySecret = new cloud.Secret(name: "STRIPE_API_TOKEN") as "Stripe Secret";
let webhookKeySecret = new cloud.Secret(name: "STRIPE_ENDPOINT_SECRET") as "Webhook Secret";
let stripe = new Stripe.Webhook(webhookKeySecret: webhookKeySecret, secretKeySecret: secretKeySecret) as "Stripe integration";

// List of events https://docs.stripe.com/api/events/types
// Make sure you regsiter events in your Stripe Event Destination https://docs.stripe.com/event-destinations
stripe.onEvent("customer.created", inflight (event) => {
log("Event: {event["type"]}");
});
70 changes: 70 additions & 0 deletions stripe/lib.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
bring expect;
bring cloud;
bring http;
bring "./lib.w" as Stripe;

let app = new Stripe.Webhook(secretKey: "super-secret-key", webhookKey: "super-secret-key");

// How can I test this!??
app.onEvent("customer.created", inflight (event) => {
return event;
});


test "app_mention event" {
let endpoint = app.api.url;

let stripeEvent = {
"id": "evt_1PYShZHDPHgalV3DNoBes3PM",
"object": "event",
"api_version": "2023-10-16",
"created": 1720011265,
"data": {
"object": {
"id": "cus_QPHGpm7ooXC4eJ",
"object": "customer",
"address": "null",
"balance": 0,
"created": 1720011265,
"currency": "null",
"default_source": "null",
"delinquent": false,
"description": "null",
"discount": "null",
"email": "null",
"invoice_prefix": "B07B9195",
"invoice_settings": {
"custom_fields": "null",
"default_payment_method": "null",
"footer": "null",
"rendering_options": "null"
},
"livemode": false,
"metadata": {},
"name": "4444",
"phone": "null",
"preferred_locales": [],
"shipping": "null",
"tax_exempt": "none",
"test_clock": "null"
}
},
"livemode": false,
"pending_webhooks": 1,
"request": {
"id": "req_HyMlRgcRZ7MMri",
"idempotency_key": "8ccbb61e-92fb-4220-8aab-f77df325ed52"
},
"type": "customer.created"
};

let res = http.post("{endpoint}/webhook", {
body: Json.stringify(stripeEvent),
headers: {
"Stripe-Signature": "t=1720018938,v1=c7d1c0bda8754d27077ff3cb93119e152f74e85ef74f0c66a8ffe41c3ce77171"
}
});

expect.equal(res.status, 202);

}
Loading
Loading