|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alcalyn\PayplugBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
6
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
11
|
|
|
use Alcalyn\PayplugBundle\Model\Payment; |
|
12
|
|
|
use Alcalyn\PayplugBundle\Model\IPN; |
|
13
|
|
|
use Alcalyn\PayplugBundle\Event\PayplugIPNEvent; |
|
14
|
|
|
use Alcalyn\PayplugBundle\Event\PayplugMalformedIPNEvent; |
|
15
|
|
|
use Alcalyn\PayplugBundle\Services\PayplugIPNService; |
|
16
|
|
|
|
|
17
|
|
|
class SimulateIPNCommand extends ContainerAwareCommand |
|
18
|
|
|
{ |
|
19
|
|
|
protected function configure() |
|
20
|
|
|
{ |
|
21
|
|
|
$this |
|
22
|
|
|
->setName('payplug:simulate:ipn') |
|
23
|
|
|
->setDescription('Simulate an IPN to test your callback') |
|
24
|
|
|
->addOption('test', 't', InputOption::VALUE_NONE, 'Simulate a sandbox IPN') |
|
25
|
|
|
->addOption('malformed', null, InputOption::VALUE_NONE, 'Simulate a malformed IPN') |
|
26
|
|
|
->addOption('amount', null, InputOption::VALUE_REQUIRED, 'Amount of the payment in cents', 1600) |
|
27
|
|
|
->addOption('order', null, InputOption::VALUE_REQUIRED, 'Order number', 0) |
|
28
|
|
|
->addOption('customer', null, InputOption::VALUE_REQUIRED, 'Customer number', 0) |
|
29
|
|
|
->addOption( |
|
30
|
|
|
'state', |
|
31
|
|
|
null, |
|
32
|
|
|
InputOption::VALUE_REQUIRED, |
|
33
|
|
|
'The new state of the transaction: "'.IPN::PAYMENT_PAID.'" or "'.IPN::PAYMENT_REFUNDED.'"', |
|
34
|
|
|
IPN::PAYMENT_PAID |
|
35
|
|
|
) |
|
36
|
|
|
; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
40
|
|
|
{ |
|
41
|
|
|
$sandboxMode = $input->getOption('test'); |
|
42
|
|
|
$malformed = $input->getOption('malformed'); |
|
43
|
|
|
$ipnTitle = ($malformed ? 'malformed ' : '').'IPN event'.($sandboxMode ? ' (sandbox mode)' : ''); |
|
44
|
|
|
|
|
45
|
|
|
$this->writeTitle($output, 'Dispatch '.$ipnTitle.'...'); |
|
46
|
|
|
|
|
47
|
|
|
if (!$malformed) { |
|
48
|
|
|
$data = $this->getIPNData($input); |
|
49
|
|
|
$ipnRequest = $this->getIPNService()->createIPNFromData($data); |
|
50
|
|
|
$event = new PayplugIPNEvent($ipnRequest); |
|
51
|
|
|
$this->getEventDispatcher()->dispatch(PayplugIPNEvent::PAYPLUG_IPN, $event); |
|
52
|
|
|
} else { |
|
53
|
|
|
$ipnRequest = $this->createMalformedIPNRequest($input); |
|
54
|
|
|
$event = new PayplugMalformedIPNEvent($ipnRequest); |
|
55
|
|
|
$this->getEventDispatcher()->dispatch(PayplugMalformedIPNEvent::PAYPLUG_IPN_MALFORMED, $event); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$output->writeLn(PHP_EOL.'[OK] Done.'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Display text with blank line above and below |
|
63
|
|
|
* |
|
64
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
65
|
|
|
* @param string $title |
|
66
|
|
|
*/ |
|
67
|
|
|
private function writeTitle(OutputInterface $output, $title) |
|
68
|
|
|
{ |
|
69
|
|
|
$output->writeLn(''); |
|
70
|
|
|
$output->writeLn($title); |
|
71
|
|
|
$output->writeLn(''); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return EventDispatcher |
|
76
|
|
|
*/ |
|
77
|
|
|
private function getEventDispatcher() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->getContainer()->get('event_dispatcher'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return PayplugIPNService |
|
84
|
|
|
*/ |
|
85
|
|
|
private function getIPNService() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->getContainer()->get('payplug.ipn'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param InputInterface $input |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
private function getIPNData(InputInterface $input) |
|
96
|
|
|
{ |
|
97
|
|
|
return array( |
|
98
|
|
|
'id_transaction' => '0', |
|
99
|
|
|
'first_name' => 'Tyler', |
|
100
|
|
|
'last_name' => 'Durden', |
|
101
|
|
|
'email' => '[email protected]', |
|
102
|
|
|
'state' => $input->getOption('state'), |
|
103
|
|
|
'amount' => $input->getOption('amount'), |
|
104
|
|
|
'origin' => 'Alcalyn Payplug bundle, IPN simulation (payplug:simulate:ipn)', |
|
105
|
|
|
'customer' => $input->getOption('customer'), |
|
106
|
|
|
'custom_data' => null, |
|
107
|
|
|
'custom_datas' => null, |
|
108
|
|
|
'status' => 0, |
|
109
|
|
|
'order' => $input->getOption('order'), |
|
110
|
|
|
'is_test' => $input->getOption('test'), |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param InputInterface $input |
|
116
|
|
|
* |
|
117
|
|
|
* @return Request |
|
118
|
|
|
*/ |
|
119
|
|
|
private function createMalformedIPNRequest(InputInterface $input) |
|
120
|
|
|
{ |
|
121
|
|
|
$callbackUrl = $this->getContainer()->get('router')->generate('payplug_ipn'); |
|
122
|
|
|
$content = json_encode($this->getIPNData($input)); |
|
123
|
|
|
$payplugSignature = base64_encode('Malformed signature'); |
|
124
|
|
|
|
|
125
|
|
|
$request = Request::create($callbackUrl, 'POST', array(), array(), array(), array(), $content); |
|
126
|
|
|
$request->headers->set('payplug-signature', $payplugSignature); |
|
127
|
|
|
|
|
128
|
|
|
return $request; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|