PHP code example of chartmogul / chartmogul-php

1. Go to this page and download the library: Download chartmogul/chartmogul-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chartmogul / chartmogul-php example snippets




ChartMogul\Configuration::getDefaultConfiguration()
    ->setApiKey('<YOUR_API_KEY>');

print_r(ChartMogul\Ping::ping()->data);

try {
  $dataSources = ChartMogul\DataSource::all();
} catch(\ChartMogul\Exceptions\ForbiddenException $e){
  // handle authentication exception
} catch(\ChartMogul\Exceptions\ChartMogulException $e){
    echo $e->getResponse();
    echo $e->getStatusCode();
}

$ds = $dataSources->first();
var_dump($ds->uuid); // access Datasource properties
$dsAsArray = $ds->toArray(); // convert to array
$ds->destroy();


$ds = ChartMogul\DataSource::create([
  'name' => 'In-house Billing'
]);

ChartMogul\DataSource::retrieve($uuid);

ChartMogul\DataSource::all();

$dataSources = ChartMogul\DataSource::all();
$ds = $dataSources->last();
$ds->destroy();

ChartMogul\Customer::create([
    "data_source_uuid" => $ds->uuid,
    "external_id" => "cus_0003",
    "name" => "Adam Smith",
    "email" => "[email protected]",
    "country" => "US",
    "city" => "New York",
    "lead_created_at" => "2016-10-01T00:00:00.000Z",
    "free_trial_started_at" => "2016-11-02T00:00:00.000Z"
]);

ChartMogul\Customer::all([
  'page' => 1,
  'data_source_uuid' => $ds->uuid
]);

ChartMogul\Customer::findByExternalId([
    "data_source_uuid" => "ds_fef05d54-47b4-431b-aed2-eb6b9e545430",
    "external_id" => "cus_0001"
]);

$cus = ChartMogul\Customer::all()->last();
$cus->destroy();

ChartMogul\Customer::retrieve($uuid);

ChartMogul\Customer::search('[email protected]');

ChartMogul\Customer::merge([
    'customer_uuid' => $cus1->uuid
], [
    'customer_uuid' => $cus2->uuid
]);

// alternatively:
ChartMogul\Customer::merge([
    'external_id' => $cus1->external_id,
    'data_source_uuid' => $ds->uuid
        ], [
    'external_id' => $cus2->external_id,
    'data_source_uuid' => $ds->uuid
]);

$result = ChartMogul\Customer::update([
    'customer_uuid' => $cus1->uuid
        ], [
    'name' => 'New Name'
]);

ChartMogul\Customer::connectSubscriptions("cus_5915ee5a-babd-406b-b8ce-d207133fb4cb", [
    "subscriptions" => [
        [
            "data_source_uuid" => "ds_ade45e52-47a4-231a-1ed2-eb6b9e541213",
            "external_id" => "d1c0c885-add0-48db-8fa9-0bdf5017d6b0",
        ],
        [
            "data_source_uuid" => "ds_ade45e52-47a4-231a-1ed2-eb6b9e541213",
            "external_id" => "9db5f4a1-1695-44c0-8bd4-de7ce4d0f1d4",
        ],
    ]
]);

$subscription1->connect("cus_5915ee5a-babd-406b-b8ce-d207133fb4cb", $subscriptions);


$customer = ChartMogul\Customer::retrieve($cus->uuid);
$customer->attributes;


$customer = ChartMogul\Customer::retrieve($cus->uuid);
$tags = $customer->addTags("important", "Prio1");

$customers = ChartMogul\Customer::search('[email protected]');

foreach ($customers->entries as $customer) {
    $customer->addTags('foo', 'bar', 'baz');
}

$customer = ChartMogul\Customer::retrieve($cus->uuid);
$tags = $customer->removeTags("important", "Prio1");

$customer = ChartMogul\Customer::retrieve($cus->uuid);
$custom = $customer->addCustomAttributes(
    ['type' => 'String', 'key' => 'channel', 'value' => 'Facebook'],
    ['type' => 'Integer', 'key' => 'age', 'value' => 8 ]
);

