magento2-plugin/Model/BTCPay.php
2020-01-06 11:41:53 +01:00

112 lines
3.3 KiB
PHP

<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Storefront\BTCPay\Model;
use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Payment\Model\Method\AbstractMethod;
/**
* Pay In Store payment method model
*/
class BTCPay extends AbstractMethod {
const PAYMENT_METHOD_CODE = 'btcpay';
/**
* Payment code
*
* @var string
*/
protected $_code = self::PAYMENT_METHOD_CODE;
/**
* Payment Method feature
*
* @var bool
*/
protected $_isGateway = true;
/**
* Payment Method feature
*
* @var bool
*/
protected $_isInitializeNeeded = true;
/**
* Availability option
*
* @var bool
*/
protected $_isOffline = true;
/**
* @var \Magento\Framework\UrlInterface
*/
private $url;
public function __construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory, \Magento\Payment\Helper\Data $paymentData, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\UrlInterface $url, \Magento\Payment\Model\Method\Logger $logger, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $data = [], DirectoryHelper $directory = null) {
parent::__construct($context, $registry, $extensionFactory, $customAttributeFactory, $paymentData, $scopeConfig, $logger, $resource, $resourceCollection, $data, $directory);
$this->url = $url;
}
public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) {
$r = parent::isAvailable($quote);
if ($r) {
$token = $this->getConfigData('token');
if (!$token) {
// Hide the payment method, no token entered
$r = false;
}
}
return $r;
}
public function getOrderPlaceRedirectUrl(){
$r = $this->url->getUrl('btcpay/redirect/forwardtopayment', [
'_secure' => true,
'_nosid' => true
]);
return $r;
}
public function getConfigData($field, $storeId = null) {
if ($field === 'order_place_redirect_url') {
return $this->getOrderPlaceRedirectUrl();
} else {
return parent::getConfigData($field, $storeId); // TODO: Change the autogenerated stub
}
}
/**
* Method that will be executed instead of authorize or capture
* if flag isInitializeNeeded set to true
*
* @param string $paymentAction
* @param object $stateObject
*
* @return \Storefront\PayIngenico\Model\Payment\PaymentAbstract
*/
public function initialize($paymentAction, $stateObject) {
$stateObject->setState(\Magento\Sales\Model\Order::STATE_NEW)->setStatus(\Magento\Sales\Model\Order::STATE_PENDING_PAYMENT);
$message = __('Customer is forwarded to BTCPay Server to pay. Awaiting feedback.');
/** @var \Magento\Sales\Model\Order $order */
$order = $this->getInfoInstance()->getOrder();
$order->addStatusHistoryComment($message);
return $this;
}
}