# How PayPal Generates Subscription Payments for Agency Handy

When PayPal is used to handle subscription payments for **Agency Handy**, the backend process involves multiple steps, including subscription creation, payment authorization, recurring billing, and transaction processing. Here’s a detailed breakdown of how PayPal manages and records these payments:

***

#### **1. Subscription Creation & Setup**

* Agency Handy's backend integrates with **PayPal Subscriptions API**.
* When a user chooses a subscription plan, Agency Handy’s system sends a request to PayPal’s API to create a **subscription agreement**.
* PayPal generates a **subscription ID** and redirects the user to approve the agreement via PayPal.

***

#### **2. Initial Payment Processing**

* Upon user approval, PayPal processes the first subscription payment.
* It checks if the payment method (PayPal balance, bank account, credit/debit card) is valid.
* If successful, PayPal sends a **webhook notification** to Agency Handy’s backend, confirming the payment.

***

#### **3. Recurring Payment Scheduling**

* PayPal maintains a **billing schedule** based on the subscription terms (e.g., monthly or yearly).
* On each renewal date, PayPal automatically charges the user.
* If the payment fails (due to insufficient funds or expired cards), PayPal retries after a certain period.

***

#### **4. Transaction Processing & Recording**

Every payment goes through multiple steps:

**(A) Authorization & Capture**

* PayPal **authorizes** the payment before deducting the funds.
* After authorization, PayPal **captures** the amount and records it under the user’s PayPal transaction history.
* PayPal sends a **payment confirmation webhook** to Agency Handy.

**(B) Payment Settlement**

* PayPal transfers the payment to Agency’s PayPal account after deducting transaction fees.
* The net amount appears in Agency’s **PayPal balance** and is available for withdrawal to a linked bank account.

**(C) Recording in PayPal Dashboard**

Each transaction includes:

* **Transaction ID:** Unique identifier for tracking.
* **Subscription ID:** Links the payment to an active subscription.
* **Timestamp & Status:** Shows success, failure, or pending status.
* **Fee Deduction:** PayPal’s transaction fees are deducted before settlement.

***

#### **5. Handling Subscription Cancellations & Failures**

* If a user cancels their subscription, PayPal stops further billing.
* If payments fail after multiple retries, PayPal marks the subscription as **suspended** or **canceled**.
* Agency Handy’s backend receives **webhooks** about cancellations and updates its records.

***

#### **6. Webhook & API Logs in Agency Handy’s Backend**

* Agency Handy’s system listens to **PayPal Webhooks** to update user subscriptions.
* Common webhooks:
  * **`BILLING.SUBSCRIPTION.CREATED`** → When a new subscription starts.
  * **`PAYMENT.SALE.COMPLETED`** → When a recurring payment is successful.
  * **`BILLING.SUBSCRIPTION.CANCELLED`** → When a user cancels.
* All transactions are logged in Agency Handy’s database, ensuring accurate record-keeping.

***

### **Final Summary**

1. **Subscription is created** → User approves via PayPal.
2. **First payment is processed** → PayPal authorizes and captures the amount.
3. **Recurring billing occurs** → PayPal automatically deducts payments at scheduled intervals.
4. **Payments are recorded in PayPal & Agency Handy** → Transaction details are stored for reference.
5. **Cancellations & failures are managed** → PayPal updates Agency Handy via webhooks.
