VaultDataBuilder.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Gateway\Request\PayPal;
  7. use Magento\Braintree\Gateway\SubjectReader;
  8. use Magento\Payment\Gateway\Request\BuilderInterface;
  9. use Magento\Vault\Model\Ui\VaultConfigProvider;
  10. /**
  11. * Vault Data Builder
  12. */
  13. class VaultDataBuilder implements BuilderInterface
  14. {
  15. /**
  16. * Additional options in request to gateway
  17. */
  18. private static $optionsKey = 'options';
  19. /**
  20. * The option that determines whether the payment method associated with
  21. * the successful transaction should be stored in the Vault.
  22. */
  23. private static $storeInVaultOnSuccess = 'storeInVaultOnSuccess';
  24. /**
  25. * @var SubjectReader
  26. */
  27. private $subjectReader;
  28. /**
  29. * VaultDataBuilder constructor.
  30. * @param SubjectReader $subjectReader
  31. */
  32. public function __construct(SubjectReader $subjectReader)
  33. {
  34. $this->subjectReader = $subjectReader;
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function build(array $buildSubject)
  40. {
  41. $result = [];
  42. $paymentDO = $this->subjectReader->readPayment($buildSubject);
  43. $payment = $paymentDO->getPayment();
  44. $data = $payment->getAdditionalInformation();
  45. // the payment token could be stored only if a customer checks the Vault flow on storefront
  46. // see https://developers.braintreepayments.com/guides/paypal/vault/javascript/v2#invoking-the-vault-flow
  47. if (!empty($data[VaultConfigProvider::IS_ACTIVE_CODE])) {
  48. $result[self::$optionsKey] = [
  49. self::$storeInVaultOnSuccess => true
  50. ];
  51. }
  52. return $result;
  53. }
  54. }