Provider31::response_Confirm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 *      Main class for EasyPay-Provider 3.1
5
 *
6
 *      @package php_EasyPay
7
 *      @version 1.1
8
 *      @author Dmitry Shovchko <[email protected]>
9
 *
10
 */
11
12
namespace EasyPay;
13
14
class Provider31
15
{
16
    /**
17
     *      @var array
18
     */
19
    protected static $options = array(
20
        'ServiceId' => 0,
21
        'UseSign' => false,
22
        'EasySoftPKey' => '',
23
        'ProviderPKey' => '',
24
    );
25
26
    /**
27
     *      @var \EasyPay\Callback
28
     */
29
    protected static $cb;
30
31
    /**
32
     *      @var mixed
33
     */
34
    protected $request;
35
36
    /**
37
     *      @var Provider31\Response
38
     */
39
    protected $response;
40
41
    /**
42
     *      Provider31 constructor
43
     *
44
     *      @param array $options
45
     *      @param Callback $cb
46
     *      @param \Debulog\LoggerInterface $log
47
     */
48
    public function __construct(array $options, \EasyPay\Callback $cb, \Debulog\LoggerInterface $log)
49
    {
50
        self::$options = array_merge(self::$options, $options);
51
        self::$cb = $cb;
52
53
        Log::set($log);
54
    }
55
56
    /**
57
     *      Get and process request, echo response
58
     *
59
     */
60
    public function process()
61
    {
62
        try
63
        {
64
            //      get request
65
            $this->get_request();
66
67
            //      get response
68
            $this->response = $this->get_response();
69
70
            Log::instance()->add('the request was processed successfully');
71
        }
72
        catch (\Exception $e)
73
        {
74
            $this->response = $this->get_error_response($e);
75
76
            Log::instance()->add('the request was processed with an error');
77
        }
78
79
        //      output response
80
        $this->response->sign_and_out(self::$options);
81
    }
82
83
    /**
84
     *      method to create a specific class of request
85
     *
86
     */
87
    protected function get_request()
88
    {
89
        $raw = new Provider31\Request\RAW();
90
91
        $r = new Provider31\Request\General($raw);
92
        $c = '\\EasyPay\\Provider31\\Request\\'.$r->Operation();
93
94
        $this->request = new $c($raw);
95
96
        //      validate request
97
        $this->request->validate_request(self::$options);
98
        Log::instance()->debug('request is valid');
99
100
        //      verify sign
101
        $this->request->verify_sign(self::$options);
102
        Log::instance()->debug('signature of request is correct');
103
    }
104
105
    /**
106
     *      generate response
107
     *
108
     *      @return mixed
109
     */
110
    protected function get_response()
111
    {
112
        $m = 'response_'.$this->request->Operation();
113
        return $this->$m();
114
    }
115
116
    /**
117
     *      run check callback and generate a response
118
     *
119
     *      @return Provider31\Response\Check
120
     */
121
    private function response_Check()
122
    {
123
        Log::instance()->add(sprintf('Check("%s")', $this->request->Account()));
124
125
        $accountinfo = self::$cb->check(
126
            $this->request->Account()
127
        );
128
129
        // Sending a response
130
        return new Provider31\Response\Check($accountinfo);
131
    }
132
133
    /**
134
     *      run payment callback and generate a response
135
     *
136
     *      @return Provider31\Response\Payment
137
     */
138
    private function response_Payment()
139
    {
140
        Log::instance()->add(sprintf('Payment("%s", "%s", "%s")', $this->request->Account(), $this->request->OrderId(), $this->request->Amount()));
141
142
        $paymentid = self::$cb->payment(
143
            $this->request->Account(),
144
            $this->request->OrderId(),
145
            $this->request->Amount()
146
        );
147
148
        // Sending a response
149
        return new Provider31\Response\Payment($paymentid);
150
    }
151
152
    /**
153
     *      run confirm callback and generate a response
154
     *
155
     *      @return Provider31\Response\Confirm
156
     */
157
    private function response_Confirm()
158
    {
159
        Log::instance()->add(sprintf('Confirm("%s")', $this->request->PaymentId()));
160
161
        $orderdate = self::$cb->confirm(
162
            $this->request->PaymentId()
163
        );
164
165
        // Sending a response
166
        return new Provider31\Response\Confirm($orderdate);
167
    }
168
169
    /**
170
     *      run cancel callback and generate a response
171
     *
172
     *      @return Provider31\Response\Cancel
173
     */
174
    private function response_Cancel()
175
    {
176
        Log::instance()->add(sprintf('Cancel("%s")', $this->request->PaymentId()));
177
178
        $canceldate = self::$cb->cancel(
179
            $this->request->PaymentId()
180
        );
181
182
        // Sending a response
183
        return new Provider31\Response\Cancel($canceldate);
184
    }
185
186
    /**
187
     *      Generates an xml with an error message
188
     *
189
     *      @param mixed $e
190
     *
191
     *      @return Provider31\Response\ErrorInfo
192
     */
193
    private function get_error_response($e)
194
    {
195
        $message = $e->getMessage();
196
197
        if ($e instanceof Exception\Structure)
198
        {
199
            $message = 'Error in request';
200
        }
201
        elseif ($e instanceof Exception\Sign)
202
        {
203
            $message = 'Signature error!';
204
        }
205
        elseif ($e instanceof Exception\Runtime)
206
        {
207
            $message = 'Error while processing request';
208
        }
209
210
        // Sending a response
211
        return new Provider31\Response\ErrorInfo($e->getCode(), $message);
212
    }
213
}
214