Difference between revisions of "REST API examples"

From aMember Pro Documentation
Jump to: navigation, search
 
(19 intermediate revisions by 3 users not shown)
Line 1: Line 1:
* Add user to aMember via API:
+
====Add user to aMember via API====
 
<source>
 
<source>
<?php
 
 
$url = 'http://YOURDOMAIN.COM/amember/api/users';
 
$url = 'http://YOURDOMAIN.COM/amember/api/users';
  
 
$fields = array(
 
$fields = array(
 
     '_key' => 'SECRETKEY',
 
     '_key' => 'SECRETKEY',
    '_format' => 'xml',
 
 
     'login' => 'USERNAME',
 
     'login' => 'USERNAME',
 
     'pass' => 'PASSWORD',
 
     'pass' => 'PASSWORD',
Line 18: Line 16:
 
//set the url, number of POST vars, POST data
 
//set the url, number of POST vars, POST data
 
curl_setopt($ch, CURLOPT_URL, $url);
 
curl_setopt($ch, CURLOPT_URL, $url);
 +
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($ch, CURLOPT_POST, true);
 
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
+
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
 +
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
  
 
//execute post
 
//execute post
Line 29: Line 29:
 
</source>
 
</source>
  
* Add Invoice, Payment, Access  
+
====Add Invoice, Payment, Access====
  
 
<source>
 
<source>
<?php
 
 
$url = 'http://YOURDOMAIN.COM/amember/api/invoices';
 
$url = 'http://YOURDOMAIN.COM/amember/api/invoices';
 
+
 
$vars = array(
 
$vars = array(
     '_key'     => 'SECRETKEY',
+
     '_key' => 'SECRETKEY',
     ///// Invoice Record;
+
     // Invoice Record
     'user_id'   => 34,
+
    'public_id' => 'TRAX',
 +
     'user_id' => 1,
 
     'paysys_id' => 'offline',
 
     'paysys_id' => 'offline',
     'currency' => 'USD',
+
     'currency' => 'USD',
 
     'first_subtotal' => '22.00',
 
     'first_subtotal' => '22.00',
     'first_discount'   => '0.00',
+
     'first_discount' => '0.00',
     'first_tax'     => '0.00',
+
     'first_tax' => '0.00',
     'first_shipping'   => '0.00',
+
     'first_shipping' => '0.00',
     'first_total'       => '22.00',
+
     'first_total' => '22.00',
     'first_period'     => '1m',
+
     'first_period' => '1m',
     'rebill_times'     => 99999, /// means forever.
+
     'rebill_times' => 99999, // means until cancel
 
     'second_subtotal' => '22.00',
 
     'second_subtotal' => '22.00',
     'second_discount'   => '0.00',
+
     'second_discount' => '0.00',
     'second_tax'     => '0.00',
+
     'second_tax' => '0.00',
     'second_shipping'   => '0.00',
+
     'second_shipping' => '0.00',
     'second_total'       => '22.00',
+
     'second_total' => '22.00',
     'second_period'     => '1m',
+
     'second_period' => '1m',
     'is_confirmed'     => 1, // Must be 1
+
     'is_confirmed' => 1, // Must be 1
     'status'           => 1, // 1 -piad 0 - pending check Invoice model.
+
     'status' => 1, // 1 - paid 0 - pending check Invoice model
 +
    'nested' => array(
 +
        'invoice-items' => array(
 +
            // InvoiceItem record
 +
            array(
 +
                'invoice_public_id' => 'TRAX',
 +
                'item_id' => 1, // product_id here;
 +
                'item_type' => 'product',
 +
                'item_title' => 'Archive',
 +
                'item_description' => 'Description of item',
 +
                'qty' => 1,
 +
                'first_discount' => '0.00',
 +
                'first_price' => '22.00',
 +
                'first_tax' => '0.00',
 +
                'first_shipping' => '0.00',
 +
                'first_total' => '22.00',
 +
                'first_period' => '1m',
 +
                'rebill_times' => 99999,
 +
                'second_discount' => '0.00',
 +
                'second_tax' => '0.00',
 +
                'second_shipping' => '0.00',
 +
                'second_total' => '22.00',
 +
                'second_price' => '22.00',
 +
                'second_period' => '1m',
 +
                'currency' => 'USD',
 +
                'billing_plan_id' => 1 // Billing plan within  product, check am_billing_plan table.
 +
            )
 +
        ),
 +
        'invoice-payments' => array(
 +
            // InvoicePayment record
 +
            array(
 +
                'invoice_public_id' => 'TRAX',
 +
                'user_id' => 1,
 +
                'paysys_id' => 'offline',
 +
                'receipt_id' => 'test-receipt',
 +
                'transaction_id' => 'test-transaction',
 +
                'currency' => 'USD',
 +
                'amount' => '22.00'
 +
            )
 +
        ),
 +
        'access' => array(
 +
            // Access record
 +
            array(
 +
                'invoice_public_id' => 'TRAX',
 +
                'user_id' => 1,
 +
                'product_id' => 1,
 +
                'transaction_id' => "test-transaction",
 +
                'begin_date' => '2012-01-01',
 +
                'expire_date' => '2013-01-01'
 +
            )
 +
        )
 +
    ));
 +
 +
//open connection
 +
$ch = curl_init();
 +
 +
//set the url, number of POST vars, POST data
 +
curl_setopt($ch, CURLOPT_URL, $url);
 +
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 +
curl_setopt($ch, CURLOPT_POST, true);
 +
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($vars));
 +
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
 +
//execute post
 +
$result = curl_exec($ch);
 +
 +
//close connection
 +
curl_close($ch);
 +
 
 +
</source>
 +
 
 +
====Add access record only====
 +
<source>
 +
$ch = curl_init();
 +
$url ="http://www.example.com/amember/api/access";
 +
$vars = array(
 +
        '_key'          =>      'xxxxxxx',
 +
        'user_id'      =>      1,
 +
        'product_id'    =>      1,
 +
        'begin_date'    =>      date('Y-m-d'), // Today;
 +
        'expire_date'  =>      '2037-12-31' // Lifetime
 +
);
 +
//set the url, number of POST vars, POST data
 +
curl_setopt($ch, CURLOPT_URL, $url);
 +
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 +
curl_setopt($ch, CURLOPT_POST, true);
 +
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($vars));
 +
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
 +
//execute post
 +
$result = curl_exec($ch);
 +
 +
//close connection
 +
curl_close($ch);
  
//// InvoiceItem record
+
</source>
  
    'nested[invoice-items][0][item_id]' => 2, //  - product_id here;
+
====Change expiration date for existing access record====
    'nested[invoice-items][0][item_type]' => 'product',
 
    'nested[invoice-items][0][item_title]' => 'Archive',.
 
    'nested[invoice-items][0][item_description]' => 'Description of item' ,
 
    'nested[invoice-items][0][qty]' => 1,
 
    'nested[invoice-items][0][first_discount]'    =>  '0.00',
 
    'nested[invoice-items][0][first_price]'    =>  '22.00',
 
    'nested[invoice-items][0][first_tax]'    =>  '0.00',
 
    'nested[invoice-items][0][first_shipping]'    =>  '0.00',
 
    'nested[invoice-items][0][first_total]'      =>  '22.00',
 
    'nested[invoice-items][0][first_period]'      =>  '1m',
 
    'nested[invoice-items][0][rebill_times]'      =>  99999,
 
    'nested[invoice-items][0][second_discount]'    =>  '0.00',
 
    'nested[invoice-items][0][second_tax]'    =>  '0.00',
 
    'nested[invoice-items][0][second_shipping]'    =>  '0.00',
 
    'nested[invoice-items][0][second_total]'      =>  '22.00',
 
    'nested[invoice-items][0][second_price]'      =>  '22.00',
 
    'nested[invoice-items][0][second_period]'      =>  '1m',
 
    'nested[invoice-items][0][currency]' => 'USD',
 
    'nested[invoice-items][0][billing_plan_id]' => 2, // Billing plan within  product, check am_billing_plan table.
 
  
     // InvoicePayment record
+
<source>
 +
$ch = curl_init();
 +
$url ="http://www.example.com/amember/api/access/5051"; // Access id should be included in url
 +
$vars = array(
 +
        '_key'          =>     'xxxxx',
 +
        'expire_date'  =>date('Y-m-d')  // Set expire date to today's date
 +
);
 +
//set the url, number of POST vars, POST data
 +
curl_setopt($ch, CURLOPT_URL, $url);
 +
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 +
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // Set method to PUT, so API module knows that this is update request.
 +
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($vars));
 +
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
 +
