View source
<?php
namespace Drupal\mollie_payment\Plugin\Payment\Method;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\Token;
use Drupal\payment\Entity\Payment;
use Drupal\payment\Entity\PaymentInterface;
use Drupal\payment\EventDispatcherInterface;
use Drupal\payment\Plugin\Payment\Status\PaymentStatusManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\payment\OperationResult;
use Drupal\payment\Plugin\Payment\Method\PaymentMethodBase;
use Drupal\payment\Plugin\Payment\Method\PaymentMethodCapturePaymentInterface;
use Drupal\payment\Plugin\Payment\Method\PaymentMethodRefundPaymentInterface;
use Drupal\payment\Plugin\Payment\Method\PaymentMethodUpdatePaymentStatusInterface;
use Drupal\payment\Response\Response;
use Drupal\mollie_payment\Entity\MollieProfile;
use Mollie_API_Client;
class MolliePayment extends PaymentMethodBase implements ContainerFactoryPluginInterface, PaymentMethodCapturePaymentInterface, PaymentMethodRefundPaymentInterface, PaymentMethodUpdatePaymentStatusInterface {
protected $paymentStatusManager;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ModuleHandlerInterface $module_handler, EventDispatcherInterface $event_dispatcher, Token $token, PaymentStatusManagerInterface $payment_status_manager) {
$configuration += $this
->defaultConfiguration();
parent::__construct($configuration, $plugin_id, $plugin_definition, $module_handler, $event_dispatcher, $token, $payment_status_manager);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'), $container
->get('payment.event_dispatcher'), $container
->get('token'), $container
->get('plugin.manager.payment.status'));
}
public function getSupportedCurrencies() {
return TRUE;
}
public function getEntityId() {
return $this->pluginDefinition['entity_id'];
}
public function getExecuteStatusId() {
return $this->pluginDefinition['execute_status_id'];
}
public function getCaptureStatusId() {
return $this->pluginDefinition['capture_status_id'];
}
public function getCapture() {
return $this->pluginDefinition['capture'];
}
public function getRefundStatusId() {
return $this->pluginDefinition['refund_status_id'];
}
public function getRefund() {
return $this->pluginDefinition['refund'];
}
public function getMollieClient() {
$profile_id = $this->pluginDefinition['profile'];
$profile = MollieProfile::load($profile_id);
return $profile
->getMollieClient();
}
protected function doExecutePayment() {
$mollie_client = $this
->getMollieClient();
$redirect_url = new Url('mollie_payment.redirect', [
'payment' => $this
->getPayment()
->id(),
], [
'absolute' => TRUE,
]);
$webhook_url = new Url('mollie_payment.webhook', [
'payment' => $this
->getPayment()
->id(),
], [
'absolute' => TRUE,
]);
$payment_data = [
'amount' => $this
->getPayment()
->getAmount(),
'description' => $this
->getPayment()
->getPaymentType()
->getPaymentDescription(),
'redirectUrl' => $redirect_url
->toString(),
'webhookUrl' => $webhook_url
->toString(),
'metadata' => Json::encode([
'id' => $this
->getPayment()
->id(),
]),
];
try {
$mollie_payment = $mollie_client->payments
->create($payment_data);
$this
->getPayment()
->set('mollie_payment_id', $mollie_payment->id)
->save();
$redirect_url = $mollie_payment
->getPaymentUrl();
$url = Url::fromUri($redirect_url);
$response = new Response($url);
return new OperationResult($response);
} catch (Mollie_API_Exception $e) {
drupal_set_message($e
->getMessage(), 'error');
return;
}
}
public function getPaymentExecutionResult() {
$mollie_client = $this
->getMollieClient();
$mollie_transaction_id = $this
->getPayment()
->get('mollie_payment_id')->value;
$mollie_payment = $mollie_client->payments
->get($mollie_transaction_id);
$response = new Response(Url::fromUri($mollie_payment
->getPaymentUrl()));
return new OperationResult($response);
}
public function doCapturePayment() {
$this
->getPayment()
->setPaymentStatus($this->paymentStatusManager
->createInstance($this
->getCaptureStatusId()));
$this
->getPayment()
->save();
}
public function doCapturePaymentAccess(AccountInterface $account) {
return $this
->getCapture() && $this
->getPayment()
->getPaymentStatus()
->getPluginId() != $this
->getCaptureStatusId();
}
public function doRefundPayment() {
$this
->getPayment()
->setPaymentStatus($this->paymentStatusManager
->createInstance($this
->getRefundStatusId()));
$this
->getPayment()
->save();
}
public function doRefundPaymentAccess(AccountInterface $account) {
return $this
->getRefund() && $this
->getPayment()
->getPaymentStatus()
->getPluginId() != $this
->getRefundStatusId();
}
public function updatePaymentStatusAccess(AccountInterface $account) {
return AccessResult::forbidden();
}
public function getSettablePaymentStatuses(AccountInterface $account, PaymentInterface $payment) {
return [];
}
public function updatePaymentStatus(Payment $payment, $payment_status) {
$payment
->setPaymentStatus($payment
->getPaymentMethod()->paymentStatusManager
->createInstance($payment_status));
$payment
->save();
}
}