$customers = ChartMogul\Customer::search('[email protected]');

foreach ($customers->entries as $customer) {
    $customer->addCustomAttributes(
        ['type' => 'String', 'key' => 'channel', 'value' => 'Facebook'],
        ['type' => 'Integer', 'key' => 'age', 'value' => 8 ]
    );
}

$customer = ChartMogul\Customer::retrieve($cus->uuid);
$custom = $customer->updateCustomAttributes(
    ['channel' => 'Twitter'],
    ['age' => 18]
);

$customer = ChartMogul\Customer::retrieve($cus->uuid);
$tags = $customer->removeCustomAttributes("age", "channel");

$customer = ChartMogul\Customer::retrieve($uuid);
$contacts = $customer->contacts([
  'cursor' => 'aabbccdd...'
]);

$customer = ChartMogul\Customer::retrieve($uuid);
$new_customer = $customer->createContact([
  "data_source_uuid" => "ds_00000000-0000-0000-0000-000000000000",
  "first_name" => "Adam",
  "last_name" => "Smith",
]);

$customer = ChartMogul\Customer::retrieve($uuid);
$customer_notes = $customer->notes([
  'cursor' => 'aabbccdd...'
]);

$customer = ChartMogul\Customer::retrieve($uuid);
$new_customer_note = $customer->createNote([
  'type' => 'note',
  'text' => 'This is a note'
]);

$customer = ChartMogul\Customer::retrieve($uuid);
$opportunities = $customer->opportunities([
  'cursor' => 'aabbccdd...'
]);

$customer = ChartMogul\Customer::retrieve($uuid);
$new_opportunity = $customer->createOpportunity([
  'owner' => '[email protected]',
  'pipeline' => 'Sales',
  'pipeline_stage' => 'Qualified',
  'estimated_close_date' => '2022-03-30',
  'currency' => 'USD',
  'amount_in_cents' => 10000,
  'type' => 'one-time',
  'forecast_category' => 'Best Case',
  'win_likelihood' => 80,
  'custom' => [{key: 'custom_key', value: 'custom_value'}]
]);

$customer_notes = ChartMogul\CustomerNote::all([
    'customer_uuid' => $uuid,
    'cursor' => 'aabbccdd...'
])

$customer_note = ChartMogul\CustomerNote::create([
    'customer_uuid': $uuid,
    'type' => 'note',
    'text' => 'This is a note'
])

$customer_note = ChartMogul\CustomerNote::retrieve($note_uuid)

$updated_customer_note = ChartMogul\CustomerNote::update($note_uuid, [
  'text' => 'This is a new note'
]);

$customer_note = ChartMogul\CustomerNote::retrieve($note_uuid)
$customer_note->destroy();

$contacts = ChartMogul\Contacts::all([
  'cursor' => 'aabbccdd...'
]);

$new_contact = ChartMogul\Contact::create([
  "customer_uuid" => "cus_00000000-0000-0000-0000-000000000000",
  "data_source_uuid" => "ds_00000000-0000-0000-0000-000000000000",
  "first_name" => "Adam",
  "last_name" => "Smith",
]);

$contact = ChartMogul\Contact::retrieve($uuid);

$contact = ChartMogul\Contact::retrieve($uuid);
$contact->destroy();

$updated_contact = ChartMogul\Contact::update([
    'contact_uuid' => $uuid
        ], [
    'first_name' => 'New Name'
]);

$merged_contact = ChartMogul\Contact::merge($into_contact_uuid, $from_contact_uuid);

$opportunities = ChartMogul\Opportunity::all([
    'cursor' => 'aabbccdd...'
])

$opportunity = ChartMogul\Opportunity::create([
    'customer_uuid' => $uuid,
    'owner' => '[email protected]',
    'pipeline' => 'New business 1',
    'pipeline_stage' => 'Discovery',
    'estimated_close_date' => '2023-12-22',
    'currency' => 'USD',
    'amount_in_cents' => 100,
    'type' => 'recurring',
    'forecast_category' => 'pipeline',
    'win_likelihood' => 3,
    'custom' => [{ 'key': 'from_campaign', 'value': true }]
])

