RefundDataBuilder.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Gateway\Request;
  7. use Magento\Braintree\Gateway\SubjectReader;
  8. use Magento\Payment\Gateway\Request\BuilderInterface;
  9. use Magento\Payment\Helper\Formatter;
  10. use Magento\Sales\Api\Data\TransactionInterface;
  11. use Magento\Sales\Model\Order\Payment;
  12. class RefundDataBuilder implements BuilderInterface
  13. {
  14. use Formatter;
  15. /**
  16. * @var SubjectReader
  17. */
  18. private $subjectReader;
  19. /**
  20. * Constructor
  21. *
  22. * @param SubjectReader $subjectReader
  23. */
  24. public function __construct(SubjectReader $subjectReader)
  25. {
  26. $this->subjectReader = $subjectReader;
  27. }
  28. /**
  29. * Builds ENV request
  30. *
  31. * @param array $buildSubject
  32. * @return array
  33. */
  34. public function build(array $buildSubject)
  35. {
  36. $paymentDO = $this->subjectReader->readPayment($buildSubject);
  37. /** @var Payment $payment */
  38. $payment = $paymentDO->getPayment();
  39. $amount = null;
  40. try {
  41. $amount = $this->formatPrice($this->subjectReader->readAmount($buildSubject));
  42. } catch (\InvalidArgumentException $e) {
  43. // pass
  44. }
  45. /*
  46. * we should remember that Payment sets Capture txn id of current Invoice into ParentTransactionId Field
  47. * We should also support previous implementations of Magento Braintree -
  48. * and cut off '-capture' postfix from transaction ID to support backward compatibility
  49. */
  50. $txnId = str_replace(
  51. '-' . TransactionInterface::TYPE_CAPTURE,
  52. '',
  53. $payment->getParentTransactionId()
  54. );
  55. return [
  56. 'transaction_id' => $txnId,
  57. PaymentDataBuilder::AMOUNT => $amount
  58. ];
  59. }
  60. }