Skip to content

Commit

Permalink
Merge pull request #10 from CHIPAsia/enhance/add-billing-traits
Browse files Browse the repository at this point in the history
- add billing traits
  • Loading branch information
wzul authored May 29, 2023
2 parents f489d70 + 6512db9 commit 587a3ba
Show file tree
Hide file tree
Showing 14 changed files with 449 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BRAND_ID='YOUR-BRAND-ID'
API_KEY='YOUR-API-KEY'
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ composer.lock
.DS_Store

# text editor settings
.vscode/
.vscode/
.env
composer.phar
27 changes: 15 additions & 12 deletions lib/ChipApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@
use Chip\Traits\Api\PaymentMethod;
use Chip\Traits\Api\Client;
use Chip\Traits\Api\Webhook;
use Chip\Traits\Api\Billing;

class ChipApi {
class ChipApi
{

use Purchase, PaymentMethod, Client, Webhook, Billing;

use Purchase, PaymentMethod, Client, Webhook;

protected $client;

protected $mapper;

public function __construct(
protected string $brandId,
protected string $apiKey,
protected string $base = 'https://gate.chip-in.asia/api/v1/',
array $config = []
){
) {
$this->mapper = new \JsonMapper();
$this->mapper->bStrictNullTypes = false;

$this->mapper->bEnforceMapType = false;

$this->client = new \GuzzleHttp\Client(array_merge([
'base_uri' => $this->base,
], $config));
}

protected function request(string $method, string $endpoint, array $options = array())
{
$headers = [];
Expand All @@ -39,10 +42,10 @@ protected function request(string $method, string $endpoint, array $options = ar
'headers' => $headers
), $options));
$body = (string)$response->getBody()->getContents();

return json_decode($body);
}

/**
*
* @param string $content
Expand All @@ -53,10 +56,10 @@ protected function request(string $method, string $endpoint, array $options = ar
public static function verify(string $content, string $signature, string $publicKey): bool
{
return 1 === openssl_verify(
$content,
base64_decode($signature),
$content,
base64_decode($signature),
$publicKey,
'sha256WithRSAEncryption'
);
}
}
}
53 changes: 53 additions & 0 deletions lib/Model/Billing/BillingTemplate.php
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;
}
}
26 changes: 26 additions & 0 deletions lib/Model/Billing/BillingTemplateClient.php
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);
}
}
25 changes: 25 additions & 0 deletions lib/Model/Billing/BillingTemplateClientAddSubscriber.php
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);
}
}
22 changes: 22 additions & 0 deletions lib/Model/Billing/BillingTemplateClientList.php
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);
}
}
22 changes: 22 additions & 0 deletions lib/Model/Billing/BillingTemplateList.php
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);
}
}
22 changes: 22 additions & 0 deletions lib/Model/ClientList.php
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);
}
}
113 changes: 113 additions & 0 deletions lib/Traits/Api/Billing.php
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());
}
}
8 changes: 7 additions & 1 deletion lib/Traits/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Chip\Traits\Api;

use Chip\Model\ClientDetails as ModelClientDetails;
use Chip\Model\ClientList;

trait Client
{
Expand All @@ -17,4 +18,9 @@ public function createClient(ModelClientDetails $client): ModelClientDetails
'json' => $client
]), new ModelClientDetails());
}
}

public function getClients()
{
return $this->mapper->map($this->request('GET', 'clients/'), new ClientList());
}
}
Loading

0 comments on commit 587a3ba

Please sign in to comment.