Skip to content

Commit

Permalink
Merge pull request #4 from westwerk/feature/checkout_flow
Browse files Browse the repository at this point in the history
Feature/checkout flow
  • Loading branch information
RBech authored Jun 27, 2019
2 parents ac2c001 + 0ef2339 commit 968a7eb
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 73 deletions.
70 changes: 54 additions & 16 deletions Components/QuickPayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,69 @@

namespace QuickPayPayment\Components;

use Exception;
use Shopware\Components\Random;
use function Shopware;

class QuickPayService
{
private $baseUrl = 'https://api.quickpay.net';

const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_GET = 'GET';
const METHOD_PATCH = 'PATCH';

/**
* Create payment
*
* @param $orderId
* @param $currency
* @param $parameters
* @return mixed
*/
public function createPayment($parameters)
public function createPayment($orderId, $parameters)
{
$parameters['order_id'] = $orderId;

//Create payment
$payment = $this->request(self::METHOD_POST, '/payments', $parameters);

return $payment;
}

/**
* Create payment
*
* @param $paymentId
* @param $parameters
* @return mixed
*/
public function updatePayment($paymentId, $parameters)
{
$resource = sprintf('/payments/%s', $paymentId);

//Update payment
$payment = $this->request(self::METHOD_PATCH, $resource, $parameters);

return $payment;
}

/**
* Get payment information
*
* @param $paymentId
* @return mixed
*/
public function getPayment($paymentId)
{
$resource = sprintf('/payments/%s', $paymentId);

//Get payment
$payment = $this->request(self::METHOD_GET, $resource);

return $payment;
}

/**
* Create payment link
*
Expand Down Expand Up @@ -87,14 +127,14 @@ private function request($method = self::METHOD_POST, $resource, $params = [], $

//Validate reponsecode
if (! in_array($responseCode, [200, 201, 202])) {
throw new \Exception('Invalid gateway response ' . $result);
throw new Exception('Invalid gateway response ' . $result);
}

$response = json_decode($result);

//Check for JSON errors
if (! $response || (json_last_error() !== JSON_ERROR_NONE)) {
throw new \Exception('Invalid json response');
throw new Exception('Invalid json response');
}

return $response;
Expand Down Expand Up @@ -124,18 +164,6 @@ private function getApiKey()
return Shopware()->Config()->getByNamespace('QuickPayPayment', 'public_key');
}

/**
* Create payment token
*
* @param float $amount
* @param int $customerId
* @return string
*/
public function createPaymentToken($amount, $customerId)
{
return md5(implode('|', [$amount, $customerId]));
}

/**
* Get language code
*
Expand All @@ -147,4 +175,14 @@ private function getLanguageCode()

return substr($locale, 0, 2);
}

/**
* Creates a unique order id
*
* @return string
*/
public function createOrderId()
{
return Random::getAlphanumericString(20);
}
}
119 changes: 81 additions & 38 deletions Controllers/Frontend/QuickPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,28 @@ public function redirectAction()
$service = $this->container->get('quickpay_payment.quickpay_service');

try {
$user = $this->getUser();
$billing = $user['billingaddress'];

$paymentId = $this->createPaymentUniqueId();
$token = $service->createPaymentToken($this->getAmount(), $billing['customernumber']);

//Save order and grab ordernumber
$orderNumber = $this->saveOrder($paymentId, $token, \Shopware\Models\Order\Status::PAYMENT_STATE_OPEN);

//Save orderNumber to session
Shopware()->Session()->offsetSet('quickpay_order_id', $orderNumber);
Shopware()->Session()->offsetSet('quickpay_order_token', $token);

$paymentParameters = [
'order_id' => $orderNumber,
'currency' => $this->getCurrencyShortName(),
'variables' => [
'payment_id' => $paymentId,
'token' => $token
],
];

//Create payment
$payment = $service->createPayment($paymentParameters);


//Save order and grab ordernumber
$paymentId = Shopware()->Session()->offsetGet('quickpay_payment_id');
if(empty($paymentId))
{
//Create new QuickPay payment
$orderId = $service->createOrderId();

$payment = $service->createPayment($orderId, $paymentParameters);
}
else
{
//Update existing QuickPay payment
$payment = $service->updatePayment($paymentId, $paymentParameters);
}

// Save ID to session
Shopware()->Session()->offsetSet('quickpay_payment_id', $payment->id);

$user = $this->getUser();
$email = $user['additional']['user']['email'];

Expand All @@ -49,14 +46,7 @@ public function redirectAction()
$this->getCancelUrl(),
$this->getCallbackUrl()
);

$repository = Shopware()->Models()->getRepository(\Shopware\Models\Order\Order::class);
$order = $repository->findOneBy(array(
'number' => $orderNumber
));
$order->getAttribute()->setQuickpayPaymentLink($paymentLink);
Shopware()->Models()->flush($order->getAttribute());


