ltsecurityinc安防产品在线商城代码
This commit is contained in:
+226
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Paypal\Model\Payflow\Service\Request;
|
||||
|
||||
use Magento\Framework\Api\SearchCriteriaBuilder;
|
||||
use Magento\Framework\DataObject;
|
||||
use Magento\Framework\Math\Random;
|
||||
use Magento\Paypal\Model\Payflow\Service\Gateway;
|
||||
use Magento\Quote\Api\CartRepositoryInterface;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Store\Api\Data\StoreInterface;
|
||||
use Magento\Store\Api\StoreRepositoryInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit\Framework\MockObject\MockObject as MockObject;
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDbIsolation disabled
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class SecureTokenTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var SecureToken
|
||||
*/
|
||||
private $service;
|
||||
|
||||
/**
|
||||
* @var Gateway|MockObject
|
||||
*/
|
||||
private $gateway;
|
||||
|
||||
/**
|
||||
* @var CartRepositoryInterface
|
||||
*/
|
||||
private $quoteRepository;
|
||||
|
||||
/**
|
||||
* @var Random|MockObject;
|
||||
*/
|
||||
private $mathRandom;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
|
||||
$this->gateway = $this->getMockBuilder(Gateway::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$this->quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
$this->objectManager->addSharedInstance($this->gateway, Gateway::class);
|
||||
|
||||
$this->mathRandom = $this->getMockBuilder(Random::class)
|
||||
->getMock();
|
||||
|
||||
$this->service = $this->objectManager->create(
|
||||
SecureToken::class,
|
||||
[
|
||||
'mathRandom' => $this->mathRandom,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->objectManager->removeSharedInstance(Gateway::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a case when secure token can be obtained with credentials for the default scope.
|
||||
*
|
||||
* @magentoDataFixture Magento/Paypal/_files/quote_payflowpro.php
|
||||
* @magentoDataFixture Magento/Paypal/Fixtures/default_payment_configuration.php
|
||||
* @magentoAppArea adminhtml
|
||||
* @return void
|
||||
*/
|
||||
public function testRequestToken(): void
|
||||
{
|
||||
$quote = $this->getQuote('100000015');
|
||||
$quote->setStoreId(null);
|
||||
$this->execute($quote, 'def_partner', 'def_vendor', 'def_user', 'def_pwd');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a case when secure token can be obtained with credentials specified per store.
|
||||
*
|
||||
* @magentoDataFixture Magento/Paypal/_files/quote_payflowpro.php
|
||||
* @magentoDataFixture Magento/Paypal/Fixtures/store_payment_configuration.php
|
||||
* @magentoAppArea adminhtml
|
||||
* @return void
|
||||
*/
|
||||
public function testRequestTokenWithStoreConfiguration(): void
|
||||
{
|
||||
$quote = $this->getQuote('100000015');
|
||||
$store = $this->getStore('test');
|
||||
$quote->setStoreId($store->getId());
|
||||
$this->execute($quote, 'store_partner', 'store_vendor', 'store_user', 'store_pwd');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a case when secure token can be obtained with credentials specified per website.
|
||||
*
|
||||
* @magentoDataFixture Magento/Paypal/_files/quote_payflowpro.php
|
||||
* @magentoDataFixture Magento/Paypal/Fixtures/website_payment_configuration.php
|
||||
* @magentoAppArea adminhtml
|
||||
* @return void
|
||||
*/
|
||||
public function testRequestTokenWithWebsiteConfiguration(): void
|
||||
{
|
||||
$quote = $this->getQuote('100000015');
|
||||
$store = $this->getStore('fixture_second_store');
|
||||
$quote->setStoreId($store->getId());
|
||||
$this->execute($quote, 'website_partner', 'website_vendor', 'website_user', 'website_pwd');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves secure token and perform test assertions.
|
||||
*
|
||||
* @param Quote $quote
|
||||
* @param string $expPartner
|
||||
* @param string $expVendor
|
||||
* @param string $expUser
|
||||
* @param string $expPwd
|
||||
* @return void
|
||||
*/
|
||||
private function execute(
|
||||
Quote $quote,
|
||||
string $expPartner,
|
||||
string $expVendor,
|
||||
string $expUser,
|
||||
string $expPwd
|
||||
): void {
|
||||
$secureTokenId = '31f2a7c8d257c70b1c9eb9051b90e0';
|
||||
$token = '80IgSbabyj0CtBDWHZZeQN3';
|
||||
|
||||
$this->mathRandom->method('getUniqueHash')
|
||||
->willReturn($secureTokenId);
|
||||
|
||||
$response = new DataObject([
|
||||
'result' => '0',
|
||||
'respmsg' => 'Approved',
|
||||
'securetoken' => $token,
|
||||
'securetokenid' => $secureTokenId,
|
||||
'result_code' => '0',
|
||||
]);
|
||||
$self = $this;
|
||||
$this->gateway->method('postRequest')
|
||||
/** @var DataObject $request */
|
||||
->with(self::callback(function ($request) use ($self, $expPartner, $expVendor, $expUser, $expPwd) {
|
||||
$self->performAssertion($expPartner, $request->getPartner(), '{Partner}');
|
||||
$self->performAssertion($expVendor, $request->getVendor(), '{Vendor}');
|
||||
$self->performAssertion($expUser, $request->getUser(), '{User}');
|
||||
$self->performAssertion($expPwd, $request->getPwd(), '{Password}');
|
||||
|
||||
return true;
|
||||
}))
|
||||
->willReturn($response);
|
||||
|
||||
$response = $this->service->requestToken($quote);
|
||||
$this->performAssertion($token, $response->getData('securetoken'), '{Secure Token}');
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform assertions test assertions.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $actual
|
||||
* @param string $property
|
||||
* @return void
|
||||
*/
|
||||
private function performAssertion(string $expected, string $actual, string $property): void
|
||||
{
|
||||
self::assertEquals($expected, $actual, "$property should match.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads quote by order increment id.
|
||||
*
|
||||
* @param string $orderIncrementId
|
||||
* @return Quote
|
||||
*/
|
||||
private function getQuote(string $orderIncrementId): Quote
|
||||
{
|
||||
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
|
||||
$searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
|
||||
$searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $orderIncrementId)
|
||||
->create();
|
||||
|
||||
$items = $this->quoteRepository->getList($searchCriteria)
|
||||
->getItems();
|
||||
|
||||
return array_pop($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads store by provided code.
|
||||
*
|
||||
* @param string $code
|
||||
* @return StoreInterface
|
||||
*/
|
||||
private function getStore(string $code): StoreInterface
|
||||
{
|
||||
/** @var StoreRepositoryInterface $storeRepository */
|
||||
$storeRepository = $this->objectManager->get(StoreRepositoryInterface::class);
|
||||
return $storeRepository->get($code);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Paypal\Model\Payflow;
|
||||
|
||||
use Magento\Checkout\Api\PaymentInformationManagementInterface;
|
||||
use Magento\Framework\Api\SearchCriteriaBuilder;
|
||||
use Magento\Paypal\Model\Config;
|
||||
use Magento\Paypal\Model\Payflowpro;
|
||||
use Magento\Quote\Api\CartRepositoryInterface;
|
||||
use Magento\Quote\Api\Data\CartInterface;
|
||||
use Magento\Sales\Api\Data\TransactionInterface;
|
||||
use Magento\Sales\Api\OrderManagementInterface;
|
||||
use Magento\Sales\Api\OrderRepositoryInterface;
|
||||
use Magento\Sales\Api\TransactionRepositoryInterface;
|
||||
use Magento\Sales\Model\Order;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class TransparentTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var PaymentInformationManagementInterface
|
||||
*/
|
||||
private $management;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->management = $this->objectManager->get(PaymentInformationManagementInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a case when order should be placed in "Suspected Fraud" status based on account verification.
|
||||
*
|
||||
* @magentoDataFixture Magento/Checkout/_files/quote_with_shipping_method.php
|
||||
* @magentoConfigFixture current_store payment/payflowpro/active 1
|
||||
* @magentoConfigFixture current_store payment/payflowpro/payment_action Authorization
|
||||
* @magentoConfigFixture current_store payment/payflowpro/fmf 1
|
||||
*/
|
||||
public function testPlaceOrderSuspectedFraud(): void
|
||||
{
|
||||
$quote = $this->getQuote('test_order_1');
|
||||
$this->addFraudPayment($quote);
|
||||
$payment = $quote->getPayment();
|
||||
$pnref = $payment->getAdditionalInformation(Payflowpro::PNREF);
|
||||
|
||||
$orderId = (int)$this->management->savePaymentInformationAndPlaceOrder($quote->getId(), $payment);
|
||||
self::assertNotEmpty($orderId);
|
||||
|
||||
/** @var OrderRepositoryInterface $orderManagement */
|
||||
$orderManagement = $this->objectManager->get(OrderRepositoryInterface::class);
|
||||
$order = $orderManagement->get($orderId);
|
||||
|
||||
self::assertEquals(Order::STATUS_FRAUD, $order->getStatus());
|
||||
self::assertEquals(Order::STATE_PAYMENT_REVIEW, $order->getState());
|
||||
|
||||
$transactions = $this->getPaymentTransactionList((int) $orderId);
|
||||
self::assertCount(1, $transactions, 'Only one transaction should be present.');
|
||||
|
||||
/** @var TransactionInterface $transaction */
|
||||
$transaction = array_pop($transactions);
|
||||
self::assertEquals(
|
||||
$pnref,
|
||||
$transaction->getTxnId(),
|
||||
'Authorization transaction id should be equal to PNREF.'
|
||||
);
|
||||
|
||||
self::assertStringContainsString(
|
||||
'Order is suspended as an account verification transaction is suspected to be fraudulent.',
|
||||
$this->getOrderComment($orderId)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves quote by provided order ID.
|
||||
*
|
||||
* @param string $reservedOrderId
|
||||
* @return CartInterface
|
||||
*/
|
||||
private function getQuote(string $reservedOrderId): CartInterface
|
||||
{
|
||||
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
|
||||
$searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
|
||||
$searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
|
||||
->create();
|
||||
|
||||
/** @var CartRepositoryInterface $quoteRepository */
|
||||
$quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
$items = $quoteRepository->getList($searchCriteria)
|
||||
->getItems();
|
||||
|
||||
return array_pop($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets payment with fraud to quote.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function addFraudPayment(CartInterface $quote): void
|
||||
{
|
||||
$payment = $quote->getPayment();
|
||||
$payment->setMethod(Config::METHOD_PAYFLOWPRO);
|
||||
$payment->setAdditionalInformation(Payflowpro::PNREF, 'A90A0D1B361D');
|
||||
$payment->setAdditionalInformation('result_code', Payflowpro::RESPONSE_CODE_FRAUDSERVICE_FILTER);
|
||||
$payment->setCcType('VI');
|
||||
$payment->setCcLast4('1111');
|
||||
$payment->setCcExpMonth('3');
|
||||
$payment->setCcExpYear('2025');
|
||||
|
||||
/** @var CartRepositoryInterface $quoteRepository */
|
||||
$quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
$quoteRepository->save($quote);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of order transactions.
|
||||
*
|
||||
* @param int $orderId
|
||||
* @return TransactionInterface[]
|
||||
*/
|
||||
private function getPaymentTransactionList(int $orderId): array
|
||||
{
|
||||
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
|
||||
$searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
|
||||
$searchCriteria = $searchCriteriaBuilder->addFilter('order_id', $orderId)
|
||||
->create();
|
||||
|
||||
/** @var TransactionRepositoryInterface $transactionRepository */
|
||||
$transactionRepository = $this->objectManager->get(TransactionRepositoryInterface::class);
|
||||
return $transactionRepository->getList($searchCriteria)
|
||||
->getItems();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns order comment.
|
||||
*
|
||||
* @param int $orderId
|
||||
* @return string
|
||||
*/
|
||||
private function getOrderComment(int $orderId): string
|
||||
{
|
||||
/** @var OrderManagementInterface $orderManagement */
|
||||
$orderManagement = $this->objectManager->get(OrderManagementInterface::class);
|
||||
$comments = $orderManagement->getCommentsList($orderId)->getItems();
|
||||
$comment = reset($comments);
|
||||
|
||||
return $comment ? $comment->getComment() : '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user