//execute post
 +
$result = curl_exec($ch);
 +
 +
//close connection
 +
curl_close($ch);
 +
 
 +
</source>
  
    'nested[invoice-payments][0][user_id]' => 34,
+
====Add Product with Billing Plans and assign it to Categories====
    'nested[invoice-payments][0][paysys_id]' => 'offline',
 
    'nested[invoice-payments][0][receipt_id]' => 'test-receipt',
 
    'nested[invoice-payments][0][transaction_id]' => 'test-transaction',
 
    'nested[invoice-payments][0][currency]' => 'USD',
 
    'nested[invoice-payments][0][amount]' => '22.00',
 
  
    // Access record
+
<source>
    'nested[access][0][user_id]' => 34,
+
$url = 'http://YOURDOMAIN.COM/amember/api/products';
    'nested[access][0][product_id]' => 2,
 
    'nested[access][0][transaction_id]' => "test-transaction",
 
    'nested[access][0][begin_date]' => '2012-01-01',
 
    'nested[access][0][expire_date]' => '2013-01-01'
 
  
);
+
$vars = array(
 +
    '_key' => 'SECRETKEY',
 +
   
 +
    'title' => 'Product Title',
 +
    'description' => 'Product Description',
 +
    'start_date' => 'product,group,payment',
 +
    'tax_group' => -1,
 +
   
 +
    'nested' => array(
 +
        'billing-plans' => array(
 +
            array(
 +
                'title' => 'One Time Payment',
 +
                'first_price' => 10,
 +
                'first_period' => '1m',
 +
                'qty' => 1,
 +
                'variable_qty' => 0,
 +
                'currency' => 'USD'
 +
            ),
 +
            array(
 +
                'title' => 'Auto Recurring Subscription',
 +
                'first_price' => 10,
 +
                'first_period' => '1m',
 +
                'second_price' => 10,
 +
                'second_period' => '1m',
 +
                'rebill_times' => 99999, //until cancelation
 +
                'qty' => 1,
 +
                'variable_qty' => 0,
 +
                'currency' => 'USD'
 +
            )
 +
        ),
 +
        'product-product-category' => array(
 +
            array(
 +
                'product_category_id' => 1
 +
            ),
 +
            array(
 +
                'product_category_id' => 2
 +
            )
 +
        )
 +
    ));
  
 
