# Payment Kit

The decentralized Stripe for the Blocklet platform.

## Contribution

### Development

1. Clone the repository
2. Run `make build`
3. Run `cd blocklets/core && blocklet dev`

#### Troubleshooting

**Error: "pre-start error component xxx is not running or unreachable"**

- Create a `.env.local` file in the project root
  - Add `BLOCKLET_DEV_APP_DID="did:abt:your payment kit server did"`
  - Add `BLOCKLET_DEV_MOUNT_POINT="/example"`
- Copy `.env.local` to the `/core` directory
  - Edit `BLOCKLET_DEV_MOUNT_POINT="/"`

**Error: "Insufficient funds to pay for transaction cost from xxx, expected 1.0020909, got 0"**

- Copy the `BLOCKLET_DEV_APP_DID`
- Transfer 2 TBA from your DID Wallet to the copied address

### Configure Stripe

Before using Stripe as a payment method, you need to configure the webhook secret:

1. Set the `STRIPE_WEBHOOK_SECRET` environment variable in your Blocklet settings
   - Add the environment variable `STRIPE_WEBHOOK_SECRET` with your Stripe webhook signing secret
   - You can find this in your Stripe Dashboard > Developers > Webhooks > Signing Secret

### Debug Stripe

1. Install and log in following the instructions at: https://stripe.com/docs/stripe-cli
2. Start your local Payment Kit server and note its port
3. Run `stripe listen --forward-to http://127.0.0.1:8188/api/integrations/stripe/webhook --log-level=debug --latest`
4. Copy the webhook signing secret from the CLI output and set it as `STRIPE_WEBHOOK_SECRET` environment variable

### Test Stripe

Invoices for subscriptions are not finalized automatically. You can use the Stripe Postman collection to finalize them and then confirm the payment.

### Easter Eggs

Non-public environment variables used in the code that can change the behavior of Payment Kit:

- `PAYMENT_CHANGE_LOCKED_PRICE`: Allows changing locked price (must be set to "1" to enable)
- `PAYMENT_RELOAD_SUBSCRIPTION_JOBS`: Reloads subscription jobs on start (must be set to "1" to enable)
- `PAYMENT_BILLING_THRESHOLD`: Global default billing threshold (must be a number)
- `PAYMENT_MIN_STAKE_AMOUNT`: Global minimum stake amount limit (must be a number)
- `PAYMENT_DAYS_UNTIL_DUE`: Global default days until due (must be a number)
- `PAYMENT_DAYS_UNTIL_CANCEL`: Global default days until cancellation (must be a number)
