-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from CHIPAsia/enhance/add-billing-traits
- add billing traits
- Loading branch information
Showing
14 changed files
with
449 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
BRAND_ID='YOUR-BRAND-ID' | ||
API_KEY='YOUR-API-KEY' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,6 @@ composer.lock | |
.DS_Store | ||
|
||
# text editor settings | ||
.vscode/ | ||
.vscode/ | ||
.env | ||
composer.phar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Chip\Model\Billing; | ||
|
||
class BillingTemplate implements \JsonSerializable | ||
{ | ||
public $type; | ||
public $id; | ||
public $created_on; | ||
public $updated_on; | ||
|
||
public $clients; //required | ||
public $purchase; //required | ||
public $company_id; | ||
public $number_of_billing_cycles; | ||
public $is_test; | ||
public $user_id; | ||
public $brand_id; //required | ||
public $title; //required | ||
public $is_subscription; //required | ||
|
||
//invoice_* required if `is_subscription` is false | ||
public $invoice_issued; | ||
public $invoice_due; | ||
public $invoice_skip_capture; | ||
public $invoice_send_receipt; | ||
|
||
//subscription_* required if `is_subscription` is true | ||
public $subscription_period; | ||
public $subscription_period_units; | ||
public $subscription_due_period; | ||
public $subscription_due_period_units; | ||
public $subscription_charge_period_end; | ||
public $subscription_trial_periods; | ||
public $subscription_active; | ||
public $subscription_has_active_clients; | ||
|
||
public $force_recurring; | ||
|
||
#[\ReturnTypeWillChange] | ||
public function jsonSerialize() | ||
{ | ||
return array_filter((array) $this, array($this, 'allow_non_null')); | ||
} | ||
|
||
private function allow_non_null($var) | ||
{ | ||
if (is_null($var)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Chip\Model\Billing; | ||
|
||
class BillingTemplateClient implements \JsonSerializable | ||
{ | ||
public $type; | ||
public $id; | ||
public $created_on; | ||
public $updated_on; | ||
|
||
public $client_id; //required | ||
public $number_of_billing_cycles_passed; | ||
public $status; | ||
public $subscription_billing_scheduled_on; | ||
public $payment_method_whitelist; | ||
public $send_invoice_on_charge_failure; | ||
public $send_invoice_on_add_subscriber; | ||
public $send_receipt; | ||
|
||
#[\ReturnTypeWillChange] | ||
public function jsonSerialize() | ||
{ | ||
return array_filter((array) $this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Chip\Model\Billing; | ||
|
||
class BillingTemplateClientAddSubscriber implements \JsonSerializable | ||
{ | ||
|
||
/** | ||
* | ||
* @var BillingTemplateClient | ||
*/ | ||
public $billing_template_client; | ||
|
||
/** | ||
* | ||
* @var Purchase | ||
*/ | ||
public $purchase; | ||
|
||
#[\ReturnTypeWillChange] | ||
public function jsonSerialize() | ||
{ | ||
return array_filter((array) $this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Chip\Model\Billing; | ||
|
||
class BillingTemplateClientList implements \JsonSerializable | ||
{ | ||
|
||
/** | ||
* | ||
* @var BillingTemplateClient[] | ||
*/ | ||
public $results; | ||
|
||
public $next; | ||
public $previous; | ||
|
||
#[\ReturnTypeWillChange] | ||
public function jsonSerialize() | ||
{ | ||
return array_filter((array) $this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Chip\Model\Billing; | ||
|
||
class BillingTemplateList implements \JsonSerializable | ||
{ | ||
|
||
/** | ||
* | ||
* @var BillingTemplate[] | ||
*/ | ||
public $results; | ||
|
||
public $next; | ||
public $previous; | ||
|
||
#[\ReturnTypeWillChange] | ||
public function jsonSerialize() | ||
{ | ||
return array_filter((array) $this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Chip\Model; | ||
|
||
class ClientList implements \JsonSerializable | ||
{ | ||
|
||
/** | ||
* | ||
* @var ClientDetails[] | ||
*/ | ||
public $results; | ||
|
||
public $next; | ||
public $previous; | ||
|
||
#[\ReturnTypeWillChange] | ||
public function jsonSerialize() | ||
{ | ||
return array_filter((array) $this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace Chip\Traits\Api; | ||
|
||
use Chip\Model\Billing\BillingTemplate; | ||
use Chip\Model\Billing\BillingTemplateClient; | ||
use Chip\Model\Billing\BillingTemplateClientAddSubscriber; | ||
use Chip\Model\Billing\BillingTemplateClientList; | ||
use Chip\Model\Billing\BillingTemplateList; | ||
use Chip\Model\Purchase; | ||
|
||
trait Billing | ||
{ | ||
/** | ||
* Send an invoice to one or several clients. | ||
*/ | ||
public function createBilling(BillingTemplate $billing) | ||
{ | ||
return $this->request('POST', 'billing/', [ | ||
'json' => $billing | ||
]); | ||
} | ||
|
||
/** | ||
* Create a template to issue repeated invoices from in the future, with or without a subscription. | ||
*/ | ||
public function createBillingTemplate(BillingTemplate $billing) | ||
{ | ||
return $this->mapper->map($this->request('POST', 'billing_templates/', [ | ||
'json' => $billing | ||
]), new BillingTemplate()); | ||
} | ||
|
||
/** | ||
* List all billing templates. | ||
*/ | ||
public function getBillingTemplates() | ||
{ | ||
return $this->mapper->map($this->request('GET', 'billing_templates/'), new BillingTemplateList()); | ||
} | ||
|
||
/** | ||
* Retrieve a billing template by ID. | ||
*/ | ||
public function getBillingTemplate(string $billing_id) | ||
{ | ||
return $this->mapper->map($this->request('GET', "billing_templates/$billing_id/"), new BillingTemplate()); | ||
} | ||
|
||
/** | ||
* Update a billing template by ID. | ||
*/ | ||
public function updateBillingTemplate(string $billing_id, BillingTemplate $billing) | ||
{ | ||
return $this->mapper->map($this->request('PUT', "billing_templates/$billing_id/", [ | ||
'json' => $billing | ||
]), new BillingTemplate()); | ||
} | ||
|
||
/** | ||
* Delete a billing template by ID. | ||
*/ | ||
public function deleteBillingTemplate(string $billing_id) | ||
{ | ||
return $this->request('DELETE', "billing_templates/$billing_id/"); | ||
} | ||
|
||
/** | ||
* Send an invoice, generating a purchase from billing template data. | ||
*/ | ||
public function sendBillingTemplateInvoice(string $billing_id, BillingTemplateClient $billingTemplateClient) | ||
{ | ||
return $this->mapper->map($this->request('POST', "billing_templates/$billing_id/send_invoice/", [ | ||
'json' => $billingTemplateClient | ||
]), new Purchase()); | ||
} | ||
|
||
/** | ||
* Add a billing template client and activate recurring billing (is_subscription: true). | ||
*/ | ||
public function addBillingTemplateSubscriber(string $billing_id, BillingTemplateClient $billingTemplateClient) | ||
{ | ||
return $this->mapper->map($this->request('POST', "billing_templates/$billing_id/add_subscriber/", [ | ||
'json' => $billingTemplateClient | ||
]), new BillingTemplateClientAddSubscriber()); | ||
} | ||
|
||
/** | ||
* List all billing template clients for this billing template. | ||
*/ | ||
public function getBillingTemplateClients(string $billing_id) | ||
{ | ||
return $this->mapper->map($this->request('GET', "billing_templates/$billing_id/clients/"), new BillingTemplateClientList()); | ||
} | ||
|
||
/** | ||
* Retrieve a billing template client by client's ID. | ||
*/ | ||
public function getBillingTemplateClient(string $billing_id, string $billing_client_id) | ||
{ | ||
return $this->mapper->map($this->request('GET', "billing_templates/$billing_id/clients/$billing_client_id/"), new BillingTemplateClient()); | ||
} | ||
|
||
/** | ||
* Partially update a billing template client by client's ID. | ||
*/ | ||
public function updateBillingTemplateClient(string $billing_id, string $billing_client_id, BillingTemplateClient $billingTemplateClient) | ||
{ | ||
return $this->mapper->map($this->request('PATCH', "billing_templates/$billing_id/clients/$billing_client_id/", [ | ||
'json' => $billingTemplateClient | ||
]), new BillingTemplateClient()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.