<commit_before><p align="center">
<a href="https://chartmogul.com"><img width="200" src="https://chartmogul.com/assets/img/logo.png"></a>
</p>

<h3 align="center">Official ChartMogul API PHP Client</h3>

<p align="center"><code>chartmogul-php</code> provides convenient PHP bindings for <a href="https://dev.chartmogul.com">ChartMogul's API</a>.</p>
<p align="center">
  <a href="https://packagist.org/packages/chartmogul/chartmogul-php"><img src="https://badge.fury.io/ph/chartmogul%2Fchartmogul-php.svg" alt="PHP Package" /></a>
  <a href="https://travis-ci.org/chartmogul/chartmogul-php"><img src="https://travis-ci.org/chartmogul/chartmogul-php.svg?branch=master" alt="Build Status"/></a>
</p>
<hr>

<p align="center">
<b><a href="#installation">Installation</a></b>
|
<b><a href="#configuration">Configuration</a></b>
|
<b><a href="#usage">Usage</a></b>
|
<b><a href="#development">Development</a></b>
|
<b><a href="#contributing">Contributing</a></b>
|
<b><a href="#license">License</a></b>
</p>
<hr>
<br>

## Installation

This library requires php 5.5 or above.


**Using Composer**:


```sh
composer require chartmogul/chartmogul-php
```
or in `composer.json`:

```json
{
    "require": {
        "chartmogul/chartmogul-php": "1.0.0"
    }
}
```

## Configuration

Setup the default configuration with your ChartMogul account token and secret key:

```php
require('./vendor/autoload.php');

ChartMogul\Configuration::getDefaultConfiguration()
    ->setAccountToken('<YOUR_ACCOUNT_TOKEN>')
    ->setSecretKey('<YOUR_SECRET_KEY>');
```

## Usage

```php
try {
  $dataSources = ChartMogul\Import\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();

```

All array results are wrapped with [`Doctrine\Common\Collections\ArrayCollection`](http://www.doctrine-project.org/api/common/2.3/class-Doctrine.Common.Collections.ArrayCollection.html) for ease of access. See [Exceptoins](#exceptions) for a list of exceptions thrown by this library.

### Import API

Available methods in Import API:

#### [Data Sources](https://dev.chartmogul.com/docs/data-sources)

**Create Datasources**

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

**List Datasources**

```php
ChartMogul\Import\DataSource::all();
```

**Delete a Datasource**

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

#### Customers

**Import a Customer**

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

**List Customers**

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

**Find Customer By External ID**

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


**Delete A Customer**

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

#### Plans

**Import a Plan**

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

**List Plans**

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


#### Invoices

**Import Invoices**

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

$line_itme_1 = new ChartMogul\Import\LineItems\Subscription([
    'subscription_external_id' => "sub_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
]);

$line_itme_2 = new ChartMogul\Import\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 = new ChartMogul\Import\Transactions\Payment([
    "date" => "2015-11-05T00:14:23.000Z",
    "result" => "successful"
]);

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

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

```

**List Customer Invoices**

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

#### Subscriptions

**List Customer Subscriptions**

```php
$subscriptions = $cus->subscriptions();
```

**Cancel Customer Subscriptions**
```php<commit_msg>Update README: Create a Transaction<commit_after><p align="center">
<a href="https://chartmogul.com"><img width="200" src="https://chartmogul.com/assets/img/logo.png"></a>
</p>

<h3 align="center">Official ChartMogul API PHP Client</h3>

<p align="center"><code>chartmogul-php</code> provides convenient PHP bindings for <a href="https://dev.chartmogul.com">ChartMogul's API</a>.</p>
<p align="center">
  <a href="https://packagist.org/packages/chartmogul/chartmogul-php"><img src="https://badge.fury.io/ph/chartmogul%2Fchartmogul-php.svg" alt="PHP Package" /></a>
  <a href="https://travis-ci.org/chartmogul/chartmogul-php"><img src="https://travis-ci.org/chartmogul/chartmogul-php.svg?branch=master" alt="Build Status"/></a>
</p>
<hr>

<p align="center">
<b><a href="#installation">Installation</a></b>
|
<b><a href="#configuration">Configuration</a></b>
|
<b><a href="#usage">Usage</a></b>
|
<b><a href="#development">Development</a></b>
|
<b><a href="#contributing">Contributing</a></b>
|
<b><a href="#license">License</a></b>
</p>
<hr>
<br>

## Installation

This library requires php 5.5 or above.


**Using Composer**:


```sh
composer require chartmogul/chartmogul-php
```
or in `composer.json`:

```json
{
    "require": {
        "chartmogul/chartmogul-php": "1.0.0"
    }
}
```

## Configuration

Setup the default configuration with your ChartMogul account token and secret key:

```php
require('./vendor/autoload.php');

ChartMogul\Configuration::getDefaultConfiguration()
    ->setAccountToken('<YOUR_ACCOUNT_TOKEN>')
    ->setSecretKey('<YOUR_SECRET_KEY>');
```

## Usage

```php
try {
  $dataSources = ChartMogul\Import\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();

```

All array results are wrapped with [`Doctrine\Common\Collections\ArrayCollection`](http://www.doctrine-project.org/api/common/2.3/class-Doctrine.Common.Collections.ArrayCollection.html) for ease of access. See [Exceptoins](#exceptions) for a list of exceptions thrown by this library.

### Import API

Available methods in Import API:

#### [Data Sources](https://dev.chartmogul.com/docs/data-sources)

**Create Datasources**

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

**List Datasources**

```php
ChartMogul\Import\DataSource::all();
```

**Delete a Datasource**

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

#### Customers

**Import a Customer**

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

**List Customers**

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

**Find Customer By External ID**

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


**Delete A Customer**

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

#### Plans

**Import a Plan**

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

**List Plans**

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


#### Invoices

**Import Invoices**

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

$line_itme_1 = new ChartMogul\Import\LineItems\Subscription([
    'subscription_external_id' => "sub_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
]);

$line_itme_2 = new ChartMogul\Import\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 = new ChartMogul\Import\Transactions\Payment([
    "date" => "2015-11-05T00:14:23.000Z",
    "result" => "successful"
]);

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

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

```

**List Customer Invoices**

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

#### Subscriptions

**Create a Transaction**

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

The same can be done with Payment class.

#### Subscriptions

**List Customer Subscriptions**

```php
$subscriptions = $cus->subscriptions();
```

**Cancel Customer Subscriptions**
```php
{'fraction_non_alphanumeric': 0.2112888888888889, 'fraction_numerical': 0.03635555555555556, 'mean_word_length': 4.740306122448979, 'pattern_counts': {'":': 4, '<': 111, '<?xml version=': 0, '>': 252, 'https://': 16, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '995146', 'n_tokens_mistral': 4594, 'n_tokens_neox': 4155, 'n_words': 720}