RCP_Payments

The RCP_Payments class is for interacting with the payments database table.

insert( $payment_data = array() )

Used for inserting a new payment into the database. The following $payment_data arguments are available:

  • subscription (string) - Name of the associated subscription level.
  • date (string) - Date the payment was made in MySQL format. Omit this parameter to use the current time.
  • amount (float) - Amount the payment was for.
  • user_id (int) - ID of the user who made the payment.
  • payment_type (string) - Description of the type of payment, i.e. "Credit Card One Time".
  • subscription_key (string) - Associated subscription key.
  • transaction_id (string) - Transaction ID from the payment gateway.
  • status (string) - Status of the payment: complete, pending, failed, refunded

Example:

$payments = new RCP_Payments();
$payments->insert( array(
    'subscription'   => 'Silver',
    'amount'         => '15.50',
    'user_id'        => 3,
    'payment_type'   => 'Credit Card One Time',
    'transaction_id' => '123456789',
    'status'         => 'complete'
) );

update( $payment_id = 0, $payment_data = array() )

Used for updating a specific payment. Use the same $payment_data arguments as the insert() method.

get_payment( $payment_id = 0 )

Retrieves a specific payment from the database. Returns a database row object.

get_payment_by( $field = 'id', $value = '' )

Similar to get_payment() but allows you to get a payment by any database column. This is useful for getting a payment by its transaction ID. Example:

$payments = new RCP_Payments();
$payment  = get_payment_by( 'transaction_id', '123456789' );

get_payments( $args = array() )

This method is used for retrieving an array of payments based on arguments. For example, getting all payments for a specific user, or all payments for a specific subscription level.

Available arguments:

  • number (int) - Maximum number of results (default is 20).
  • offset (int) - Offset the results (default is 0).
  • subscription (string) - Name of a subscription level.
  • user_id (int) - ID of a user.
  • date (array) - Array of day/month/year values.
  • fields (string) - Column name(s) to return. Omit to return all columns.
  • status (string) - Name of a status to filter by.
  • s (string) - Search value. This will search the email, subscription key, subscription level, or transaction ID.
  • order (string) - DESC | ASC
  • orderby (string) - Column to order by.

get_earnings( $args = array() )

Calculates the total earnings of all payments in the database. There are a few arguments that allow you to filter the results:

  • subscription (string) - Name of a subscription level.
  • user_id (int) - ID of a user.
  • date (array) - Array of day/month/year values.

Example:

$payments      = new RCP_Payments();
$july_earnings = $payments->get_earnings( array(
    'date' => array(
        'day'   => 01,
        'month' => 07,
        'year'  => 2016
    )
) );

Getting, setting, and deleting payment meta

Payments have a meta table, just like user meta or post meta. You can use the following methods to interact with the meta table:

  • get_meta( $payment_id = 0, $meta_key = '', $single = false ) - Get the meta value for a payment ID and key.
  • add_meta( $payment_id = 0, $meta_key = '', $meta_value, $unique = false ) - Add meta data to a payment.
  • update_meta( $payment_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) - Update an existing meta value. If the field does not yet exist, it will be created.
  • delete_meta( $payment_id = 0, $meta_key = '', $meta_value = '' ) - Deletes the matching meta for a payment.