/src/Component/Payment/BasePayment.php

https://github.com/sonata-project/ecommerce · PHP · 228 lines · 134 code · 43 blank · 51 comment · 9 complexity · 06a4bfeb87994b6706738956ecdc91d4 MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Sonata Project package.
  5. *
  6. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Sonata\Component\Payment;
  12. use Psr\Log\LoggerInterface;
  13. use Sonata\Component\Order\OrderInterface;
  14. abstract class BasePayment implements PaymentInterface
  15. {
  16. /**
  17. * @var string
  18. */
  19. protected $name;
  20. /**
  21. * @var string
  22. */
  23. protected $code;
  24. /**
  25. * @var array
  26. */
  27. protected $options;
  28. /**
  29. * @var array
  30. */
  31. protected $transformers;
  32. protected $logger;
  33. /**
  34. * @var bool
  35. */
  36. protected $isDebug;
  37. /**
  38. * @var bool
  39. */
  40. protected $enabled;
  41. /**
  42. * @throws \RuntimeException
  43. *
  44. * @return string
  45. */
  46. public function generateUrlCheck(OrderInterface $order)
  47. {
  48. if (!$order->getCreatedAt() instanceof \DateTime) {
  49. throw new \RuntimeException(sprintf('The order must have a creation date - id:%s, reference:%s ', $order->getId(), $order->getReference()));
  50. }
  51. return sha1(
  52. $order->getReference().
  53. $order->getCreatedAt()->format('m/d/Y:G:i:s').
  54. $order->getId().
  55. $this->getOption('shop_secret_key')
  56. );
  57. }
  58. public function getCode()
  59. {
  60. return $this->code;
  61. }
  62. public function setCode($code): void
  63. {
  64. $this->code = $code;
  65. }
  66. public function getName()
  67. {
  68. return $this->name;
  69. }
  70. public function setName($name): void
  71. {
  72. $this->name = $name;
  73. }
  74. public function setOptions(array $options): void
  75. {
  76. $this->options = $options;
  77. }
  78. public function getOptions()
  79. {
  80. return $this->options;
  81. }
  82. public function getOption($name, $default = null)
  83. {
  84. return $this->options[$name] ?? $default;
  85. }
  86. /**
  87. * @param $name
  88. *
  89. * @return bool
  90. */
  91. public function hasOption($name)
  92. {
  93. return \array_key_exists($name, $this->options);
  94. }
  95. public function encodeString($value)
  96. {
  97. return $value;
  98. }
  99. public function setLogger(LoggerInterface $logger): void
  100. {
  101. $this->logger = $logger;
  102. }
  103. /**
  104. * @return LoggerInterface
  105. */
  106. public function getLogger()
  107. {
  108. return $this->logger;
  109. }
  110. public function addTransformer($id, $transformer): void
  111. {
  112. $this->transformers[$id] = $transformer;
  113. }
  114. public function getTransformer($name)
  115. {
  116. return $this->transformers[$name] ?? false;
  117. }
  118. public function callback(TransactionInterface $transaction)
  119. {
  120. // check if the order exists
  121. if (!$transaction->getOrder()) {
  122. $transaction->setStatusCode(TransactionInterface::STATUS_ORDER_UNKNOWN);
  123. $transaction->setState(TransactionInterface::STATE_KO);
  124. $transaction->setInformation('The order does not exist');
  125. return $this->handleError($transaction);
  126. }
  127. // check if the request is valid
  128. if (!$this->isRequestValid($transaction)) {
  129. $transaction->setStatusCode(TransactionInterface::STATUS_WRONG_REQUEST);
  130. $transaction->setState(TransactionInterface::STATE_KO);
  131. $transaction->setInformation('The request is not valid');
  132. return $this->handleError($transaction);
  133. }
  134. // check if the callback is valid
  135. if (!$this->isCallbackValid($transaction)) {
  136. $transaction->setStatusCode(TransactionInterface::STATUS_WRONG_CALLBACK);
  137. $transaction->setState(TransactionInterface::STATE_KO);
  138. $transaction->setInformation('The callback reference is not valid');
  139. return $this->handleError($transaction);
  140. }
  141. // apply the transaction id
  142. $this->applyTransactionId($transaction);
  143. // if the order is not open, then something already happen ... (duplicate callback)
  144. if (!$transaction->getOrder()->isOpen()) {
  145. $transaction->setState(TransactionInterface::STATE_OK); // the transaction is valid, but not the order state
  146. $transaction->setStatusCode(TransactionInterface::STATUS_ORDER_NOT_OPEN);
  147. $transaction->setInformation('The order is not open, then something already happen ... (duplicate callback)');
  148. return $this->handleError($transaction);
  149. }
  150. // send the confirmation request to the bank
  151. if (!($response = $this->sendConfirmationReceipt($transaction))) {
  152. $transaction->setInformation('Fail to send the confirmation receipt');
  153. $response = $this->handleError($transaction);
  154. }
  155. return $response;
  156. }
  157. /**
  158. * @param bool $enabled
  159. */
  160. public function setEnabled($enabled): void
  161. {
  162. $this->enabled = $enabled;
  163. }
  164. /**
  165. * @return bool
  166. */
  167. public function getEnabled()
  168. {
  169. return $this->enabled;
  170. }
  171. public function report(TransactionInterface $transaction): void
  172. {
  173. if (!$this->logger) {
  174. return;
  175. }
  176. if (TransactionInterface::STATE_KO === $transaction->getState()) {
  177. $method = 'crit';
  178. } else {
  179. $method = 'info';
  180. }
  181. foreach (explode("\n", (string) $transaction->getInformation()) as $message) {
  182. \call_user_func([$this->logger, $method], $message);
  183. }
  184. }
  185. }