Skip to content

Commit

Permalink
Import files
Browse files Browse the repository at this point in the history
  • Loading branch information
winzou committed Jul 18, 2017
1 parent 7aa2e55 commit a4837b9
Show file tree
Hide file tree
Showing 9 changed files with 1,624 additions and 4 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
composer.phar
/vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
/bin/
97 changes: 97 additions & 0 deletions Action/CaptureAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Villafinder\Payum2c2p\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\ApiAwareInterface;
use Payum\Core\ApiAwareTrait;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Reply\Base;
use Payum\Core\Reply\HttpPostRedirect;
use Payum\Core\Request\Capture;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Request\GetHttpRequest;
use Payum\Core\Request\Notify;
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
use Payum\Core\Security\GenericTokenFactoryAwareTrait;
use Villafinder\Payum2c2p\Api;

/**
* @property Api $api
*/
class CaptureAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface
{
use ApiAwareTrait;
use GatewayAwareTrait;
use GenericTokenFactoryAwareTrait;

public function __construct()
{
$this->apiClass = Api::class;
}

/**
* @param Capture $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$model = ArrayObject::ensureArrayObject($request->getModel());

// Ensure payment currency is configured, Exception is thrown otherwise
$this->api->getCurrencyConfigByCode($request->getFirstModel()->getCurrencyCode());

$httpRequest = new GetHttpRequest();
$this->gateway->execute($httpRequest);

// We are back from 2c2p
if (isset($httpRequest->request['payment_status'])) {
// Only if we trust user request, we can handle the request
if ($this->api->trustUserRequest()) {
try {
$notify = new Notify($model);
$this->gateway->execute($notify);
} catch (Base $e) {
// NotifyAction throws a request we don't want to use here, we just skip
}
}

return;
}

// User will come back to this URL
if (empty($model['result_url_1']) && $request->getToken()) {
$model['result_url_1'] = $request->getToken()->getTargetUrl();
}

// Server-to-server notification will be sent to this URL
if (empty($model['result_url_2']) && $request->getToken() && $this->tokenFactory) {
$notifyToken = $this->tokenFactory->createNotifyToken(
$request->getToken()->getGatewayName(),
$request->getToken()->getDetails()
);

$model['result_url_2'] = $notifyToken->getTargetUrl();
}

// We POST redirect to 2c2p
throw new HttpPostRedirect(
$this->api->getOffsiteUrl(),
$this->api->prepareOffsitePayment($model->toUnsafeArray())
);
}

/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof Capture &&
$request->getModel() instanceof \ArrayAccess
;
}
}
50 changes: 50 additions & 0 deletions Action/ConvertPaymentAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Villafinder\Payum2c2p\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Model\PaymentInterface;
use Payum\Core\Request\Convert;
use Payum\ISO4217\Currency;
use Payum\ISO4217\ISO4217;

class ConvertPaymentAction implements ActionInterface
{
/**
* @param Convert $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

/** @var PaymentInterface $payment */
$payment = $request->getSource();

$iso4217 = new ISO4217();
/** @var Currency $currency */
$currency = $iso4217->findByCode($payment->getCurrencyCode());

$details = ArrayObject::ensureArrayObject($payment->getDetails());
$details['payment_description'] = $payment->getDescription();
$details['currency'] = $currency->getNumeric();
$details['amount'] = sprintf('%012d', $payment->getTotalAmount());
$details['customer_email'] = $payment->getClientEmail();
$details['order_id'] = $payment->getNumber();

$request->setResult((array) $details);
}

/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof Convert &&
$request->getSource() instanceof PaymentInterface &&
$request->getTo() == 'array'
;
}
}
69 changes: 69 additions & 0 deletions Action/NotifyAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Villafinder\Payum2c2p\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\ApiAwareInterface;
use Payum\Core\ApiAwareTrait;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Reply\HttpResponse;
use Payum\Core\Request\GetHttpRequest;
use Payum\Core\Request\Notify;
use Villafinder\Payum2c2p\Api;

/**
* @property Api $api
*/
class NotifyAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
{
use ApiAwareTrait;
use GatewayAwareTrait;

public function __construct()
{
$this->apiClass = Api::class;
}

/**
* @param Notify $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$model = ArrayObject::ensureArrayObject($request->getModel());

$httpRequest = new GetHttpRequest();
$this->gateway->execute($httpRequest);

if ('POST' !== $httpRequest->method) {
throw new HttpResponse('Notification is invalid. Code 1', 400);
}

if (!$this->api->checkResponseHash($httpRequest->request)) {
throw new HttpResponse('Notification is invalid. Code 2', 400);
}

if ($model['amount'] != $httpRequest->request['amount']) {
throw new HttpResponse('Notification is invalid. Code 3', 400);
}

$model->replace($httpRequest->request);

throw new HttpResponse('OK', 200);
}

/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof Notify &&
$request->getModel() instanceof \ArrayAccess
;
}
}
47 changes: 47 additions & 0 deletions Action/StatusAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Villafinder\Payum2c2p\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Request\GetStatusInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Villafinder\Payum2c2p\Api;

class StatusAction implements ActionInterface
{
/**
* @param GetStatusInterface $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$model = ArrayObject::ensureArrayObject($request->getModel());

if (!isset($model['payment_status'])) {
$request->markNew();
} elseif (Api::STATUS_SUCCESS === $model['payment_status']) {
$request->markCaptured();
} elseif (Api::STATUS_PENDING === $model['payment_status']) {
$request->markPending();
} elseif (Api::STATUS_CANCEL === $model['payment_status']) {
$request->markCanceled();
} elseif (Api::STATUS_REJECTED === $model['payment_status']) {
$request->markFailed();
} else {
$request->markUnknown();
}
}

/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof GetStatusInterface &&
$request->getModel() instanceof \ArrayAccess
;
}
}
Loading

0 comments on commit a4837b9

Please sign in to comment.