//open connection
 
//open connection
Line 102: Line 222:
  
 
//set the url, number of POST vars, POST data
 
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
+
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $vars);
+
curl_setopt($ch, CURLOPT_POST, true);
 
+
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($vars));
 +
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
 
//execute post
 
//execute post
 
$result = curl_exec($ch);
 
$result = curl_exec($ch);
Line 111: Line 232:
 
//close connection
 
//close connection
 
curl_close($ch);
 
curl_close($ch);
 
 
</source>
 
</source>

Latest revision as of 05:08, 8 August 2018

Add user to aMember via API


$url = 'http://YOURDOMAIN.COM/amember/api/users';

$fields = array(
    '_key' => 'SECRETKEY',
    'login' => 'USERNAME',
    'pass' => 'PASSWORD',
    'email' => 'EMAIL',
    'name_f' => 'FIRST NAME',
    'name_l' => 'LAST NAME',
);
//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);



Add Invoice, Payment, Access


$url = 'http://YOURDOMAIN.COM/amember/api/invoices';
 
$vars = array(
    '_key' => 'SECRETKEY',
    // Invoice Record
    'public_id' => 'TRAX',
    'user_id' => 1,
    'paysys_id' => 'offline',
    'currency' => 'USD',
    'first_subtotal' => '22.00',
    'first_discount' => '0.00',
    'first_tax' => '0.00',
    'first_shipping' => '0.00',
    'first_total' => '22.00',
    'first_period' => '1m',
    'rebill_times' => 99999, // means until cancel
    'second_subtotal' => '22.00',
    'second_discount' => '0.00',
    'second_tax' => '0.00',
    'second_shipping' => '0.00',
    'second_total' => '22.00',
    'second_period' => '1m',
    'is_confirmed' => 1, // Must be 1
    'status' => 1, // 1 - paid 0 - pending check Invoice model
    'nested' => array(
        'invoice-items' => array(
            // InvoiceItem record
            array(
                'invoice_public_id' => 'TRAX',
                'item_id' => 1, // product_id here;
                'item_type' => 'product',
                'item_title' => 'Archive',
                'item_description' => 'Description of item',
                'qty' => 1,
                'first_discount' => '0.00',
                'first_price' => '22.00',
                'first_tax' => '0.00',
                'first_shipping' => '0.00',
                'first_total' => '22.00',
                'first_period' => '1m',
                'rebill_times' => 99999,
                'second_discount' => '0.00',
                'second_tax' => '0.00',
                'second_shipping' => '0.00',
                'second_total' => '22.00',
                'second_price' => '22.00',
                'second_period' => '1m',
                'currency' => 'USD',
                'billing_plan_id' => 1 // Billing plan within  product, check am_billing_plan table.
            )
        ),
        'invoice-payments' => array(
            // InvoicePayment record
            array(
                'invoice_public_id' => 'TRAX',
                'user_id' => 1,
                'paysys_id' => 'offline',
                'receipt_id' => 'test-receipt',
                'transaction_id' => 'test-transaction',
                'currency' => 'USD',
                'amount' => '22.00'
            )
        ),
        'access' => array(
            // Access record
            array(
                'invoice_public_id' => 'TRAX',
                'user_id' => 1,
                'product_id' => 1,
                'transaction_id' => "test-transaction",
                'begin_date' => '2012-01-01',
                'expire_date' => '2013-01-01'
            )
        )
    ));
 
