FraudHandler.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\Payflow\Service\Response\Handler;
  7. use Magento\Framework\DataObject;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\Xml\Security;
  10. use Magento\Payment\Model\InfoInterface;
  11. use Magento\Paypal\Model\Info;
  12. use Magento\Paypal\Model\Payflowpro;
  13. /**
  14. * Class FraudHandler
  15. */
  16. class FraudHandler implements HandlerInterface
  17. {
  18. /**
  19. * Response message code
  20. */
  21. const RESPONSE_MESSAGE = 'respmsg';
  22. /**
  23. * Fraud rules xml code
  24. */
  25. const FRAUD_RULES_XML = 'fps_prexmldata';
  26. /**
  27. * @var Info
  28. */
  29. private $paypalInfoManager;
  30. /**
  31. * The security scanner XML document
  32. *
  33. * @var Security
  34. */
  35. private $xmlSecurity;
  36. /**
  37. * Constructor
  38. *
  39. * @param Info $paypalInfoManager
  40. * @param Security $xmlSecurity
  41. */
  42. public function __construct(Info $paypalInfoManager, Security $xmlSecurity)
  43. {
  44. $this->paypalInfoManager = $paypalInfoManager;
  45. $this->xmlSecurity = $xmlSecurity;
  46. }
  47. /**
  48. * {inheritdoc}
  49. */
  50. public function handle(InfoInterface $payment, DataObject $response)
  51. {
  52. if (!in_array(
  53. $response->getData('result'),
  54. [
  55. Payflowpro::RESPONSE_CODE_DECLINED_BY_FILTER,
  56. Payflowpro::RESPONSE_CODE_FRAUDSERVICE_FILTER
  57. ]
  58. )) {
  59. return;
  60. }
  61. $fraudMessages = ['RESPMSG' => $response->getData(self::RESPONSE_MESSAGE)];
  62. if ($response->getData(self::FRAUD_RULES_XML)) {
  63. $fraudMessages = array_merge(
  64. $fraudMessages,
  65. $this->getFraudRulesDictionary($response->getData(self::FRAUD_RULES_XML))
  66. );
  67. }
  68. $this->paypalInfoManager->importToPayment(
  69. [
  70. Info::FRAUD_FILTERS => array_merge(
  71. $fraudMessages,
  72. (array)$payment->getAdditionalInformation(Info::FRAUD_FILTERS)
  73. )
  74. ],
  75. $payment
  76. );
  77. }
  78. /**
  79. * Converts rules xml document to description=>message dictionary
  80. *
  81. * @param string $rulesString
  82. * @return array
  83. * @throws LocalizedException
  84. */
  85. private function getFraudRulesDictionary($rulesString)
  86. {
  87. $rules = [];
  88. if (!$this->xmlSecurity->scan($rulesString)) {
  89. return $rules;
  90. }
  91. try {
  92. $rulesXml = new \SimpleXMLElement($rulesString);
  93. foreach ($rulesXml->{'rule'} as $rule) {
  94. $rules[(string)$rule->{'ruleDescription'}] = (string)$rule->{'triggeredMessage'};
  95. }
  96. } catch (\Exception $e) {
  97. } finally {
  98. libxml_use_internal_errors(false);
  99. }
  100. return $rules;
  101. }
  102. }