Skip to content

PayPal Setup & Integration Guide

The paypal provider integrates PayPal Orders API v2, Subscriptions, PayPal Vault (Saved Accounts), and Disputes into the Kirak payments runtime.


Kirak connects to PayPal directly via async REST API calls with httpx. No external PayPal SDK or additional pip package is required.

Terminal window
# 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.


In the PayPal Developer Dashboard:

  1. Navigate to Apps & Credentials.
  2. Select Sandbox or Live and click Create App.
  3. Copy the Client ID and Secret.
{
"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"
}
}
}
}
Terminal window
KIRAK_PAYMENT_PAYPAL_CLIENT_ID=...
KIRAK_PAYMENT_PAYPAL_CLIENT_SECRET=...
KIRAK_PAYMENT_PAYPAL_WEBHOOK_ID=... # Obtained in Step 4

In your PayPal REST App settings, scroll to Webhooks $\rightarrow$ Add Webhook:

  1. Webhook URL: https://your-domain.com/payments/webhook/paypal
  2. 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
  3. 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-signature endpoint with the incoming PAYPAL-* 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.


# 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
})

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: True returns 501 NOT_SUPPORTED.
  • Updating a subscription’s plan requires the buyer’s manual re-approval: send the customer to result["data"]["url"] returned by update_subscription.

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.


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