-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormWithRedirectStrategy.php
109 lines (93 loc) · 3.76 KB
/
FormWithRedirectStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Created by PhpStorm.
* User: vasiliy
* Date: 6/3/14
* Time: 3:31 PM
*/
namespace TwoDudes\FXPay\HandlerStrategy;
use TwoDudes\FXPay\Events\AfterBuildPaymentResponseEvent;
use TwoDudes\FXPay\Events\AfterFormBuildEvent;
use TwoDudes\FXPay\Events\AfterRequestBuildEvent;
use TwoDudes\FXPay\Events\BeforeProcessResponseEvent;
use TwoDudes\FXPay\PaymentRequest\PaymentRequestInterface;
use TwoDudes\FXPay\PaymentResponse;
use TwoDudes\FXPay\RequestBuilder\RequestBuilderInterface;
use TwoDudes\FXPay\ResponseBuilder\ResponseBuilderInterface;
use TwoDudes\FXPay\ResponseValidator\Exception\InvalidDepositException;
use TwoDudes\FXPay\ResponseValidator\Exception\InvalidResponseException;
use TwoDudes\FXPay\ResponseValidator\ResponseValidatorInterface;
/**
* Class G2SHandler
* @package Hiwayfx\Core\DepositBundle\VendorHandler
*/
class FormWithRedirectStrategy extends AbstractStrategy
{
/**
* @param RequestBuilderInterface $requestBuilder
* @param PaymentRequestInterface $deposit
* @return mixed
*/
public function buildRequest(RequestBuilderInterface $requestBuilder, PaymentRequestInterface $deposit)
{
$this->log("Processing %s deposit %s", $this->config->getVendorName(), $deposit->getId());
$params = $requestBuilder->buildRequest($this->config, $deposit);
$event = new AfterRequestBuildEvent();
$event->setParams($params);
$this->dispatch($event);
$params = $event->getParams();
$this->log("Build params: %s", json_encode($params));
$content = $this->generateForm($params);
$this->log("Generated form: %s", $content);
$event = new AfterFormBuildEvent();
$event->setContent($content);
$this->dispatch($event);
$content = $event->getContent();
return $content;
}
/**
* @param ResponseValidatorInterface $responseValidator
* @param ResponseBuilderInterface $responseBuilder
* @param array $responseParams
* @return PaymentResponse|false
*/
public function processResponse(ResponseValidatorInterface $responseValidator, ResponseBuilderInterface $responseBuilder, array $responseParams = [])
{
$this->log('Received %s process request', $this->config->getVendorName());
$this->log('Received params %s', json_encode($responseParams));
$event = new BeforeProcessResponseEvent();
$event->setParams($responseParams);
$this->dispatch($event);
$responseParams = $event->getParams();
try {
$responseValidator->validate($this->config, $responseParams);
} catch (InvalidDepositException $e) {
$this->log("Invalid deposit");
return false;
} catch (InvalidResponseException $e) {
$this->log("Invalid response");
return false;
}
$response = $responseBuilder->buildResponse($responseParams);
$response->setHandler($this->config->getVendorName());
$event = new AfterBuildPaymentResponseEvent();
$event->setPaymentResponse($response);
$this->dispatch($event);
$response = $event->getPaymentResponse();
return $response;
}
/**
* @param $params
* @return string
*/
protected function generateForm($params):string
{
$content = '<form action="' . $this->config->getVendorUrl() . '" method="POST" id="FXPayForm" style="display: none">';
foreach ($params as $name => $value) {
$content .= ' <input type="text" name="' . $name . '" value="' . $value . '"></br>';
}
$content .= '<input type="submit"></form>';
$content .= '<script type="text/javascript">window.onload = function() { document.getElementById("FXPayForm").submit(); }</script>';
return $content;
}
}