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

Support Fatsoma payment process #1

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6b23c00
Add Webhook Endpoints API
biinari May 5, 2021
f92f63d
Fix LOCALSTRIPE_SOURCE when not last script in page
biinari May 6, 2021
a95b869
Accounts: implement Account resource
biinari May 6, 2021
6598891
Server: Redirect to remove trailing slash from URL
biinari May 6, 2021
278defa
Resources: add details to bad request errors
biinari May 6, 2021
07d6b3e
fix(javascript): use normal function for Stripe
biinari May 6, 2021
7757bf6
feat(js): Implement paymentRequest
biinari May 7, 2021
63a2a54
feat(js Element): Support different card* types
biinari May 7, 2021
dfac2b5
feat(js Element): Add off()
biinari May 7, 2021
fd47d85
feat(js Element): Add blur, focus, clear, update methods
biinari May 7, 2021
1903cae
feat(js Element): Pass change event
biinari May 7, 2021
9f811d3
feat(js Element): Trigger focus, blur on input events
biinari May 7, 2021
09bebcc
Add createPaymentMethod
biinari Jul 6, 2021
32493b3
Support customers list all email param
biinari Jul 26, 2021
ea0c5b4
Add payment_intent webhooks
biinari Jul 27, 2021
60de803
Add PaymentMethod webhook events
biinari Jul 27, 2021
bf47d0c
Return webhook in custom config webhook endpoint
biinari Jul 27, 2021
f8308b6
Support old confirmPaymentIntent()
biinari Sep 9, 2021
b00129e
Allow requires_confirmation status for payment intent _authenticate
biinari Sep 9, 2021
3a6a5b8
Log sending webhooks
biinari Sep 9, 2021
6e5c26c
Add Dockerfile
biinari Sep 9, 2021
1c7b3f4
Build from scratch for docker
biinari Sep 9, 2021
410904b
Configure webhooks for docker based on env / vault
biinari Oct 5, 2021
7f4339f
Add Makefile to manage docker
biinari Oct 5, 2021
1ea169a
Add /_status endpoint
biinari Oct 5, 2021
8c368af
Skip logging status requests
biinari Oct 6, 2021
f98d044
docker entrypoint: Exit on error
biinari Oct 6, 2021
1fbbb64
Restore webhooks from pickled data
biinari Oct 19, 2021
1d9489e
Add closing quote for request log
biinari Oct 19, 2021
369a9e0
Delete payment intent after success
biinari Nov 9, 2021
5eefbbf
Add webhook endpoint config from json in stdin
biinari Nov 11, 2021
556f989
Arg --no-save to skip dump to disk
biinari Nov 11, 2021
26309ef
Set from-scratch and no-save in docker entrypoint
biinari Nov 11, 2021
5b03d48
Update to aws ecr get-login-password
ednp Sep 6, 2022
9c28141
Merge pull request #2 from Fatsoma/update/ecr_login
ednp Sep 6, 2022
afcde53
Fix docker run locally
biinari Jun 28, 2023
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
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# example env file for local docker run

# Stripe Webhook:
WEBHOOK_URL=http://host.docker.internal:8082/stripe/webhook
WEBHOOK_SIGNING_SECRET=your_secret

