| Server IP : 54.233.248.239 / Your IP : 172.28.20.13 Web Server : Apache System : Linux ip-172-28-29-189 6.5.0-1014-aws #14~22.04.1-Ubuntu SMP Thu Feb 15 15:27:06 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.2.34-43+ubuntu22.04.1+deb.sury.org+1 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/vinumday2_0/vendor/pagarme/pagarme-php/lib/Transaction/Request/ |
Upload File : |
<?php
namespace PagarMe\Sdk\Transaction\Request;
use PagarMe\Sdk\RequestInterface;
use PagarMe\Sdk\Transaction\Transaction;
use PagarMe\Sdk\SplitRule\SplitRuleCollection;
use PagarMe\Sdk\Customer\Address;
use PagarMe\Sdk\Customer\Phone;
use PagarMe\Sdk\Customer\Customer;
class TransactionCreate implements RequestInterface
{
/**
* @var \PagarMe\Sdk\Transaction\Transaction
*/
protected $transaction;
/**
* @param \PagarMe\Sdk\Transaction\Transaction $transaction
*/
public function __construct(Transaction $transaction)
{
$this->transaction = $transaction;
}
/**
* @return array
*/
public function getPayload()
{
$customer = $this->transaction->getCustomer();
$transactionData = [
'amount' => $this->transaction->getAmount(),
'payment_method' => $this->transaction->getPaymentMethod(),
'postback_url' => $this->transaction->getPostbackUrl(),
'metadata' => $this->transaction->getMetadata()
];
$customerData = [
'name' => $customer->getName(),
'document_number' => $customer->getDocumentNumber(),
'email' => $customer->getEmail(),
'sex' => $customer->getGender(),
'born_at' => $customer->getBornAt()
];
$customerData = array_merge(
$customerData,
$this->getCustomerAddressData($customer),
$this->getCustomerPhoneData($customer)
);
$transactionData['customer'] = $customerData;
if ($this->transaction->getSplitRules() instanceof SplitRuleCollection) {
$transactionData['split_rules'] = $this->getSplitRulesInfo(
$this->transaction->getSplitRules()
);
}
return $transactionData;
}
/**
* @return string
*/
public function getPath()
{
return 'transactions';
}
/**
* @return string
*/
public function getMethod()
{
return self::HTTP_POST;
}
/**
* @param \PagarMe\Sdk\SplitRule\SplitRuleCollection $splitRules
* @return array
*/
private function getSplitRulesInfo(SplitRuleCollection $splitRules)
{
$rules = [];
foreach ($splitRules as $key => $splitRule) {
$rule = [
'recipient_id' => $splitRule->getRecipient()->getId(),
'charge_processing_fee' => $splitRule->getChargeProcessingFee(),
'liable' => $splitRule->getLiable()
];
$rules[$key] = array_merge($rule, $this->getRuleValue($splitRule));
}
return $rules;
}
/**
* @param \PagarMe\Sdk\SplitRule\SplitRule $splitRule
* @return array
*/
private function getRuleValue($splitRule)
{
if (!is_null($splitRule->getAmount())) {
return ['amount' => $splitRule->getAmount()];
}
return ['percentage' => $splitRule->getPercentage()];
}
/**
* @param \PagarMe\Sdk\Customer\Customer $customer
* @return array
*/
public function getCustomerAddressData(Customer $customer)
{
$address = $customer->getAddress();
if (is_null($address)) {
return [];
}
if (is_array($address)) {
$address = new \PagarMe\Sdk\Customer\Address($address);
}
return [
'address' => [
'street' => $address->getStreet(),
'street_number' => $address->getStreetNumber(),
'complementary' => $address->getComplementary(),
'neighborhood' => $address->getNeighborhood(),
'zipcode' => $address->getZipcode()
]
];
}
/**
* @param \PagarMe\Sdk\Customer\Customer $customer
* @return array
*/
public function getCustomerPhoneData(Customer $customer)
{
$phone = $customer->getPhone();
if (is_null($phone)) {
return [];
}
if (is_array($phone)) {
$phone = new \PagarMe\Sdk\Customer\Phone($phone);
}
return [
'phone' => [
'number' => (string) $phone->getNumber(),
'ddd' => (string) $phone->getDdd(),
'ddi' => (string) $phone->getDdi()
]
];
}
}