Skip to content

Commit

Permalink
feat(PRICING): ✨ added the pricing resource
Browse files Browse the repository at this point in the history
added the Pricing class with methods to use Pricing APIs and also update .README for it. added the license and update composer.json

1.pricing resource added 2.composer update 3.license added
  • Loading branch information
asif987patel committed May 29, 2022
1 parent 3b98c9d commit 051b872
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 6 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Asif Patel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
65 changes: 61 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<div align="center">
<p><img src="cover.png" alt="Laravel In-app Purchase cover"></p>
<p>
<img src="https://img.shields.io/github/issues/mazimez/laravel-gigapay">
<img src="https://img.shields.io/github/forks/mazimez/laravel-gigapay">
<img src="https://img.shields.io/github/stars/mazimez/laravel-gigapay">
</p>
</div>

# Laravel-Gigapay
Expand Down Expand Up @@ -35,6 +40,10 @@ To understand the Event flow of `Gigapay`, you can see it's [Event Documentation
* [Retrieve single](#invoice-retrieve)
* [Update](#invoice-update)
* [Delete](#invoice-delete)
- [Pricing](#pricing)
* [List](#pricing-list)
* [Retrieve single](#pricing-retrieve)
* [Calculate Pricing](#pricing-calculate)
- [ListResource](#listresource)
* [Pagination](#paginate)
* [Search](#search)
Expand Down Expand Up @@ -281,8 +290,8 @@ You can retrieve any `payout` by it's id. you can get more info from [`Gigapay`
```php
use Mazimez\Gigapay\Payout;

$employee = Payout::findById('1'); //getting employee by it's id
return $employee->getJson();
$payout = Payout::findById('1'); //getting payout by it's id
return $payout->getJson();
```

### payout-delete
Expand Down Expand Up @@ -321,7 +330,7 @@ you can learn more about that from [Gigapay doc](https://developer.gigapay.se/#i


### invoice-list
The Invoice::list() method will return the [ListResource](#listresource) for invoices that you can use to apply filters and search into all your invoice. you can get more info from [`Gigapay` doc](https://developer.gigapay.se/#list-all-invoices)
The `Invoice::list()` method will return the [ListResource](#listresource) for invoices that you can use to apply filters and search into all your invoice. you can get more info from [`Gigapay` doc](https://developer.gigapay.se/#list-all-invoices)
```php
use Mazimez\Gigapay\Invoice;

Expand Down Expand Up @@ -367,6 +376,54 @@ use Mazimez\Gigapay\Invoice;
$invoice = Invoice::findById('f3ee8cb8-fc95-4ea2-9b2e-18875b0d759a');//getting invoice by it's ID
$invoice->destroy(); //deletes the invoice
return $invoice->getJson(); //return the empty invoice instance
```
# Pricing
- The Pricing Resource allows you to calculate the price of payout you would like to make, and to retrieve the price information about previously made payouts. The Resource is designed to mirror the Payouts Resource as closely as possible, e.g. the same request can be used to retrieve the price information of a Payout you'd like to make and to actually make it.
- Either `amount`, `invoiced_amount` or `cost` is used as a basis for calculating the Pricing breakdown.
- One is provided and the other are calculated based on that. Their definitions are:
1. `amount`: net amount paid out plus obligations paid by the recipient.
2. `invoiced_amount`: `amount` plus obligations paid by the employer.
3. `cost`: `invoiced_amount` plus Gigapay's fee. This is the final amount you end up paying.
### Pricing-List
The `Pricing::list()` method will return the [ListResource](#listresource) for pricing that you can use to apply filters. you can get more info from [`Gigapay` doc](https://developer.gigapay.se/#list-pricing-info)
```php
use Mazimez\Gigapay\Pricing;

$pricing = Pricing::list(); //getting list of pricing
$pricing = $pricing->paginate(1, 5); //add pagination
return $pricing->getJson();
```
### Pricing-Retrieve
You can retrieve any payout's Pricing info by payout's id. you can get more info from [`Gigapay` doc](https://developer.gigapay.se/#retrieve-pricing-info-of-payout)
```php
use Mazimez\Gigapay\Pricing;

$pricing = Pricing::findById('89f9cfbe-f1ec-4d17-a895-21cdb584eb4d')//getting pricing info by payout's ID
return $pricing->getJson();
```
### Pricing-Calculate
- Pricing resource mainly helps you calculating the payouts before actually making it. you can use `Pricing::calculatePricing` method to calculate any payout without actually making it.
- while calculating pricing, you need to give `employee_id` as well as the amount that you want to pay, you can give either the `amount` or `cost` or `invoiced_amount` according to your need.
```php
use Mazimez\Gigapay\Pricing;

$pricing = Pricing::calculatePricing(
'0d42728d-b565-402d-80aa-a20bec94a9a2', //id of employee that you want to pay
'SEK', //currency of payout
null, //cost (only 1 out cost, amount and invoiced_amount is allowed)
'120', //amount (only 1 out cost, amount and invoiced_amount is allowed)
null, //invoiced_amount (only 1 out cost, amount and invoiced_amount is allowed)
'test', //description
false, //full_salary_specification
json_encode([
"data" => "data from your system" //metadata of payout
]),
null, //start_at
null, //end_at
null, //id
);
return $pricing->getJson();

```

# ListResource
Expand Down Expand Up @@ -449,7 +506,7 @@ use Mazimez\Gigapay\Invoice;

try {
return Invoice::findById("non-exiting-id")->getJson(); //code that will surely gives error.
} catch (GigapayException $th) { //catching the error will GigapayException
} catch (GigapayException $th) { //catching the error with GigapayException
return [
'message' => $th->getErrorMessage(), //the error message
'json' => $th->getJson() //the json
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
],
"minimum-stability": "dev",
"require": {
"guzzlehttp/guzzle": "7.4.x-dev",
"symfony/psr-http-message-bridge": "2.1.x-dev"
"guzzlehttp/guzzle": "^7.4",
"symfony/psr-http-message-bridge": "^2.1"
},
"autoload": {
"psr-4": {
Expand Down
237 changes: 237 additions & 0 deletions src/Pricing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<?php

namespace Mazimez\Gigapay;

use Mazimez\Gigapay\Managers\RequestManager;
use Mazimez\Gigapay\Resources\ListResource;
use Exception;

class Pricing
{
/**
* Decimal formatted string of the gross salary amount.
*
* @var string
*/
public $amount;

/**
* Decimal formatted string of the invoiced amount.
*
* @var string
*/
public $invoiced_amount;

/**
* Decimal formatted string of the total salary cost.
*
* @var string
*/
public $cost;

/**
* ISO-4217 currency code.
*
* @var string
*/
public $currency;

/**
* Decimal formatted string of Gigapay's fee for this Payout.
*
* @var string
*/
public $fee;

/**
* Decimal formatted string of the cost of mandated health insurance. Will be none if health insurance is not mandated.
*
* @var string
*/
public $health_insurance;

/**
* Decimal formatted string of the payroll taxes. Will be none if payroll taxes are not mandated.
*
* @var string
*/
public $payroll;

/**
* Decimal formatted string of the preliminary income taxes the will be reported and paid on behalf of the recipient.
*
* @var string
*/
public $tax;

/**
* Decimal formatted string of the VAT for the Payout.
*
* @var string/json
*/
public $vat;

/**
* Create a new Pricing instance.
*
* @param json $json
* @return $this
*/
public function __construct($json)
{
$this->amount = $json->amount;
$this->invoiced_amount = $json->invoiced_amount;
$this->cost = $json->cost;
$this->currency = $json->currency;
$this->fee = $json->fee;
$this->health_insurance = $json->health_insurance;
$this->payroll = $json->payroll;
$this->tax = $json->tax;
$this->vat = $json->vat;

return $this;
}

/**
* get the url for Pricing resource
*
* @return $string
*/
static function getUrl()
{
return config('gigapay.server_url') . '/pricing';
}

/**
* get List resource of Pricing info for past Payouts.
* doc: https://developer.gigapay.se/#list-pricing-info
*
* @return \Mazimez\Gigapay\Resources\ListResource
*/
static function list()
{
return new ListResource(Pricing::getUrl());
}

/**
* get pricing info of payout instance by it's ID,
* doc: https://developer.gigapay.se/#retrieve-pricing-info-of-payout
*
* @param string $payout_id
* @return \Mazimez\Gigapay\Pricing
*/
static function findById($payout_id)
{
$url = Pricing::getUrl() . '/' . $payout_id;
$request_manager = new RequestManager();
return new Pricing(
$request_manager->getData('GET', $url)
);
}

/**
* calculate the pricing info for the payout you would like to make,
* doc: https://developer.gigapay.se/#calculate-pricing-info
*
* @param string $employee
* @param string $currency
* @param string $cost
* @param string $amount
* @param string $invoiced_amount
* @param string $description
* @param boolean $full_salary_specification
* @param object $metadata
* @param string $start_at
* @param string $end_at
* @return \Mazimez\Gigapay\Pricing
*/
static function calculatePricing(
$employee,
$currency = null,
$cost = null,
$amount = null,
$invoiced_amount = null,
$description = null,
$full_salary_specification = null,
$metadata = null,
$start_at = null,
$end_at = null,
$id = null
) {
$url = Pricing::getUrl();
if (!$cost && !$amount && !$invoiced_amount) {
throw new Exception('Either cost or amount or invoiced_amount is required.');
}
$params = [];
if ($employee) {
$params = array_merge($params, ['employee' => $employee]);
}
if ($currency) {
$params = array_merge($params, ['currency' => $currency]);
}
if ($cost) {
$params = array_merge($params, ['cost' => $cost]);
}
if ($amount) {
$params = array_merge($params, ['amount' => $amount]);
}
if ($invoiced_amount) {
$params = array_merge($params, ['invoiced_amount' => $invoiced_amount]);
}
if ($description) {
$params = array_merge($params, ['description' => $description]);
}
if ($full_salary_specification) {
$params = array_merge($params, ['full_salary_specification' => $full_salary_specification]);
}
if ($metadata) {
$params = array_merge($params, ['metadata' => $metadata]);
}
if ($start_at) {
$params = array_merge($params, ['start_at' => $start_at]);
}
if ($end_at) {
$params = array_merge($params, ['end_at' => $end_at]);
}
if ($description) {
$params = array_merge($params, ['description' => $description]);
}
if ($id) {
$params = array_merge($params, ['id' => $id]);
}
$request_manager = new RequestManager();
return new Pricing(
$request_manager->getData(
'POST',
$url,
[
'form_params' => $params
]
)
);
}

/**
* convert the Invoice instance to json
*
* @return json
*/
public function getJson()
{
return json_decode(
json_encode(
[
"amount" => $this->amount,
"invoiced_amount" => $this->invoiced_amount,
"cost" => $this->cost,
"currency" => $this->currency,
"fee" => $this->fee,
"health_insurance" => $this->health_insurance,
"payroll" => $this->payroll,
"tax" => $this->tax,
"vat" => $this->vat,
]
)
);;
}
}

0 comments on commit 051b872

Please sign in to comment.