$this->redirect($paymentLink);
} catch (\Exception $e) {
die($e->getMessage());
Expand Down Expand Up @@ -86,24 +76,21 @@ public function callbackAction()
//Check if payment is accepted
if ($response->accepted === true) {

//Check is test mode is enabled
$testmode = Shopware()->Config()->getByNamespace('QuickPayPayment', 'testmode');

//Cancel order if testmode is disabled and payment is test mode
if (!$testmode && ($response->test_mode === true)) {
if (!$this->checkTestMode($response)) {

//Set order as cancelled
$this->savePaymentStatus($response->variables->payment_id, $response->variables->token, \Shopware\Models\Order\Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED);
$this->savePaymentStatus($response->order_id, $response->id, \Shopware\Models\Order\Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED);
Shopware()->PluginLogger()->info("Order attempted paid with testcard while testmode was disabled");
return;
}

//Set order as reserved
$this->savePaymentStatus($response->variables->payment_id, $response->variables->token, \Shopware\Models\Order\Status::PAYMENT_STATE_RESERVED);
$this->savePaymentStatus($response->order_id, $response->id, \Shopware\Models\Order\Status::PAYMENT_STATE_RESERVED);
}
} else {
//Cancel order
$this->savePaymentStatus($response->variables->payment_id, $response->variables->token, \Shopware\Models\Order\Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED);
$this->savePaymentStatus($response->order_id, $response->id, \Shopware\Models\Order\Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED);
Shopware()->PluginLogger()->info('Checksum mismatch');
}
}
Expand All @@ -114,6 +101,42 @@ public function callbackAction()
*/
public function successAction()
{
/** @var \QuickPayPayment\Components\QuickPayService $service */
$service = $this->container->get('quickpay_payment.quickpay_service');

$paymentId = Shopware()->Session()->offsetGet('quickpay_payment_id');

if(empty($paymentId))
{
$this->redirect(['controller' => 'checkout', 'action' => 'confirm']);
return;
}

$payment = $service->getPayment($paymentId);
if(empty($payment) || !isset($payment->order_id))
{
$this->redirect(['controller' => 'checkout', 'action' => 'confirm']);
return;
}

$state = \Shopware\Models\Order\Status::PAYMENT_STATE_OPEN;
if($payment->accepted && $this->checkTestMode($payment))
{
$state = \Shopware\Models\Order\Status::PAYMENT_STATE_RESERVED;
}

$orderNumber = $this->saveOrder($payment->order_id, $payment->id, $state);

$repository = Shopware()->Models()->getRepository(\Shopware\Models\Order\Order::class);
$order = $repository->findOneBy(array(
'number' => $orderNumber
));
$order->getAttribute()->setQuickpayPaymentLink($payment->link->url);
Shopware()->Models()->flush($order->getAttribute());

//Remove ID from session
Shopware()->Session()->offsetUnset('quickpay_payment_id');

//Redirect to finish
$this->redirect(['controller' => 'checkout', 'action' => 'finish']);

Expand All @@ -125,7 +148,7 @@ public function successAction()
*/
public function cancelAction()
{
$this->redirect(['controller' => 'checkout', 'action' => 'cancel']);
$this->redirect(['controller' => 'checkout', 'action' => 'confirm']);
}

/**
Expand Down Expand Up @@ -178,4 +201,24 @@ private function getCallbackUrl()
public function getWhitelistedCSRFActions() {
return ['callback'];
}

/**
* Check if the text_mode property of the payment matches the shop configuration
*
* @param mixed $payment
* @return boolean
*/
private function checkTestMode($payment)
{
//Check is test mode is enabled
$testmode = Shopware()->Config()->getByNamespace('QuickPayPayment', 'testmode');

//Check if test_mode property matches the configuration
if (!$testmode && ($payment->test_mode === true)) {

return false;
}

return true;
}
}
68 changes: 49 additions & 19 deletions QuickPayPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
use Shopware\Components\Plugin\Context\UpdateContext;
use Shopware\Models\Payment\Payment;

class QuickPayPayment extends Plugin
Expand Down Expand Up @@ -33,19 +34,21 @@ public function install(InstallContext $context)
];

$installer->createOrUpdate($context->getPlugin(), $options);

$crud = $this->container->get('shopware_attribute.crud_service');
$crud->update('s_order_attributes', 'quickpay_payment_link', 'string', array(
'displayInBackend' => true,
'label' => 'QuickPay payment link'
), null, false, 'NULL');

Shopware()->Models()->generateAttributeModels(
array('s_order_attributes')
);


$this->createAttributes();
}

/**
* Update plugin
*
* @param UpdateContext $context
*/
public function update(UpdateContext $context)
{
$this->createAttributes();

}

/**
* Uninstall plugin
*
Expand All @@ -55,14 +58,7 @@ public function uninstall(UninstallContext $context)
{
$this->setActiveFlag($context->getPlugin()->getPayments(), false);

$crud = $this->container->get('shopware_attribute.crud_service');
try {
$crud->delete('s_order_attributes', 'quickpay_payment_link');
} catch (\Exception $e) {
}
Shopware()->Models()->generateAttributeModels(
array('s_order_attributes')
);
$this->removeAttributes();
}

/**
Expand Down Expand Up @@ -98,4 +94,38 @@ private function setActiveFlag($payments, $active)
}
$em->flush();
}

/**
* Create or update all Attributes
*
*/
private function createAttributes()
{

$crud = $this->container->get('shopware_attribute.crud_service');
$crud->update('s_order_attributes', 'quickpay_payment_link', 'string', array(
'displayInBackend' => true,
'label' => 'QuickPay payment link'
), null, false, 'NULL');

Shopware()->Models()->generateAttributeModels(
array('s_order_attributes')
);

}

/**
* Remove all attributes
*/
private function removeAttributes()
{
$crud = $this->container->get('shopware_attribute.crud_service');
try {
$crud->delete('s_order_attributes', 'quickpay_payment_link');
} catch (\Exception $e) {
}
Shopware()->Models()->generateAttributeModels(
array('s_order_attributes')
);
}
}

0 comments on commit 968a7eb

Please sign in to comment.