PayPal Setup & Integration Guide
The paypal provider integrates PayPal Orders API v2, Subscriptions, PayPal Vault (Saved Accounts), and Disputes into the Kirak payments runtime.
1. Prerequisites & Installation
Section titled “1. Prerequisites & Installation”Kirak connects to PayPal directly via async REST API calls with httpx. No external PayPal SDK or additional pip package is required.
# Standard Kirak installation includes everything needed:pip install "kirak"[!NOTE] Why Native REST Instead of the PayPal Python SDK: PayPal’s official server SDK ships under a proprietary non-open-source license, lacks asynchronous execution, does not support dispute resolution or webhook signature verification, and imposes rigid setuptools dependency constraints. Kirak implements a lightweight, fully async REST client directly against PayPal’s v1/v2 endpoints.
2. Credentials & Configuration
Section titled “2. Credentials & Configuration”Step 1: Create a REST App
Section titled “Step 1: Create a REST App”In the PayPal Developer Dashboard:
- Navigate to Apps & Credentials.
- Select Sandbox or Live and click Create App.
- Copy the Client ID and Secret.
Step 2: Configure kirak.json
Section titled “Step 2: Configure kirak.json”{ "payments": { "default_provider": "paypal", "providers": { "paypal": { "type": "paypal", "environment": "sandbox", "success_url": "https://your-domain.com/payment/success", "cancel_url": "https://your-domain.com/payment/cancel" } } }}Step 3: Set Environment Variables
Section titled “Step 3: Set Environment Variables”KIRAK_PAYMENT_PAYPAL_CLIENT_ID=...KIRAK_PAYMENT_PAYPAL_CLIENT_SECRET=...KIRAK_PAYMENT_PAYPAL_WEBHOOK_ID=... # Obtained in Step 43. Configuring Webhooks
Section titled “3. Configuring Webhooks”In your PayPal REST App settings, scroll to Webhooks $\rightarrow$ Add Webhook:
- Webhook URL:
https://your-domain.com/payments/webhook/paypal - Select Events:
- One-Time Checkout:
CHECKOUT.ORDER.APPROVED,PAYMENT.CAPTURE.COMPLETED,PAYMENT.CAPTURE.DECLINED,PAYMENT.CAPTURE.DENIED,PAYMENT.CAPTURE.PENDING,PAYMENT.CAPTURE.REFUNDED,PAYMENT.REFUND.PENDING - Subscriptions:
BILLING.SUBSCRIPTION.ACTIVATED,BILLING.SUBSCRIPTION.UPDATED,BILLING.SUBSCRIPTION.SUSPENDED,BILLING.SUBSCRIPTION.CANCELLED,BILLING.SUBSCRIPTION.EXPIRED,BILLING.SUBSCRIPTION.PAYMENT.FAILED,PAYMENT.SALE.COMPLETED - Disputes:
CUSTOMER.DISPUTE.CREATED,CUSTOMER.DISPUTE.UPDATED,CUSTOMER.DISPUTE.RESOLVED - Vaulted Accounts:
VAULT.PAYMENT-TOKEN.CREATED,VAULT.PAYMENT-TOKEN.DELETED
- One-Time Checkout:
- Copy the generated Webhook ID and export it as
KIRAK_PAYMENT_PAYPAL_WEBHOOK_ID.
[!NOTE] Signature Verification: PayPal signs webhooks using asymmetric public certificates. Kirak verifies incoming deliveries by calling PayPal’s
verify-webhook-signatureendpoint with the incomingPAYPAL-*transmission headers and body. Note that synthetic mock events sent from the dashboard’s webhook simulator cannot be verified this way and will be rejected; test using real Sandbox checkouts.
4. Usage Examples
Section titled “4. Usage Examples”4.1 One-Time Order & Capture
Section titled “4.1 One-Time Order & Capture”# 1. Create order:result = await kirak.payments.initiate_payment({ "provider": "paypal", "user_id": "usr_400", "amount": 2999, # $29.99 in cents "currency": "USD", "name": "Annual License", "type": "one_time",})order_id = result["data"]["order_id"]approval_url = result["data"]["url"]
# 2. Capture upon buyer return (on success page):await kirak.payments.verify_payment({ "provider": "paypal", "payment_id": order_id, # Captures the approved order})4.2 Subscriptions
Section titled “4.2 Subscriptions”Create your subscription plan in PayPal first. The plan ID has the format P-...:
result = await kirak.payments.initiate_payment({ "provider": "paypal", "user_id": "usr_400", "amount": 0, # Price determined by PayPal Plan "currency": "USD", "name": "Cloud Pro Plan", "type": "subscription", "subscription_plan_id": "P-XXXXXXXXXXXXXXXXXXXX",})subscription_url = result["data"]["url"][!WARNING] Cancellation & Plan Changes:
- PayPal’s API only supports immediate cancellation. Passing
at_period_end: Truereturns501 NOT_SUPPORTED.- Updating a subscription’s plan requires the buyer’s manual re-approval: send the customer to
result["data"]["url"]returned byupdate_subscription.
4.3 Vault & Off-Session Charges
Section titled “4.3 Vault & Off-Session Charges”Enable PayPal Vault in your developer account to store vaulted PayPal accounts during one-time checkouts (save_payment_method: True). You can subsequently charge the vaulted account headless using charge_off_session.
5. Webhook Event Mapping
Section titled “5. Webhook Event Mapping”Normalized event |
Gateway Events | Description |
|---|---|---|
payment_completed |
PAYMENT.CAPTURE.COMPLETED, PAYMENT.SALE.COMPLETED |
Payment settled |
payment_failed |
PAYMENT.CAPTURE.DECLINED, PAYMENT.CAPTURE.DENIED |
Capture failed |
subscription_renewed |
PAYMENT.SALE.COMPLETED (on recurring cycle) |
Cycle billed |
subscription_updated |
BILLING.SUBSCRIPTION.UPDATED, BILLING.SUBSCRIPTION.SUSPENDED |
Subscription modified |
subscription_cancelled |
BILLING.SUBSCRIPTION.CANCELLED, BILLING.SUBSCRIPTION.EXPIRED |
Subscription terminated |
refund_completed |
PAYMENT.CAPTURE.REFUNDED |
Refund recorded |
dispute_created |
CUSTOMER.DISPUTE.CREATED |
Buyer filed PayPal dispute |
dispute_updated |
CUSTOMER.DISPUTE.UPDATED, CUSTOMER.DISPUTE.RESOLVED |
Dispute evidence or outcome updated |
payment_method_saved |
VAULT.PAYMENT-TOKEN.CREATED |
Vaulted PayPal account saved |
payment_method_removed |
VAULT.PAYMENT-TOKEN.DELETED |
Vault token deleted |