$opportunity = ChartMogul\Opportunity::retrieve($opportunity_uuid)

$updated_opportunity = ChartMogul\Opportunity::update($opportunity_uuid, [
  'estimated_close_date' => '2024-12-22',
]);

$opportunity = ChartMogul\Opportunity::retrieve($opportunity_uuid)
$opportunity->destroy();

ChartMogul\Plan::create([
    "data_source_uuid" => $ds->uuid,
    "name" => "Bronze Plan",
    "interval_count" => 1,
    "interval_unit" => "month",
    "external_id" => "plan_0001"
]);

ChartMogul\Plan::retrieve($uuid);

$plans = ChartMogul\Plan::all([
  'page' => 1
]);

$plan = ChartMogul\Plan::all()->last();
$plan->destroy();

$plan = ChartMogul\Plan::update(["plan_uuid" => $plan->uuid], [
            "name" => "Bronze Monthly Plan",
            "interval_count" => 1,
            "interval_unit" => "month"
]);

$subscription_events = ChartMogul\SubscriptionEvent::all();

ChartMogul\SubscriptionEvent::create(['subscription_event' => [
    "external_id" => "evnt_026",
    "customer_external_id" => "cus_0003",
    "data_source_uuid" => $ds->uuid,
    "event_type" => "subscription_start_scheduled",
    "event_date" => "2022-03-30",
    "effective_date" => "2022-04-01",
    "subscription_external_id" => "sub_0001",
    "plan_external_id" => "plan_0001",
    "currency" => "USD",
    "amount_in_cents" => 1000
    "quantity" => 1
]]);

ChartMogul\SubscriptionEvent::destroyWithParams(['subscription_event' => [
    "id" => "some_event_id"
]]);

ChartMogul\SubscriptionEvent::updateWithParams(['subscription_event' => [
    "id" => "some_event_id",
    "currency" => "EUR",
    "amount_in_cents" => "100"
]]);

ChartMogul\PlanGroup::create([
    "name" => "Bronze Plan",
    "plans" => [$plan_uuid_1, $plan_uuid_2],
]);

ChartMogul\PlanGroup::retrieve($uuid);

$plan_groups = ChartMogul\PlanGroup::all([
  'page' => 1
]);

$plan_group = ChartMogul\PlanGroup::all()->last();
$plan_group->destroy();

$plan_group = ChartMogul\PlanGroup::update(
  ["plan_group_uuid" => $plan_group->uuid],
  [
  "name" => "A new plan group name",
  "plans" => [$plan_uuid_1, $plan_uuid_2]
]);

$plan_group_plans = ChartMogul\PlanGroups\Plan::all(
  ["plan_group_uuid" => $plan_group->uuid]
);

$plan = ChartMogul\Plan::all()->first();
$cus = ChartMogul\Customer::all()->first();

$line_item_1 = new ChartMogul\LineItems\Subscription([
    'subscription_external_id' => "sub_0001",
    'subscription_set_external_id' => 'set_0001',
    'plan_uuid' =>  $plan->uuid,
    'service_period_start' =>  "2015-11-01 00:00:00",
    'service_period_end' =>  "2015-12-01 00:00:00",
    'amount_in_cents' => 5000,
    'quantity' => 1,
    'discount_code' => "PSO86",
    'discount_amount_in_cents' => 1000,
    'tax_amount_in_cents' => 900,
    'transaction_fees_currency' => "EUR",
    'discount_description' => "5 EUR"
]);

$line_item_2 = new ChartMogul\LineItems\OneTime([
    "description" => "Setup Fees",
    "amount_in_cents" => 2500,
    "quantity" => 1,
    "discount_code" => "PSO86",
    "discount_amount_in_cents" => 500,
    "tax_amount_in_cents" => 450,
    "transaction_fees_currency" => "EUR",
    "discount_description" => "2 EUR"
]);