//open connection
$ch = curl_init();
 
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($vars));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
//execute post
$result = curl_exec($ch);
 
//close connection
curl_close($ch);



Add access record only


$ch = curl_init();
$url ="http://www.example.com/amember/api/access";
$vars = array(
        '_key'          =>      'xxxxxxx',
        'user_id'       =>      1,
        'product_id'    =>      1,
        'begin_date'    =>      date('Y-m-d'), // Today; 
        'expire_date'   =>      '2037-12-31' // Lifetime
);
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($vars));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
//execute post
$result = curl_exec($ch);
 
//close connection
curl_close($ch);



Change expiration date for existing access record


$ch = curl_init();
$url ="http://www.example.com/amember/api/access/5051"; // Access id should be included in url
$vars = array(
        '_key'          =>      'xxxxx',
        'expire_date'   =>date('Y-m-d')  // Set expire date to today's date
);
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // Set method to PUT, so API module knows that this is update request. 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($vars));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
//execute post
$result = curl_exec($ch);
 
//close connection
curl_close($ch);



Add Product with Billing Plans and assign it to Categories


$url = 'http://YOURDOMAIN.COM/amember/api/products';

$vars = array(
    '_key' => 'SECRETKEY',
    
    'title' => 'Product Title',
    'description' => 'Product Description',
    'start_date' => 'product,group,payment',
    'tax_group' => -1,
    
    'nested' => array(
        'billing-plans' => array(
            array(
                'title' => 'One Time Payment',
                'first_price' => 10,
                'first_period' => '1m',
                'qty' => 1,
                'variable_qty' => 0,
                'currency' => 'USD'
            ),
            array(
                'title' => 'Auto Recurring Subscription',
                'first_price' => 10,
                'first_period' => '1m',
                'second_price' => 10,
                'second_period' => '1m',
                'rebill_times' => 99999, //until cancelation
                'qty' => 1,
                'variable_qty' => 0,
                'currency' => 'USD'
            )
        ),
        'product-product-category' => array(
            array(
                'product_category_id' => 1
            ),
            array(
                'product_category_id' => 2
            )
        )
    ));

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($vars));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);