PayflowLinkForm.php in Commerce PayPal 8
File
src/PluginForm/PayflowLinkForm.php
View source
<?php
namespace Drupal\commerce_paypal\PluginForm;
use Drupal\commerce_payment\Exception\PaymentGatewayException;
use Drupal\commerce_payment\PluginForm\PaymentOffsiteForm as BasePaymentOffsiteForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
class PayflowLinkForm extends BasePaymentOffsiteForm {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$configuration = $this->plugin
->getConfiguration();
$redirect_mode = $configuration['redirect_mode'];
foreach ([
'partner',
'vendor',
'user',
'password',
] as $key) {
if (empty($configuration[$key])) {
throw new PaymentGatewayException($this
->t('Payflow Link is not configured for use. Please contact an administrator to resolve this issue.'));
}
}
$payment = $this->entity;
$order = $payment
->getOrder();
$mode = $configuration['mode'];
$commerce_payflow_data = [
'tokenid' => str_replace('.', '', uniqid('', TRUE)),
];
$order
->setData('commerce_payflow', $commerce_payflow_data);
$token = $this->plugin
->createSecureToken($order);
if (!empty($token)) {
$commerce_payflow_data['token'] = $token;
$order
->setData('commerce_payflow', $commerce_payflow_data);
$order
->save();
switch ($configuration['redirect_mode']) {
case 'get':
$redirect_url = $this->plugin
->getRedirectUrl($order);
if (empty($redirect_url)) {
throw new PaymentGatewayException($this
->t('Communication with PayPal failed. Please try again or contact an administrator to resolve the issue.'));
}
$redirect = new TrustedRedirectResponse($redirect_url);
$redirect
->send();
break;
case 'post':
$redirect_url = $this->plugin
->getRedirectUrl();
$form['#action'] = $redirect_url;
$data = [
'SECURETOKEN' => $order
->getData('commerce_payflow')['token'],
'SECURETOKENID' => $order
->getData('commerce_payflow')['tokenid'],
];
if ($mode === 'test') {
$data['MODE'] = 'TEST';
}
foreach ($data as $name => $value) {
if (!empty($value)) {
$form[$name] = [
'#type' => 'hidden',
'#value' => $value,
];
}
}
break;
}
}
else {
$order
->unsetData('commerce_payflow');
$order
->save();
throw new PaymentGatewayException($this
->t('Communication with PayPal failed. Please try again or contact an administrator to resolve the issue.'));
}
$form = $this
->buildRedirectForm($form, $form_state, $redirect_url, $data, $redirect_mode);
return $form;
}
}