Skip to content

Instantly share code, notes, and snippets.

@marysmech
Last active January 29, 2018 09:27
Show Gist options
  • Save marysmech/25a1e8dfcaa9181e4ca6314b0340fedb to your computer and use it in GitHub Desktop.
Save marysmech/25a1e8dfcaa9181e4ca6314b0340fedb to your computer and use it in GitHub Desktop.
GoPay module for Drupal 8 - example
my_module.gopay.test:
path: '/my-module/gopay/test'
defaults:
_controller: '\Drupal\my_module\Controller\MyPurchaseController::testCallback'
_title: 'GoPay Test'
my_module.gopay.test.result:
path: '/my-module/test/result'
defaults:
_controller: '\Drupal\my_module\Controller\MyPurchaseController::testCallbackResult'
_title: 'GoPay Test Result'
<?php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\gopay\GoPayFactoryInterface;
use Drupal\Core\Url;
use Drupal\gopay\GoPayState;
use GoPay\Definition\Payment\Currency;
use Symfony\Component\HttpFoundation\Request;
use Drupal\gopay\Response\PaymentResponseInterface;
class MyPurchaseController extends ControllerBase {
/**
* GoPay Factory.
*
* @var \Drupal\gopay\GoPayFactoryInterface
*/
protected $goPayFactory;
public function __construct(GoPayFactoryInterface $go_pay_factory) {
$this->goPayFactory = $go_pay_factory;
}
public static function create(ContainerInterface $container) {
return new static(
$container->get('gopay.factory')
);
}
/**
* Test callback page for GoPay.
*/
public function testCallback() {
$pay_it = $this->goPayFactory->createStandardPayment()
->setCurrency(Currency::EUROS)
->setDefaultPaymentInstrument(PaymentInstrument::PAYMENT_CARD)
->setAllowedPaymentInstruments([PaymentInstrument::PAYMENT_CARD, PaymentInstrument::BANK_ACCOUNT])
->setContact($this->goPayFactory->createContact()
->setFirstName('John')
->setLastName('Smith')
->setEmail('jsmith@gmail.com')
->setCity('City')
->setPhoneNumber(123456)
->setStreet('Main 1')
->setPostalCode(13)
->setCountryCode('USA')
)
->addItem($this->goPayFactory->createItem()
->setName('laptop')
->setAmount(900, FALSE)
)
->addItem($this->goPayFactory->createItem()
->setName('beer')
->setAmount(150)
)
->setAmount(20150)
->setOrderNumber(123)
->setOrderDescription('description')
->setReturnUrl(Url::fromRoute(
'my_module.gopay.test.result',
[],
[
'absolute' => TRUE,
]
)->toString()
);
return $pay_it->buildInlineForm();
}
/**
* Test callback page to return from GoPay.
*/
public function testCallbackResult(Request $request) {
// GoPay return payment id in URL.
$payment_id = $request->get('id');
// Load payment status from GoPay.
$payment_response = $this->goPayFactory->createResponseStatus($payment_id);
if ($payment_response->isPaid()) {
// Payment ok.
drupal_set_message('Payment paid.');
}
else {
// Payment error or canceled.
$state = $payment_response->getState();
drupal_set_message('Payment error: ' . GoPayState::getStateDescription($state), 'error');
}
return [
'#markup' => 'Test payment callback result.',
];
}
}
<?php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\gopay\GoPayFactoryInterface;
use Drupal\Core\Url;
use Drupal\gopay\GoPayState;
use GoPay\Definition\Payment\Currency;
use Symfony\Component\HttpFoundation\Request;
use Drupal\gopay\Response\PaymentResponseInterface;
class MyPurchaseController extends ControllerBase {
/**
* GoPay Factory.
*
* @var \Drupal\gopay\GoPayFactoryInterface
*/
protected $goPayFactory;
public function __construct(GoPayFactoryInterface $go_pay_factory) {
$this->goPayFactory = $go_pay_factory;
}
public static function create(ContainerInterface $container) {
return new static(
$container->get('gopay.factory')
);
}
// Method testCallback goes here.
// Method testCallbackResult goes here.
}
<?php
/**
* Test callback page for GoPay.
*/
public function testCallback() {
$pay_it = $this->goPayFactory->createStandardPayment()
->setCurrency(Currency::EUROS)
->setDefaultPaymentInstrument(PaymentInstrument::PAYMENT_CARD)
->setAllowedPaymentInstruments([PaymentInstrument::PAYMENT_CARD, PaymentInstrument::BANK_ACCOUNT])
->setContact($this->goPayFactory->createContact()
->setFirstName('John')
->setLastName('Smith')
->setEmail('jsmith@gmail.com')
->setCity('City')
->setPhoneNumber(123456)
->setStreet('Main 1')
->setPostalCode(13)
->setCountryCode('USA')
)
->addItem($this->goPayFactory->createItem()
->setName('laptop')
->setAmount(900, FALSE)
)
->addItem($this->goPayFactory->createItem()
->setName('beer')
->setAmount(150)
)
->setAmount(20150)
->setOrderNumber(123)
->setOrderDescription('description')
->setReturnUrl(Url::fromRoute(
'my_module.gopay.test.result',
[],
[
'absolute' => TRUE,
]
)->toString()
);
return $pay_it->buildInlineForm();
}
<?php
/**
* Test callback page to return from GoPay.
*/
public function testCallbackResult(Request $request) {
// GoPay return payment id in URL.
$payment_id = $request->get('id');
// Load payment status from GoPay.
$payment_response = $this->goPayFactory->createResponseStatus($payment_id);
if ($payment_response->isPaid()) {
// Payment ok.
drupal_set_message('Payment paid.');
}
else {
// Payment error or canceled.
$state = $payment_response->getState();
drupal_set_message('Payment error: ' . GoPayState::getStateDescription($state), 'error');
}
return [
'#markup' => 'Test payment callback result.',
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment