# Recurring Payments - Developer: EDD_Subscription

##### Table of Contents

- [Properties](#properties)
- [Method](#methods)
- [Usage](#usage)
- [Object Modification](#modification)
- [Operational Methods](#operational)
- [Getting Subscription Information](#getters)
- [Subscription Conditionals](#conditionals)
- [Status Methods](#status)



[Recurring Payments](https://easydigitaldownloads.com/downloads/recurring-payments/) comes with a new EDD class for managing subscriptions. The EDD\_Subscription class allows a developer to work with subscriptions programmatically. This document will list the properties, methods, and suggested functionality of EDD\_Subscription.

### Properties

- $id = 0;
- $customer\_id = 0;
- $period = &#039;&#039;;
- $initial\_amount = &#039;&#039;;
- $recurring\_amount = &#039;&#039;;
- $bill\_times = 0;
- $parent\_payment\_id = 0;
- $product\_id = 0;
- $created = &#039;0000-00-00 00:00:00&#039;;
- $expiration = &#039;0000-00-00 00:00:00&#039;;
- $status = &#039;pending&#039;;
- $profile\_id = &#039;&#039;;
- $gateway = &#039;&#039;;
- $customer;

### Methods

- create( $data = array() )
- update( $args = array() )
- delete()
- get\_original\_payment\_id()
- get\_child\_payments()
- get\_total\_payments()
- get\_lifetime\_value()
- add\_payment( $args = array() )
- renew()
- complete()
- expire()
- failing()
- cancel()
- can\_cancel()
- get\_cancel\_url()
- can\_update()
- get\_update\_url()
- is\_active()
- is\_expired()
- get\_expiration()
- get\_expiration\_time()
- get\_status()
- get\_status\_label()
- payment\_exists( $txn\_id = &#039;&#039; )

### Usage

##### Basic Instantiation

An &quot;empty&quot; EDD\_Subscription object may be created with this code:

```
$subscription = new EDD_Subscription();
```

This will return an object with this structure:

```
EDD_Subscription Object
(
    [subs_db:EDD_Subscription:private] =&gt; EDD_Subscriptions_DB Object
        (
            [table_name] =&gt; wp_edd_subscriptions
            [version] =&gt; 1.0
            [primary_key] =&gt; id
        )

    [id] =&gt; 0
    [customer_id] =&gt; 0
    [period] =&gt; 
    [initial_amount] =&gt; 
    [recurring_amount] =&gt; 
    [bill_times] =&gt; 0
    [parent_payment_id] =&gt; 0
    [product_id] =&gt; 0
    [created] =&gt; 0000-00-00 00:00:00
    [expiration] =&gt; 0000-00-00 00:00:00
    [status] =&gt; pending
    [profile_id] =&gt; 
    [gateway] =&gt; 
    [customer] =&gt; 
)
```

##### Instantiating An Existing Subscription

The EDD\_Subscription constructor accepts 2 arguments. The first is either an integer representing a subscription ID or if the second argument is true then the first may be a subscription profile\_id.

You can find a subscription ID in the URL while viewing a subscription. ![](https://easydigitaldownloads.com/wp-content/uploads/2022/07/6184cdf1d52d8.png)

You can find the profile ID listed when viewing a subscription. ![](https://easydigitaldownloads.com/wp-content/uploads/2022/07/6184cdf22022e.png)

These two lines of code will return the same output because they refer to the same subscription:

```
$subscription = new EDD_Subscription( 3 );
```

```
$subscription = new EDD_Subscription( &#039;sub_85YmWtzABSAWN7&#039;, true );
```

The above lines of code would each return an object like this:

```
EDD_Subscription Object
(
    [subs_db:EDD_Subscription:private] =&gt; EDD_Subscriptions_DB Object
        (
            [table_name] =&gt; wp_edd_subscriptions
            [version] =&gt; 1.0
            [primary_key] =&gt; id
        )

    [id] =&gt; 1
    [customer_id] =&gt; 3
    [period] =&gt; month
    [initial_amount] =&gt; 50.00
    [recurring_amount] =&gt; 50.00
    [bill_times] =&gt; 0
    [parent_payment_id] =&gt; 87
    [product_id] =&gt; 85
    [created] =&gt; 2016-03-15 15:36:30
    [expiration] =&gt; 2016-04-15 23:59:59
    [status] =&gt; pending
    [profile_id] =&gt; paypal-363e3cc178dab152bb59b958024bce75
    [gateway] =&gt; paypal
    [customer] =&gt; EDD_Customer Object
        (
            [id] =&gt; 3
            [purchase_count] =&gt; 3
            [purchase_value] =&gt; 110.000000
            [email] =&gt; user@example.com
            [name] =&gt; Bob Smith
            [date_created] =&gt; 2016-03-08 15:01:02
            [payment_ids] =&gt; 54,63,87,88,91
            [user_id] =&gt; 2
            [notes] =&gt; Array
                (
                    [0] =&gt; March 16, 2016 14:37:22 - Subscription #3 cancelled admin
                )

            [db:protected] =&gt; EDD_DB_Customers Object
                (
                    [table_name] =&gt; wp_edd_customers
                    [version] =&gt; 1.0
                    [primary_key] =&gt; id
                )

        )

)
```

#### Object Modification

Using the included methods, almost anything may be done with a subscription.

Unless otherwise stated, every method example given will use this instantiation model:

```
$subscription = new EDD_Subscription();
```

#### Operational Methods

EDD\_Subscription includes methods for creating, deleting, updating, and renewing subscriptions, as well as adding payments.

##### $subscription-&gt;create()

In order to properly create a subscription, you need to pass in some information. These items are required:

```
$data = array(
    &#039;customer_id&#039;       =&gt; 0, // an integer, should be a valid customer_id
    &#039;period&#039;            =&gt; &#039;&#039;, // accepts &#039;day&#039;, &#039;week&#039;, &#039;month&#039;, or &#039;year&#039;; how often the subscription renews
    &#039;initial_amount&#039;    =&gt; &#039;&#039;, // accepts a float
    &#039;recurring_amount&#039;  =&gt; &#039;&#039;, // accepts a float
    &#039;bill_times&#039;        =&gt; 0, // accepts an integer; the number of times billing should happen, 0 means indefinite
    &#039;parent_payment_id&#039; =&gt; 0, // accepts an integer; the payment id returned by the initial payment
    &#039;product_id&#039;        =&gt; 0, // accepts an integer; the id of the product
    &#039;created&#039;           =&gt; &#039;&#039;, // accepts a date string; formatted like 0000-00-00 00:00:00
    &#039;expiration&#039;        =&gt; &#039;&#039;, // accepts a date string; formatted like 0000-00-00 00:00:00
    &#039;status&#039;            =&gt; &#039;&#039;, // accepts &#039;Pending&#039;, &#039;Active&#039;, &#039;Cancelled&#039;, &#039;Expired&#039;, &#039;Failing&#039;, &#039;Completed&#039;
    &#039;profile_id&#039;        =&gt; &#039;&#039;, // accepts a string returned by the payment gateway as their subscription ID
);
```

Then you can call the `create()` method with that information.

```
$subscription-&gt;create( $data );
```

The `create()` method returns an object with the same data you would get by querying a specific subscription ID.

#####  

##### $subscription-&gt;update()

Updating a subscription uses the `update()` method any subset of the array options used for the `create()` method. The `update()` method also requires a subscription ID. Something like this would work:

```
$data = array(
    &#039;expiration&#039; =&gt; &#039;2019-07-17 23:59:59&#039;,
);
$subscription-&gt;update( $data );
```

#####  

##### $subscription-&gt;delete()

The `delete()` method accepts a subscription ID and permanently removes the subscription from the EDD store. Note that this does not affect subscriptions registered on Payment Gateways. Those should be handled individually.

##### $subscription-&gt;renew()

This method renews a subscription, extending it by one period beyond the current expiration date.

##### $subscription-&gt;add\_payment( $args = array() )

This method allows you to make a payment. it accepts an array and requires these three array elements:

- **amount**: the amount of money being paid
- **transaction\_id**: the transaction ID of the subscription from the payment gateway. Example: &#039;ch\_17q04L4NqFpaKRwY8ucZjW3t&#039;
- **gateway**: the payment gateway used for the subscription. Example: &#039;stripe&#039;

Upon success `add_payment()` returns true.

#### Getting Subscription Information

EDD\_Subscription provides a number of methods for getting information about a subscription.

##### $subscription-&gt;get\_original\_payment\_id()

This method returns an integer that is the ID of the original payment. In the User Interface, this Payment can be found under Downloads → Payment History.

##### $subscription-&gt;get\_child\_payments()

The method returns an array of EDD\_Payment objects where each object is a child payment. The objects will look something like this:

```
Array
(
    [0] =&gt; EDD_Payment Object
        (
            [ID] =&gt; 1111111
            [_ID:protected] =&gt; 1111111
            [new:protected] =&gt;
            [number:protected] =&gt; 1111111
            [mode:protected] =&gt; live
            [key:protected] =&gt; 8d6f842jkf66cb6312117b9ad417dc6be
            [total:protected] =&gt; 57.4
            [subtotal:protected] =&gt; 57.4
            [tax:protected] =&gt; 0
            [discounted_amount:protected] =&gt; 0
            [tax_rate:protected] =&gt;
            [fees:protected] =&gt; Array
                (
                )

            [fees_total:protected] =&gt; 0
            [discounts:protected] =&gt; samplediscount
            [date:protected] =&gt; 2018-05-17 06:40:29
            [completed_date:protected] =&gt;
            [status:protected] =&gt; edd_subscription
            [post_status:protected] =&gt; edd_subscription
            [old_status:protected] =&gt;
            [status_nicename:protected] =&gt; Renewal
            [customer_id:protected] =&gt; 111111
            [user_id:protected] =&gt; 1nan1
            [first_name:protected] =&gt; Bob Smith
            [last_name:protected] =&gt; Smith
            [email:protected] =&gt; user@example.com
            [user_info:EDD_Payment:private] =&gt; Array
                (
                    [first_name] =&gt; Bob Smith
                    [last_name] =&gt; Smith
                    [discount] =&gt; samplediscount
                    [id] =&gt; 1nan1
                    [email] =&gt; user@example.com
                    [address] =&gt; Array
                        (
                            [line1] =&gt;
                            [line2] =&gt;
                            [city] =&gt;
                            [country] =&gt;
                            [state] =&gt;
                            [zip] =&gt;
                            [vat] =&gt;
                            [notes] =&gt;
                        )

                )

            [payment_meta:EDD_Payment:private] =&gt; Array
                (
                    [key] =&gt; 8d6f842jkf66cb6312117b9ad417dc6be
                    [email] =&gt; user@example.com
                    [date] =&gt; 2018-05-17 06:40:29
                    [user_info] =&gt; Array
                        (
                            [first_name] =&gt; Bob Smith
                            [discount] =&gt; samplediscount
                            [id] =&gt; 1nan1
                            [email] =&gt; user@example.com
                            [address] =&gt; Array
                                (
                                    [line1] =&gt;
                                    [line2] =&gt;
                                    [city] =&gt;
                                    [country] =&gt;
                                    [state] =&gt;
                                    [zip] =&gt;
                                    [vat] =&gt;
                                    [notes] =&gt;
                                )

                        )

                    [[downloads]] =&gt; Array
                        (
                            [0] =&gt; Array
                                (
                                    [id] =&gt; 1234
                                    [quantity] =&gt; 1
                                    [options] =&gt; Array
                                        (
                                            [quantity] =&gt; 1
                                            [price_id] =&gt; 0
                                        )

                                )

                        )

                    [cart_details] =&gt; Array
                        (
                            [0] =&gt; Array
                                (
                                    [name] =&gt; Sample Product
                                    [id] =&gt; 1234
                                    [item_number] =&gt; Array
                                        (
                                            [id] =&gt; 1234
                                            [quantity] =&gt; 1
                                            [options] =&gt; Array
                                                (
                                                    [quantity] =&gt; 1
                                                    [price_id] =&gt; 0
                                                )

                                        )

                                    [item_price] =&gt; 57.4
                                    [quantity] =&gt; 1
                                    [discount] =&gt; 0
                                    [subtotal] =&gt; 57.4
                                    [tax] =&gt; 0
                                    [fees] =&gt; Array
                                        (
                                        )

                                    [price] =&gt; 57.4
                                )

                        )

                    [fees] =&gt; Array
                        (
                        )

                    [currency] =&gt; USD
                )

            [address:protected] =&gt; Array
                (
                    [line1] =&gt;
                    [line2] =&gt;
                    [city] =&gt;
                    [country] =&gt;
                    [state] =&gt;
                    [zip] =&gt;
                    [vat] =&gt;
                    [notes] =&gt;
                )

            [transaction_id:protected] =&gt; 7M999999999999999
            [[downloads:protected]] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [id] =&gt; 1234
                            [quantity] =&gt; 1
                            [options] =&gt; Array
                                (
                                    [quantity] =&gt; 1
                                    [price_id] =&gt; 0
                                )

                        )

                )

            [ip:protected] =&gt; 192.168.1.1
            [gateway:protected] =&gt; paypalexpress
            [currency:protected] =&gt; USD
            [cart_details:protected] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [name] =&gt; Sample Product
                            [id] =&gt; 1234
                            [item_number] =&gt; Array
                                (
                                    [id] =&gt; 1234
                                    [quantity] =&gt; 1
                                    [options] =&gt; Array
                                        (
                                            [quantity] =&gt; 1
                                            [price_id] =&gt; 0
                                        )

                                )

                            [item_price] =&gt; 57.4
                            [quantity] =&gt; 1
                            [discount] =&gt; 0
                            [subtotal] =&gt; 57.4
                            [tax] =&gt; 0
                            [fees] =&gt; Array
                                (
                                )

                            [price] =&gt; 57.4
                        )

                )

            [has_unlimited_downloads:protected] =&gt;
            [pending:EDD_Payment:private] =&gt; Array
                (
                )

            [parent_payment:protected] =&gt; 830936
        )
)
```

##### $subscription-&gt;get\_total\_payments()

This method returns an integer that is a count of the total number of payments made, including the initial payments plus all child payments.

##### $subscription-&gt;get\_lifetime\_value()

This method returns a float that is the monetary total of all payments ever made.

##### $subscription-&gt;get\_cancel\_url()

This method will return a URL for cancelling a subscription that gets processed by the gateway through which the subscription was purchased.

##### $subscription-&gt;get\_update\_url()

This method will return a URL for updating a subscription that gets processed by the gateway through which the subscription was purchased.

##### $subscription-&gt;get\_expiration()

Returns the date and time that the subscription will expire in this format:

```
2016-04-15 23:59:59
```

##### $subscription-&gt;get\_expiration\_time()

This method returns an integer that is a Unix timestamp of the expiration.

##### $subscription-&gt;get\_status()

This method returns the current status of a subscription in format useful for code. Options are &#039;pending&#039;, &#039;active&#039;, &#039;cancelled&#039;, &#039;expired&#039;, &#039;failing&#039;, &#039;completed&#039;.

##### $subscription-&gt;get\_status\_label()

This method returns the current status of a subscription in format useful for presentation. Options are &#039;Pending&#039;, &#039;Active&#039;, &#039;Cancelled&#039;, &#039;Expired&#039;, &#039;Failing&#039;, &#039;Completed&#039;.

#### Subscription Conditionals

EDD\_Subscription offers a number of conditional methods.

##### $subscription-&gt;can\_cancel()

This method returns boolean and is filtered by payment gateways in order to return true on subscriptions that can be cancelled with a profile ID through the merchant processor.

##### $subscription-&gt;can\_update()

This method returns boolean and is filtered by payment gateways in order to return true if the subscription can have its payment method updated. It is *only* for determining if a payment method may be updated.

##### $subscription-&gt;is\_active()

This method returns boolean, and is true if the subscription is not expired
*and* the status is
*either* &#039;active&#039; or &#039;cancelled&#039;.

#####  

##### $subscription-&gt;is\_expired()

This method returns boolean, and is true if the subscription is expired.



##### $subscription-&gt;payment\_exists( $txn\_id = &#039;&#039; )

This method accepts a transaction ID that has been created by a payment gateway. Here&#039;s an example of a subscription transaction ID from Stripe:

```
ch_17q04L4NqFpaKRwY8ucZjW3t
```

This method returns boolean, true if it finds a transaction with the provided ID.

#### Status Methods

EDD_Subscription includes several methods for changing the status of a subscription.

##### $subscription-&gt;complete()



This method changes the status of a subscription to &#039;complete&#039;.

##### $subscription-&gt;expire()

This method changes the status of a subscription to &#039;expired&#039;.

##### $subscription-&gt;failing()

This method changes the status of a subscription to &#039;failing&#039;.

##### $subscription-&gt;cancel()

This method changes the status of a subscription to &#039;cancelled&#039;.