public function createPayout(PayoutInterface $payout)
{
$request = $this->createNewRequest();
$request->setMethod($request::METHOD_POST);
$request->setPath('payouts');
$amount = $payout->getAmount();
$currency = $payout->getCurrency();
$effectiveDate = $payout->getEffectiveDate();
$token = $payout->getToken();
$body = array('token' => $token->getToken(), 'amount' => $amount, 'currency' => $currency->getCode(), 'instructions' => array(), 'effectiveDate' => $effectiveDate, 'pricingMethod' => $payout->getPricingMethod(), 'guid' => Util::guid(), 'nonce' => Util::nonce());
// Optional
foreach (array('reference', 'notificationURL', 'notificationEmail') as $value) {
$function = 'get' . ucfirst($value);
if ($payout->{$function}() != null) {
$body[$value] = $payout->{$function}();
}
}
// Add instructions
foreach ($payout->getInstructions() as $instruction) {
$body['instructions'][] = array('label' => $instruction->getLabel(), 'address' => $instruction->getAddress(), 'amount' => $instruction->getAmount());
}
$request->setBody(json_encode($body));
$this->addIdentityHeader($request);
$this->addSignatureHeader($request);
$this->request = $request;
$this->response = $this->sendRequest($request);
$body = json_decode($this->response->getBody(), true);
$error_message = false;
$error_message = !empty($body['error']) ? $body['error'] : $error_message;
$error_message = !empty($body['errors']) ? $body['errors'] : $error_message;
$error_message = is_array($error_message) ? implode("\n", $error_message) : $error_message;
if (false !== $error_message) {
throw new \Exception($error_message);
}
$data = $body['data'];
$payout->setId($data['id'])->setAccountId($data['account'])->setResponseToken($data['token'])->setStatus($data['status']);
foreach ($data['instructions'] as $c => $instruction) {
$payout->updateInstruction($c, 'setId', $instruction['id']);
}
return $payout;
}