$transaction = new ChartMogul\Transactions\Payment([
    "date" => "2015-11-05T00:14:23.000Z",
    "result" => "successful"
]);

$invoice = new ChartMogul\Invoice([
    'external_id' => 'INV0001',
    'date' =>  "2015-11-01 00:00:00",
    'currency' => 'USD',
    'due_date' => "2015-11-15 00:00:00",
    'line_items' => [$line_item_1, $line_item_2],
    'transactions' => [$transaction]
]);

$ci = ChartMogul\CustomerInvoices::create([
    'customer_uuid' => $cus->uuid,
    'invoices' => [$invoice]
]);


$ci = ChartMogul\CustomerInvoices::all([
    'customer_uuid' => $cus->uuid,
    'page' => 1,
    'per_page' => 200
]);

$invoices = ChartMogul\Invoice::all([
    'external_id' => 'my_invoice',
    'page' => 1,
    'per_page' => 200
]);

$invoice = ChartMogul\Invoice::retrieve('inv_uuid');

ChartMogul\Transactions\Refund::create([
    "invoice_uuid" => $ci->invoices[0]->uuid,
    "date" => "2015-12-25 18:10:00",
    "result" => "successful"
]);

$subscriptions = $cus->subscriptions();

$subscription = new ChartMogul\Subscription(["uuid" => $subsUUID])
// or use some existing instance of subsctiption, eg. fetched from Customer->subscriptions
$canceldate = '2016-01-01T10:00:00.000Z';
$subscription->cancel($canceldate);

$subscription = new ChartMogul\Subscription(["uuid" => $subsUUID])
$cancellationDates = ['2016-01-01T10:00:00.000Z', '2017-01-01T10:00:00.000Z']
$subscription->setCancellationDates($cancellationDates)

ChartMogul\Metrics::all([
    'start-date' => '2015-01-01',
    'end-date' => '2015-11-24',
    'interval' => 'month',
    'geo' => 'GB',
    'plans' => $plan->name
]);

ChartMogul\Metrics::mrr([
    'start-date' => '2015-01-01',
    'end-date' => '2015-11-24',
    'interval' => 'month',
    'geo' => 'GB',
    'plans' => $plan->name
]);

ChartMogul\Metrics::arr([
    'start-date' => '2015-01-01',
    'end-date' => '2015-11-24',
    'interval' => 'month'
]);

ChartMogul\Metrics::arpa([
    'start-date' => '2015-01-01',
    'end-date' => '2015-11-24',
    'interval' => 'month'
]);

ChartMogul\Metrics::asp([
    'start-date' => '2015-01-01',
    'end-date' => '2015-11-24',
    'interval' => 'month',
]);

ChartMogul\Metrics::customerCount([
    'start-date' => '2015-01-01',
    'end-date' => '2015-11-24',
    'interval' => 'month',
]);

ChartMogul\Metrics::customerChurnRate([
    'start-date' => '2015-01-01',
    'end-date' => '2015-11-24',
]);

ChartMogul\Metrics::mrrChurnRate([
    'start-date' => '2015-01-01',
    'end-date' => '2015-11-24',
]);

ChartMogul\Metrics::ltv([
    'start-date' => '2015-01-01',
    'end-date' => '2015-11-24',
]);

ChartMogul\Metrics\Customers\Subscriptions::all([
    "customer_uuid" => $cus->uuid
]);

ChartMogul\Metrics\Customers\Activities::all([
    "customer_uuid" => $cus->uuid
]);

ChartMogul\Metrics\Activities::all([
    'start-date' => '2020-06-02T00:00:00Z',
    'per-page' => 100
]);

**Create an Activities Export**


$id = '7f554dba-4a41-4cb2-9790-2045e4c3a5b1';
ChartMogul\Metrics\ActivitiesExport::retrieve($id);

ChartMogul\Account::retrieve();

ChartMogul\Configuration::getDefaultConfiguration()
    ->setApiKey('<YOUR_API_KEY>')
    ->setRetries(15); //0 disables retrying
sh
composer