KhipuRequestBuilder.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. // Copyright 2019 Hackware SpA <human@hackware.cl>
  3. // This file is part of "Hackware Web Services Payment" and licensed under
  4. // the terms of the GNU Affero General Public License version 3, or (at your
  5. // option) a later version. You should have received a copy of this license
  6. // along with the software. If not, see <https://www.gnu.org/licenses/>.
  7. namespace Hawese\Payment\Gateways;
  8. use Hawese\Payment\Support\Http as HttpSupport;
  9. use Psr\Http\Message\RequestInterface;
  10. class KhipuRequestBuilder extends AbstractRequestBuilder
  11. {
  12. const ENDPOINT = 'https://khipu.com/api/2.0';
  13. private $credentials;
  14. public function __construct(...$args)
  15. {
  16. parent::__construct(...$args);
  17. $this->credentials = (object) config('gateways.khipu.credentials');
  18. $this->addDefaultHeaders();
  19. }
  20. protected function getEndpoint(): string
  21. {
  22. return self::ENDPOINT;
  23. }
  24. private function addDefaultHeaders(): void
  25. {
  26. $this->headers['Authorization'] = $this->authorizationHeader();
  27. }
  28. private function authorizationHeader(): string
  29. {
  30. return $this->credentials->receiverId . ':' . $this->getSignature();
  31. }
  32. private function getSignature(): string
  33. {
  34. return hash_hmac(
  35. 'sha256',
  36. $this->toSign(),
  37. $this->credentials->secretKey
  38. );
  39. }
  40. private function toSign(): string
  41. {
  42. $params = $this->getBodyOrQueryParams();
  43. ksort($params); // Khipu signature requires sorting
  44. return $this->method . '&' .
  45. rawurlencode((string) $this->buildUri()->withQuery('')) . '&' .
  46. HttpSupport::buildUriQuery($params);
  47. }
  48. }