-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from villafinder/redirect-url
Pass redirect URL to 2c2p user defined parameters
- Loading branch information
Showing
6 changed files
with
198 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Villafinder\Payum2c2p\Action; | ||
|
||
use Payum\Core\Action\ActionInterface; | ||
use Payum\Core\ApiAwareInterface; | ||
use Payum\Core\ApiAwareTrait; | ||
use Payum\Core\Exception\LogicException; | ||
use Payum\Core\GatewayAwareInterface; | ||
use Payum\Core\GatewayAwareTrait; | ||
use Payum\Core\Reply\HttpPostRedirect; | ||
use Payum\Core\Reply\HttpResponse; | ||
use Payum\Core\Request\Capture; | ||
use Payum\Core\Exception\RequestNotSupportedException; | ||
use Payum\Core\Request\GetHttpRequest; | ||
use Payum\Core\Security\GenericTokenFactoryAwareInterface; | ||
use Payum\Core\Security\GenericTokenFactoryAwareTrait; | ||
use Villafinder\Payum2c2p\Api; | ||
|
||
/** | ||
* @property Api $api | ||
*/ | ||
class CaptureOnsiteNullAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface | ||
{ | ||
use ApiAwareTrait; | ||
use GatewayAwareTrait; | ||
use GenericTokenFactoryAwareTrait; | ||
use CheckRequestOnsiteTrait; | ||
|
||
public function __construct() | ||
{ | ||
$this->apiClass = Api::class; | ||
} | ||
|
||
/** | ||
* @param Capture $request | ||
*/ | ||
public function execute($request) | ||
{ | ||
RequestNotSupportedException::assertSupports($this, $request); | ||
|
||
$httpRequest = new GetHttpRequest(); | ||
$this->gateway->execute($httpRequest); | ||
|
||
if (!$this->isBackFrom2c2p($httpRequest)) { | ||
return; | ||
} | ||
|
||
if (!$this->api->trustUserRequest()) { | ||
return; | ||
} | ||
|
||
try { | ||
$response = $this->readFromRequest($httpRequest); | ||
} catch (LogicException $e) { | ||
throw new HttpResponse($e->getMessage(), 400); | ||
} | ||
|
||
$redirectIndex = sprintf('userDefined%d', CaptureOnsiteAction::USER_DEFINED_URL); | ||
|
||
if (!isset($response[$redirectIndex])) { | ||
throw new HttpResponse('Redirect URL is missing from 2C2P response.', 400); | ||
} | ||
|
||
$redirectUrl = $response[$redirectIndex]; | ||
|
||
if (!filter_var($redirectUrl, FILTER_VALIDATE_URL)) { | ||
throw new HttpResponse('Redirect URL is invalid.', 400); | ||
} | ||
|
||
throw new HttpPostRedirect( | ||
$redirectUrl, | ||
$httpRequest->request | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function supports($request) | ||
{ | ||
return | ||
$request instanceof Capture && | ||
null === $request->getModel() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Villafinder\Payum2c2p\Action; | ||
|
||
use Payum\Core\Action\ActionInterface; | ||
use Payum\Core\ApiAwareInterface; | ||
use Payum\Core\ApiAwareTrait; | ||
use Payum\Core\Exception\LogicException; | ||
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\GetToken; | ||
use Payum\Core\Request\Notify; | ||
use Payum\Core\Security\TokenInterface; | ||
use Villafinder\Payum2c2p\Api; | ||
|
||
/** | ||
* @property Api $api | ||
*/ | ||
class NotifyOnsiteNullAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface | ||
{ | ||
use ApiAwareTrait; | ||
use GatewayAwareTrait; | ||
use CheckRequestOnsiteTrait; | ||
|
||
public function __construct() | ||
{ | ||
$this->apiClass = Api::class; | ||
} | ||
|
||
/** | ||
* @param Notify $request | ||
*/ | ||
public function execute($request) | ||
{ | ||
RequestNotSupportedException::assertSupports($this, $request); | ||
|
||
$httpRequest = new GetHttpRequest(); | ||
$this->gateway->execute($httpRequest); | ||
|
||
try { | ||
$response = $this->readFromRequest($httpRequest); | ||
} catch (LogicException $e) { | ||
throw new HttpResponse($e->getMessage(), 400); | ||
} | ||
|
||
$tokenIndex = sprintf('userDefined%d', CaptureOnsiteAction::USER_DEFINED_TOKEN); | ||
|
||
if (!isset($response[$tokenIndex])) { | ||
throw new HttpResponse('Token is missing from 2C2P response.', 400); | ||
} | ||
|
||
$this->gateway->execute($getToken = new GetToken($response[$tokenIndex])); | ||
|
||
if (!$getToken->getToken() instanceof TokenInterface) { | ||
throw new HttpResponse('Token does not exist.', 400); | ||
} | ||
|
||
$this->gateway->execute(new Notify($getToken->getToken())); | ||
|
||
throw new HttpResponse('OK', 200); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function supports($request) | ||
{ | ||
return | ||
$request instanceof Notify && | ||
null === $request->getModel() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters