uc_gc_client-8.x-1.x-dev/src/Form/PaymentCancel.php
src/Form/PaymentCancel.php
<?php
namespace Drupal\uc_gc_client\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
// Use Drupal\uc_order\Entity\Order;.
use Drupal\uc_gc_client\Controller\GoCardlessPartner;
/**
* Defines a confirmation form for cancelling pending payments.
*/
class PaymentCancel extends ConfirmFormBase {
/**
* The Ubercart order ID of the item to cancel.
*
* @var int
*/
protected $ucid;
/**
* The commerce payment ID of the item to cancel.
*
* @var int
*/
protected $paymentId;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'uc_gc_client_payment_cancel';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to cancel GoCardless payment @paymentId?', [
'@paymentId' => $this->paymentId,
]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url(
'uc_gc_client.payments_form', [
'uc_order' => $this->ucid,
]
);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Cancel payment');
}
/**
* {@inheritdoc}
*/
public function getCancelText() {
return $this->t('Go back');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $ucid = NULL, $payment_id = NULL) {
$this->ucid = $ucid;
$this->paymentId = $payment_id;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$partner = new GoCardlessPartner();
$result = $partner->api(
[
'endpoint' => 'payments',
'action' => 'cancel',
'id' => $this->paymentId,
]
);
if ($result->response->status_code == 200) {
drupal_set_message($this->t('Payment @paymentId has been cancelled with GoCardless.', ['@paymentId' => $this->paymentId]));
}
else {
drupal_set_message($this->t('Something went wrong cancelling paynent @paymentId with GoCardless.', ['@paymentId' => $this->paymentId]), 'warning');
}
$form_state->setRedirect(
'uc_gc_client.payments_form', [
'uc_order' => $this->ucid,
]
);
}
}
