Skip to content

Instantly share code, notes, and snippets.

@williankeller
Created June 7, 2018 04:11
Show Gist options
  • Save williankeller/55a516b3036bfbde9693a74ab155bbf2 to your computer and use it in GitHub Desktop.
Save williankeller/55a516b3036bfbde9693a74ab155bbf2 to your computer and use it in GitHub Desktop.
<?php
namespace OrderService\Resources\Simulation;
use OrderService\Resources\AbstractTransferBuild;
class TransferBuild extends AbstractTransferBuild
{
/**
* {@inheritdoc}
*/
protected function getParams()
{
$result = [
'accountId' => $this->getRequest()->getAccountId(),
'deliveryDate' => $this->getRequest()->getDeliveryDate(),
'paymentMethod' => $this->getCustomerPaymentMethod(),
'empties' => [
'extraAmount' => $this->getRequest()->getEmptyExtraAmount()
]
];
foreach ($this->getRequest()->getItems() as $item) {
$result['items'][] = [
'sku' => $item->getSku(),
'quantity' => $item->getQuantity()
];
}
return $result;
}
/**
* Getting payment method from customer if there are different value from simulation data.
*
* @return string
*/
protected function getCustomerPaymentMethod()
{
// Get payment method from customer.
$customerMethods = $this->request->customer->getPaymentMethods();
// Get payment method from order simulation quote table.
$simulatedMethod = $this->getRequest()->getPaymentMethod();
// Get default playment methods code.
$creditCode = $this->getCreditCode();
$usedMethod = $this->getCashCode();
// Check if account have Credit as payment method.
if (in_array($creditCode, unserialize($customerMethods))) {
// Define as used method.
$usedMethod = $creditCode;
}
// Check if order simulation method is different of customer payment method.
if ($simulatedMethod != $usedMethod) {
return $usedMethod;
}
// Return default value.
return $simulatedMethod;
}
/**
* @return string
*/
protected function getCashCode()
{
/**
* @var \Payment\Model\Method\Cash
*/
return strtoupper(\Payment\Model\Method\Cash::CODE);
}
/**
* @return string
*/
protected function getCreditCode()
{
/**
* @var \Payment\Model\Method\Credit
*/
return strtoupper(\Payment\Model\Method\Credit::CODE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment