Skip to content

Commit

Permalink
Merge pull request #8 from taciobrito/master
Browse files Browse the repository at this point in the history
Added endpoint methods and command to publish configs
  • Loading branch information
adhenrique authored Nov 12, 2020
2 parents 32cacb2 + e0d1122 commit 774d5a8
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 17 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ Update your application configuration to register the package in `config/app.php
],
~~~

### 3 - Update ZOOP Laravel configuration
Rename config.example.php to config.php in `zoop/src/resources/config/` and change the following lines:
### 3 - Publish ZOOP Laravel configuration
Use the following command to publish the configuration settings from config.example.php in `zoop/src/resources/config/`:

~~~
php artisan vendor:publish --provider "Zoop\ZoopServiceProvider" --tag="config"
~~~

This will create the `config/zoopconfig.php` configuration file. Now, change the following lines:

~~~
'defaults' => [
Expand Down
41 changes: 41 additions & 0 deletions src/Contracts/ZoopWebhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Zoop\Contracts;

use Zoop\Lib\APIResource;

interface ZoopWebhooks{

/**
* API Resource.
*
* @param $APIResource $APIResource
*/
public function __construct(APIResource $APIResource);

/**
* Create a new Webhook
*
* @param $post array
*/
public function create($post);

/**
* Delete a Webhook by id
*
* @param $webhookID string
*/
public function delete($webhookID);

/**
* Retrieve the details of a Webhook by id
*
* @param $webhookID string
*/
public function get($webhookID);

/**
* Returns a JSON object with a list of plans.
*/
public function getAll();
}
2 changes: 1 addition & 1 deletion src/Exceptions/ZoopAuthenticationException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zoop\Exceptions;
namespace Zoop\src\Exceptions;

class ZoopAuthenticationException extends \Exception{}
2 changes: 1 addition & 1 deletion src/Exceptions/ZoopException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zoop\Exceptions;
namespace Zoop\src\Exceptions;

class ZoopException extends \Exception {}
2 changes: 1 addition & 1 deletion src/Exceptions/ZoopObjectNotFound.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Zoop\Exceptions;
namespace Zoop\src\Exceptions;

class ZoopObjectNotFound extends \Exception {}
15 changes: 15 additions & 0 deletions src/Facades/ZoopWebhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Zoop\Facades;

use Illuminate\Support\Facades\Facade;

class ZoopWebhooks extends Facade{

/**
* @return string
*/
protected static function getFacadeAccessor(){
return 'ZoopWebhooks';
}
}
30 changes: 25 additions & 5 deletions src/Lib/APIResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ public static function getSingleton(ZoopBase $zoopBase){
* @throws ZoopException
*/
public function fileAPI($api, $files){
$url = $this->zoopBase->getUrl() . $this->zoopBase->getMarketplaceId() . '/' . $api;
if ($this->zoopBase->endpointIsBeta($api)) {
$url = $this->zoopBase->getUrlBeta() . $this->zoopBase->getMarketplaceId() . '/' . $api;
} else {
$url = $this->zoopBase->getUrl() . $this->zoopBase->getMarketplaceId() . '/' . $api;
}
$mimeTypes = [
'application/pdf',
'image/jpeg',
Expand Down Expand Up @@ -110,7 +114,11 @@ public function fileAPI($api, $files){
* @throws ZoopException
*/
public function createAPI($api, $attributes = []) {
$url = $this->zoopBase->getUrl() . $this->zoopBase->getMarketplaceId() . '/' . $api;
if ($this->zoopBase->endpointIsBeta($api)) {
$url = $this->zoopBase->getUrlBeta() . $this->zoopBase->getMarketplaceId() . '/' . $api;
} else {
$url = $this->zoopBase->getUrl() . $this->zoopBase->getMarketplaceId() . '/' . $api;
}
try {
return $this->APIRequest->request('POST', $url, $this->zoopBase->getHeaders(), $attributes);

Expand All @@ -129,7 +137,11 @@ public function createAPI($api, $attributes = []) {
* @throws ZoopException
*/
public function searchAPI($api){
$url = $this->zoopBase->getUrl() . $this->zoopBase->getMarketplaceId() . '/' . $api;
if ($this->zoopBase->endpointIsBeta($api)) {
$url = $this->zoopBase->getUrlBeta() . $this->zoopBase->getMarketplaceId() . '/' . $api;
} else {
$url = $this->zoopBase->getUrl() . $this->zoopBase->getMarketplaceId() . '/' . $api;
}
try {
return $this->APIRequest->request('GET', $url, $this->zoopBase->getHeaders());

Expand All @@ -148,7 +160,11 @@ public function searchAPI($api){
* @throws ZoopException
*/
public function deleteAPI($api) {
$url = $this->zoopBase->getUrl() . $this->zoopBase->getMarketplaceId() . '/' . $api;
if ($this->zoopBase->endpointIsBeta($api)) {
$url = $this->zoopBase->getUrlBeta() . $this->zoopBase->getMarketplaceId() . '/' . $api;
} else {
$url = $this->zoopBase->getUrl() . $this->zoopBase->getMarketplaceId() . '/' . $api;
}
try {
return $this->APIRequest->request('DELETE', $url, $this->zoopBase->getHeaders());

Expand All @@ -168,7 +184,11 @@ public function deleteAPI($api) {
* @throws ZoopException
*/
public function updateAPI($api, $attributes = []){
$url = $this->zoopBase->getUrl() . $this->zoopBase->getMarketplaceId() . '/' . $api;
if ($this->zoopBase->endpointIsBeta($api)) {
$url = $this->zoopBase->getUrlBeta() . $this->zoopBase->getMarketplaceId() . '/' . $api;
} else {
$url = $this->zoopBase->getUrl() . $this->zoopBase->getMarketplaceId() . '/' . $api;
}
try {
return $this->APIRequest->request('PUT', $url, $this->zoopBase->getHeaders(), $attributes);

Expand Down
57 changes: 57 additions & 0 deletions src/Lib/ZoopWebhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Zoop\Lib;


class ZoopWebhooks implements \Zoop\Contracts\ZoopPlans {

/**
* API Resource
*
* @var object
*/
protected $APIResource;

/**
* ZoopPlans constructor.
* @param APIResource $APIResource
*/
public function __construct(APIResource $APIResource){
$this->APIResource = $APIResource;
}

/**
* @param array $post
* @return mixed
*/
public function create($post = []){
$api = 'webhooks';
return $this->APIResource->createAPI($api, $post);
}

/**
* @param string $webhookID
* @return mixed
*/
public function delete($webhookID){
$api = 'webhooks/' . $webhookID;
return $this->APIResource->deleteAPI($api);
}

/**
* @param string $webhookID
* @return mixed
*/
public function get($webhookID){
$api = 'webhooks/' . $webhookID;
return $this->APIResource->searchAPI($api);
}

/**
* @return mixed
*/
public function getAll(){
$api = 'webhooks';
return $this->APIResource->searchAPI($api);
}
}
23 changes: 23 additions & 0 deletions src/ZoopBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ public function getUrl(){
return $this->config['defaults']['endpoint'] . '/' . $this->config['defaults']['api_version'] . '/marketplaces/';
}

/**
* Get endpoint URL beta
*
* @return string
*/
public function getUrlBeta(){
return $this->config['defaults']['endpoint_beta'] . '/' . $this->config['defaults']['api_version'] . '/marketplaces/';
}

/**
* Get Curl Headers
*
Expand All @@ -76,4 +85,18 @@ public function getUrl(){
public function getHeaders(){
return $this->config['headers'];
}

/**
* return bool api is beta
*
* @return bool
*/
public function endpointIsBeta($endpoint) {
$pattern = implode('|', $this->config['defaults']['apis_use_beta']);
preg_match("($pattern)", $endpoint, $match);
if (count($match) > 0) {
return true;
}
return false;
}
}
31 changes: 24 additions & 7 deletions src/ZoopServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@
use Zoop\Lib\ZoopPlans;
use Zoop\Lib\ZoopSubscriptions;
use Zoop\Lib\ZoopInvoices;
use Zoop\Lib\ZoopWebhooks;

class ZoopServiceProvider extends ServiceProvider {

/**
* @return void
*/
public function boot(){

$this->registerPublishing();
$this->mergeConfigFrom(__DIR__.'/resources/config/config.example.php', 'zoopconfig');
}

/**
* @return void
*/
public function register(){

$configFile = __DIR__.'/resources/config/config.php';

$this->mergeConfigFrom($configFile, 'zoopconfig');

$service = ZoopBase::getSingleton($this->app['config']->get('zoopconfig', []));

$this->app->singleton('ZoopBankAccounts', function () use ($service) {
Expand Down Expand Up @@ -80,6 +77,25 @@ public function register(){
$this->app->singleton('ZoopInvoices', function () use ($service) {
return new ZoopInvoices(APIResource::getSingleton($service));
});

$this->app->singleton('ZoopWebhooks', function () use ($service) {
return new ZoopWebhooks(APIResource::getSingleton($service));
});
}

/**
* Register the package's publishable resources.
*
* @return void
*/
private function registerPublishing()
{
if ($this->app->runningInConsole()) {
// Lumen lacks a config_path() helper, so we use base_path()
$this->publishes([
__DIR__.'/resources/config/config.example.php' => base_path('config/zoopconfig.php'),
], 'config');
}
}

/**
Expand All @@ -96,7 +112,8 @@ public function provides(){
ZoopTransfers::class,
ZoopPlans::class,
ZoopSubscriptions::class,
ZoopInvoices::class
ZoopInvoices::class,
ZoopWebhooks::class
];
}

Expand Down
2 changes: 2 additions & 0 deletions src/resources/config/config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
'publishable_key' => 'YOUR_PUBLISHABLE_KEY',
'marketplace_id' => 'YOUR_MARKETPLACE_ID',
'endpoint' => 'https://api.zoop.ws',
'endpoint_beta' => 'https://api-beta.zoop.ws',
'api_version' => 'v1',
'apis_use_beta' => ['tests', 'examples'],
]
];

0 comments on commit 774d5a8

Please sign in to comment.