Skip to content

Commit

Permalink
Update funding via webhooks (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
duogenesis authored Dec 26, 2024
1 parent 8570192 commit e26c60e
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 2 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ jobs:
DUO_R2_ACCESS_KEY_ID: s3-mock-access-key-id
DUO_R2_ACCESS_KEY_SECRET: s3-mock-secret-access-key-secret

functionality-tests-5:
runs-on: ubuntu-latest

steps:
- name: Check Out Code
uses: actions/checkout@v3

- name: Run functionality tests
run: ./test/util/with-container.sh ./test/functionality5.sh
env:
DUO_EMAIL_KEY: 'not-a-real-key'
DUO_EMAIL_URL: 'https://example.com'

DUO_R2_BUCKET_NAME: s3-mock-bucket
DUO_R2_ACCT_ID: unused-in-dev-env
DUO_R2_ACCESS_KEY_ID: s3-mock-access-key-id
DUO_R2_ACCESS_KEY_SECRET: s3-mock-secret-access-key-secret

unit-tests:
runs-on: ubuntu-latest

Expand Down
10 changes: 10 additions & 0 deletions duotypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
constr,
field_validator,
model_validator,
Extra,
)
from datetime import datetime, date
from dateutil.relativedelta import relativedelta
Expand Down Expand Up @@ -440,3 +441,12 @@ class PostSkip(BaseModel):

class PostVerificationSelfie(BaseModel):
base64_file: Optional[Base64File] = None


class PostKofiData(BaseModel):
verification_token: str
amount: int
currency: str

class Config:
extra = Extra.allow
7 changes: 5 additions & 2 deletions init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,10 @@ CREATE TABLE IF NOT EXISTS banned_person (

CREATE TABLE IF NOT EXISTS funding (
id SMALLINT PRIMARY KEY,

estimated_end_date TIMESTAMP NOT NULL,
token_hash TEXT NOT NULL DEFAULT '',
cost_per_month_usd FLOAT NOT NULL,

CONSTRAINT id CHECK (id = 1)
);
Expand Down Expand Up @@ -1236,8 +1239,8 @@ WHERE
)
ON CONFLICT DO NOTHING;

INSERT INTO funding (id, estimated_end_date)
VALUES (1, '2024-09-17 15:02:10.866000+00')
INSERT INTO funding (id, estimated_end_date, cost_per_month_usd)
VALUES (1, '2024-09-17 15:02:10.866000+00', 360.0)
ON CONFLICT (id) DO NOTHING;

--------------------------------------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions migrations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ALTER TABLE
funding
ADD COLUMN IF NOT EXISTS
token_hash
TEXT NOT NULL DEFAULT
'';

ALTER TABLE
funding
ADD COLUMN IF NOT EXISTS
cost_per_month_usd
FLOAT NOT NULL DEFAULT
360.0;

ALTER TABLE
funding
ALTER COLUMN
cost_per_month_usd
DROP DEFAULT;
5 changes: 5 additions & 0 deletions service/application/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,8 @@ def get_export_data_token(s: t.SessionInfo):
@get('/export-data/<token>')
def get_export_data(token: str):
return person.get_export_data(token=token)

@post('/kofi-donation')
@validate(t.PostKofiData)
def post_kofi_donation(req: t.PostKofiData):
return person.post_kofi_donation(req=req)
12 changes: 12 additions & 0 deletions service/person/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1898,3 +1898,15 @@ def get_export_data(token: str):
as_attachment=True,
download_name='export.json',
)

def post_kofi_donation(req: t.PostKofiData):
if req.currency.lower() != 'usd':
return

params = dict(
token_hash=sha512(req.verification_token),
amount=req.amount,
)

with api_tx() as tx:
tx.execute(Q_KOFI_DONATION, params)
11 changes: 11 additions & 0 deletions service/person/sql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2965,3 +2965,14 @@
FROM
absolute_numbers
"""

Q_KOFI_DONATION = """
UPDATE
funding
SET
estimated_end_date
= estimated_end_date
+ interval '28 days' * %(amount)s / cost_per_month_usd
WHERE
token_hash = %(token_hash)s
"""
82 changes: 82 additions & 0 deletions test/functionality1/kofi-donation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env bash

# This test is mostly intended to verify that the `verificationjobrunner` runs
# the jobs. The unit tests for the `verification` Python module are much
# more in-depth.

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
cd "$script_dir"

source ../util/setup.sh

set -xe

q "
update
funding
set
estimated_end_date = '2024-09-17 15:02:10.866',
token_hash = '$(printf 'valid-token' | sha512sum | cut -d' ' -f1)',
cost_per_month_usd = 100.0
"



echo 'Invalid tokens are ignored'

jc POST /kofi-donation -d '{
"verification_token": "invalid-token",
"message_id": "4859ebc6-6721-4e93-9f8e-12dc08d1cda4",
"timestamp": "2024-12-26T10:52:52Z",
"type": "Donation",
"is_public": true,
"from_name": "Jo Example",
"message": "Good luck with the integration!",
"amount": "3.00",
"url": "https://ko-fi.com/Home/CoffeeShop?txid=00000000-1111-2222-3333-444444444444",
"email": "[email protected]",
"currency": "USD",
"is_subscription_payment": false,
"is_first_subscription_payment": false,
"kofi_transaction_id": "00000000-1111-2222-3333-444444444444",
"shop_items": null,
"tier_name": null,
"shipping": null
}'

actual=$(q "select estimated_end_date from funding")
expected='2024-09-17 15:02:10.866'

diff <(echo "$actual") <(echo "$expected")



echo 'The estimated end date increases when adding 50.0 dollars'

test_token_hash=$(sha512sum <<< "test-token")

jc POST /kofi-donation -d '{
"verification_token": "valid-token",
"message_id": "4859ebc6-6721-4e93-9f8e-12dc08d1cda4",
"timestamp": "2024-12-26T10:52:52Z",
"type": "Donation",
"is_public": true,
"from_name": "Jo Example",
"message": "Good luck with the integration!",
"amount": "50.00",
"url": "https://ko-fi.com/Home/CoffeeShop?txid=00000000-1111-2222-3333-444444444444",
"email": "[email protected]",
"currency": "USD",
"is_subscription_payment": false,
"is_first_subscription_payment": false,
"kofi_transaction_id": "00000000-1111-2222-3333-444444444444",
"shop_items": null,
"tier_name": null,
"shipping": null
}'

actual=$(q "select estimated_end_date from funding")
expected='2024-10-01 15:02:10.866'

diff <(echo "$actual") <(echo "$expected")
14 changes: 14 additions & 0 deletions test/functionality5.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
cd "$script_dir"

for t in ./functionality5/*.sh
do
output=$( "$t" 2>&1 ) || {
rc=$?
echo "$output"
echo "Test failed: $t"
exit "$rc"
}
done
File renamed without changes.

0 comments on commit e26c60e

Please sign in to comment.