public function Sermepa::processRequest in Commerce sermepa 8.2
Processes the notification request.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The request.
\Drupal\commerce_order\Entity\OrderInterface $order: The order.
Return value
bool TRUE if the payment is valid, otherwise FALSE.
Throws
\CommerceRedsys\Payment\SermepaException
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
\Drupal\Core\Entity\EntityStorageException
\Drupal\Core\TypedData\Exception\MissingDataException
2 calls to Sermepa::processRequest()
- Sermepa::onNotify in src/
Plugin/ Commerce/ PaymentGateway/ Sermepa.php - Processes the notification request.
- Sermepa::onReturn in src/
Plugin/ Commerce/ PaymentGateway/ Sermepa.php - Processes the "return" request.
File
- src/
Plugin/ Commerce/ PaymentGateway/ Sermepa.php, line 329
Class
- Sermepa
- Provides the Sermepa/Redsýs payment gateway.
Namespace
Drupal\commerce_sermepa\Plugin\Commerce\PaymentGatewayCode
public function processRequest(Request $request, OrderInterface $order = NULL) {
// Capture received data values.
$feedback = [
'Ds_SignatureVersion' => $request
->get('Ds_SignatureVersion'),
'Ds_MerchantParameters' => $request
->get('Ds_MerchantParameters'),
'Ds_Signature' => $request
->get('Ds_Signature'),
];
if (empty($feedback['Ds_SignatureVersion']) || empty($feedback['Ds_MerchantParameters']) || empty($feedback['Ds_Signature'])) {
throw new PaymentGatewayException('Bad feedback response, missing feedback parameter.');
}
// Get the payment method settings.
$payment_method_settings = $this
->getConfiguration();
// Create a new instance of the Sermepa library and initialize it.
$gateway = new SermepaApi($payment_method_settings['merchant_name'], $payment_method_settings['merchant_code'], $payment_method_settings['merchant_terminal'], $payment_method_settings['merchant_password'], $this
->getMode());
// Get order number from feedback data and compare it with the order object
// argument or loaded.
$parameters = $gateway
->decodeMerchantParameters($feedback['Ds_MerchantParameters']);
$order_id = $parameters['Ds_MerchantData'];
if ($order === NULL) {
$order_storage = $this->entityTypeManager
->getStorage('commerce_order');
$order = $order_storage
->load($order_id);
}
if ($order === NULL || $order
->id() != $order_id) {
$this->logger
->warning('The received order ID and the argument order ID does not match.');
}
// The onNotify and onReturn methods can collide causing a race condition.
if ($this->lock
->acquire($this
->getLockName($order))) {
// Validate feedback values.
if (!$gateway
->validSignatures($feedback)) {
$this->lock
->release($this
->getLockName($order));
throw new PaymentGatewayException('Bad feedback response, signatures does not match.');
}
if ($gateway
->authorizedResponse($parameters['Ds_Response'])) {
/** @var \Drupal\commerce_payment\PaymentStorageInterface $payment_storage */
$payment_storage = $this->entityTypeManager
->getStorage('commerce_payment');
// Check if the payment has been processed, we could have multiple
// payments.
$payments = $payment_storage
->getQuery()
->condition('payment_gateway', $this->parentEntity
->id())
->condition('order_id', $order
->id())
->condition('remote_id', $parameters['Ds_AuthorisationCode'])
->execute();
if (empty($payments)) {
/** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
$payment = $payment_storage
->create([
'state' => 'authorization',
'amount' => $order
->getTotalPrice(),
'payment_gateway' => $this->parentEntity
->id(),
'order_id' => $order
->id(),
'test' => $this
->getMode() == 'test',
'remote_id' => $parameters['Ds_AuthorisationCode'],
'remote_state' => SermepaApi::handleResponse($parameters['Ds_Response']),
'authorized' => $this->time
->getRequestTime(),
]);
$status_mapping = $this
->getStatusMapping();
if (isset($status_mapping[$this
->getConfiguration()['transaction_type']])) {
$payment
->setState($status_mapping[$this
->getConfiguration()['transaction_type']]);
}
if (!$order
->get('payment_method')
->isEmpty()) {
/** @var \Drupal\Core\Entity\Plugin\DataType\EntityReference $credit_card */
$credit_card = $order
->get('payment_method')
->first()
->get('entity')
->getTarget()
->getValue();
$payment
->set('payment_method', $credit_card)
->save();
}
$payment
->save();
}
$this->lock
->release($this
->getLockName($order));
return TRUE;
}
$this->lock
->release($this
->getLockName($order));
throw new PaymentGatewayException('Failed attempt, the payment could not be made.');
}
}