Skip to content

Commit

Permalink
feat(PAYOUT): ✨ added support for payout creation with inline employee
Browse files Browse the repository at this point in the history
added new method that can create new Payout and Employee together (inline employee)

1. new API support 2. Readme updated
  • Loading branch information
asif987patel committed Oct 8, 2023
1 parent bc00fc5 commit cdb6737
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
composer.lock
/phpunit.xml
.phpunit.result.cache
/.vscode
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ To understand the Event flow of `Gigapay`, you can see it's [Event Documentation
* [List](#payout-list)
* [Creation](#payout-creation)
* [Multiple Creation](#payout-multiple-creation)
* [Creation with inline Employee](#payout-inline-creation)
* [Retrieve single](#payout-retrieve)
* [Delete](#payout-delete)
* [Resend](#payout-resend)
Expand Down Expand Up @@ -370,6 +371,38 @@ return $payout->getJson();
```
the `getJson()` method will return the `Payout`'s object in JSON format

### payout-inline-creation
- `Gigapay` also provides API to create payouts and employee all together.
- while creating new Payout and Employee togher, it's important to note that you should use `invoiced_amount` since other type of filed like `cost` and `amount` needs employee to be verified and since it's new Employee, it wont be verified at that point.
- if you are using this API with some existing Employee then you can use `cost` and `amount` too, it wont create new Employee but just merge it with the given data.
- the arguments for this method is almost same as normal create method, only change is that now instead of employee-id, you need to pass an array with info like Employee `email` and `name` etc.
- you can get more info from [Gigapay doc](https://developer.gigapay.se/?javascript#register-a-payout-with-an-inline-employee)
- thanks @rolandlluka for suggesting this method and adding a pull-request for it. I hope you will be suggesting more improvements too😅
```php
use Mazimez\Gigapay\Payout;

$payout = Payout::createInline(
[
"name" => "jhone dao", //name (required)
"email" => "[email protected]", //email (required)
"country" => "SWE", //country(optional)
"cellphone_number" => "+46760024938"//phone number(optional)
], //inline employee data
'Instagram samarbete 2021-11-13.', //description for payout
null, //amount of payout
null, //cost of payout
120, //invoice amount of payout
'SEK', //currency of payout
json_encode([
"data" => "data from your system" //metadata of payout
]),
null, //The time at which the gig will start. Displayed as ISO 8601 string.
null, //The time at which the gig will end. Displayed as ISO 8601 string.
3 //Unique identifier for the object.
);
return $payout->getJson();
```

### payout-multiple-creation
- `Gigapay` also provides API to create multiple payouts at once
- All of this payouts will be added to the same Invoice object.
Expand Down
13 changes: 10 additions & 3 deletions src/Exceptions/GigapayException.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function getErrorMessage()
$error_message = null;
if ($this->json) {
foreach ($this->json as $key => $value) {

$problem_message = null;
$problem_key = null;
if (is_numeric($key)) {
Expand All @@ -79,13 +80,19 @@ public function getErrorMessage()
if ($key != "non_field_errors") {
$problem_key = $key;
}

if (is_array($value)) {
$problem_message = $value[0];
} else {
$problem_message = $value;
if (is_object($value)) {
if (isset($value->non_field_errors) && is_array($value->non_field_errors)) {
$problem_message = $value->non_field_errors[0];
}
} else {
$problem_message = $value;
}
}


if ($problem_key) {
if ($problem_key == "events") {
foreach ($value as $key => $val) {
Expand Down Expand Up @@ -125,4 +132,4 @@ public function getErrorMessage()

return $error_message;
}
}
}
63 changes: 36 additions & 27 deletions src/Payout.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,11 @@ static function getUrl()
return config('gigapay.server_url') . '/payouts';
}

/**
* get the url for payout resource
* for inline employees
*
* @return $string
*/
static function getInlineUrl()
{
return config('gigapay.server_url') . '/payouts/?expand=employee';
}

/**
* create the new Payout with API
* doc: https://developer.gigapay.se/#register-a-payout
*
* @param string $employee
* @param string $employee_id
* @param string $description
* @param string $amount
* @param string $cost
Expand All @@ -186,7 +175,7 @@ static function getInlineUrl()
* @return \Mazimez\Gigapay\Payout
*/
static function create(
$employee,
$employee_id,
$description,
$amount = null,
$cost = null,
Expand Down Expand Up @@ -217,8 +206,8 @@ static function create(
if ($description) {
$params = array_merge($params, ['description' => $description]);
}
if ($employee) {
$params = array_merge($params, ['employee' => $employee]);
if ($employee_id) {
$params = array_merge($params, ['employee' => $employee_id]);
}
if ($invoiced_amount) {
$params = array_merge($params, ['invoiced_amount' => $invoiced_amount]);
Expand Down Expand Up @@ -248,32 +237,45 @@ static function create(
* create a new Payout with an Inline employee
* doc: https://developer.gigapay.se/#register-a-payout-with-an-inline-employee
*
* @param string $id
* @param string $currency
* @param array $employee
* @param string $description
* @param string $amount
* @param string $cost
* @param string $invoiced_amount
* @param string $currency
* @param object $metadata
* @param string $start_at
* @param string $end_at
* @param string $id
* @return \Mazimez\Gigapay\Payout
* @throws Exceptions\GigapayException
*/

static function createInline(
$id,
$currency,
$description,
$employee,
$invoiced_amount,
$metadata = null
$description,
$amount = null,
$cost = null,
$invoiced_amount = null,
$currency = null,
$metadata = null,
$start_at = null,
$end_at = null,
$id = null
) {
$url = Payout::getInlineUrl();
if (!$invoiced_amount) {
throw new Exception('Invoiced_amount is required.');
$url = Payout::getUrl() . '/?expand=employee';
if (!$amount && !$cost && !$invoiced_amount) {
throw new Exception('Either amount, cost or invoiced_amount is required.');
}
$params = [];
if ($id) {
$params = array_merge($params, ['id' => $id]);
}
if ($amount) {
$params = array_merge($params, ['amount' => $amount]);
}
if ($cost) {
$params = array_merge($params, ['cost' => $cost]);
}
if ($currency) {
$params = array_merge($params, ['currency' => $currency]);
}
Expand All @@ -289,6 +291,13 @@ static function createInline(
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]);
}

$request_manager = new RequestManager();
return new Payout(
$request_manager->getData(
Expand Down Expand Up @@ -538,4 +547,4 @@ public function getJson()
)
);;
}
}
}

0 comments on commit cdb6737

Please sign in to comment.