# Stripe Connect Webhook:
CONNECT_WEBHOOK_URL=http://host.docker.internal:8082/stripe/connect-webhook
CONNECT_WEBHOOK_SIGNING_SECRET=your_connect_secret
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ __pycache__
*.py[cod]
/dist
/*.egg-info

.env.*
!.env.example
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM python:3

ENV VAULT_VERSION=1.3.1

RUN cd /tmp && \
wget https://releases.hashicorp.com/vault/${VAULT_VERSION}/vault_${VAULT_VERSION}_linux_amd64.zip && \
unzip vault_${VAULT_VERSION}_linux_amd64.zip && \
mv vault /usr/local/bin/vault && \
rm vault_${VAULT_VERSION}_linux_amd64.zip

COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

COPY . /app

RUN cd /app && \
rm dist/localstripe-*.tar.gz && \
python setup.py sdist && \
pip install dist/localstripe-*.tar.gz && \
rm -rf /app

ENV PORT=8420
EXPOSE 8420

CMD ["/usr/local/bin/entrypoint.sh"]
65 changes: 65 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.PHONY: all
all: help

PROJECTNAME = localstripe
ENV ?= preview
ifeq ($(ENV),sandbox)
REGION = us-east-1
else
REGION = us-west-1
endif
LOCAL_TAG = $(ENV)-$(REGION)-localstripe:latest
ECR_URL = 819738237059.dkr.ecr.$(REGION).amazonaws.com
ECR_TAG = $(ECR_URL)/$(ENV)-$(REGION)-localstripe:latest

SYSTEM = $(shell uname -s)
HOST_PORT ?= 8420
HOST_NAME=host.docker.internal
RUN_ENV ?= development

## docker-login: Login to ECR repository
.PHONY: docker-login
docker-login:
@echo " > Logging in to ECR repository for environment $(ENV)"
@aws ecr get-login-password --region $(REGION) | docker login --username AWS --password-stdin $(ECR_URL)

## docker-build: Build docker image ENV=preview (default)
.PHONY: docker-build
docker-build:
@echo " > Building Docker image $(LOCAL_TAG)"
@docker build -t $(LOCAL_TAG) .
@echo " > Build Completed"

## docker-push: Push docker image ENV=preview (default)
.PHONY: docker-push
docker-push:
@echo " > Tagging image: $(ECR_TAG)"
@docker tag $(LOCAL_TAG) $(ECR_TAG)
@echo " > Pushing docker image: $(ECR_TAG)"
@docker push $(ECR_TAG)
@echo " > Push Completed"

## docker-run: Run docker image locally RUN_ENV=development (default)
.PHONY: docker-run
ifeq ($(SYSTEM),Darwin)
docker-run:
@echo " > Running docker image: $(LOCAL_TAG)"
@docker run --rm -p $(HOST_PORT):8420 --env-file .env.$(RUN_ENV) $(LOCAL_TAG)
else
HOST_IP ?= $(shell docker network inspect bridge -f "{{json (index .IPAM.Config 0).Gateway}}")
docker-run:
@echo " > Running docker image: $(LOCAL_TAG) with host $(HOST_NAME):$(HOST_IP)"
@docker run --rm -p $(HOST_PORT):8420 --add-host "$(HOST_NAME):$(HOST_IP)" --env-file .env.$(RUN_ENV) $(LOCAL_TAG)
endif

## docker-image: Combine docker build and push
.PHONY: docker-image
docker-image: docker-build docker-push

.PHONY: help
help: Makefile
@echo
@echo "Choose a command to run in: '$(PROJECTNAME)':"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo
12 changes: 5 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,17 @@ Then simply run the command ``localstripe``. The fake Stripe server is now
listening on port 8420.

Or launch a container using `the Docker image
<https://hub.docker.com/r/adrienverge/localstripe/>`_:
<https://hub.docker.com/r/fatsoma/localstripe/>`_:

.. code:: shell

docker run -p 8420:8420 adrienverge/localstripe:latest
make docker-run ENV=development

Docker image can be rebuilt using:

.. code::

docker build --no-cache -t adrienverge/localstripe -<<EOF
FROM python:3
RUN pip install localstripe
CMD ["localstripe"]
EOF
docker build --no-cache -t fatsoma/localstripe .

Examples
--------
Expand Down Expand Up @@ -215,6 +211,8 @@ Only those events types are currently supported:
``customer.subscription.deleted``
- Invoice: ``invoice.created``, ``invoice.payment_succeeded`` and
``invoice.payment_failed``
- PaymentIntent: ``payment_intent.created``, ``payment_intent.succeeded``, ``payment_intent.payment_failed`` and ``payment_intent.canceled``
- PaymentMethod: ``payment_method.attached``, ``payment_method.detached``

Flush stored data
-----------------
Expand Down
72 changes: 72 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

set -e

get_data() {
python -c "import json; import sys; print(json.load(sys.stdin)['data']['$1'])"
}

PORT=${PORT:-8420}
if ! echo "$PORT" | grep -q '^[0-9]\+$' ; then
echo "Unexpected value for PORT: ${PORT}"
exit 1
fi

if [ -z "$WEBHOOK_URL" ] ||
[ -z "$CONNECT_WEBHOOK_URL" ] ||
[ -z "$WEBHOOK_SIGNING_SECRET" ] ||
[ -z "$CONNECT_WEBHOOK_SIGNING_SECRET" ]
then
if [ -z "$ENVIRONMENT" ]; then
echo 'Missing ENVIRONMENT env variable'
exit 1
fi

vaultCmd=/usr/local/bin/vault

VAULT_SECRETS=
[ -n "$VAULT_ADDR" ] || export VAULT_ADDR=$VAULT_ADDRESS
if [ -n "$VAULT_ADDR" ]; then
token=$("$vaultCmd" login -token-only -method=aws role="$VAULT_ROLE") && \
VAULT_SECRETS=$(VAULT_TOKEN="$token" "$vaultCmd" kv get -format=json "secret/fatsoma/${ENVIRONMENT}/api/payment")
if [ -z "$WEBHOOK_URL" ]; then
WEBHOOK_URL=$(echo "$VAULT_SECRETS" | get_data 'stripe_webhook_url')
fi
if [ -z "$CONNECT_WEBHOOK_URL" ]; then
CONNECT_WEBHOOK_URL=$(echo "$VAULT_SECRETS" | get_data 'stripe_connect_webhook_url')
fi
if [ -z "$WEBHOOK_SIGNING_SECRET" ]; then
WEBHOOK_SIGNING_SECRET=$(echo "$VAULT_SECRETS" | get_data 'stripe_webhook_signing_secret')
fi
if [ -z "$CONNECT_WEBHOOK_SIGNING_SECRET" ]; then
CONNECT_WEBHOOK_SIGNING_SECRET=$(echo "$VAULT_SECRETS" | get_data 'stripe_connect_webhook_signing_secret')
fi
fi
fi

echo "Starting localstripe"
exec localstripe --port "$PORT" --from-scratch --no-save --config <<JSON
{
"WebhookEndpoints": {
"webhook": {
"url": "$WEBHOOK_URL",
"secret": "$WEBHOOK_SIGNING_SECRET",
"events": [
"payment_intent.succeeded",
"payment_intent.payment_failed",
"payment_method.updated",
"payment_method.card_automatically_updated",
"payment_method.detached"
]
},
"connect-webhook": {
"url": "$CONNECT_WEBHOOK_URL",
"secret": "$CONNECT_WEBHOOK_SIGNING_SECRET",
"events": [
"payment_intent.succeeded",
"payment_intent.payment_failed"
]
}
}
}
JSON
3 changes: 3 additions & 0 deletions localstripe/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ def __init__(self, code, message=None, contents=None):
self.body['error']['message'] = message

def to_response(self):
if self.__cause__ is not None:
self.body['error']['message'] += ': ' + repr(self.__cause__)

return json_response(self.body, status=self.code)
Loading