1+ <?php
2+ class unitpayPayment extends waPayment implements waIPayment
3+ {
4+ public function allowedCurrency ()
5+ {
6+ $ default = array (
7+ 'RUB ' ,
8+ 'USD ' ,
9+ 'EUR ' ,
10+ );
11+ return $ default ;
12+ }
13+
14+ public function payment ($ payment_form_data , $ order_data , $ auto_submit = false )
15+ {
16+ $ result = null ;
17+ $ order = waOrder::factory ($ order_data );
18+
19+ $ public_key = $ this ->unit_public_key ;
20+ $ sum = $ order ->total ;
21+ $ account = $ order ->id ;
22+ $ desc = $ order ->description ;
23+
24+ $ view = wa ()->getView ();
25+ $ view ->assign ('public_key ' , $ public_key );
26+ $ view ->assign ('sum ' , $ sum );
27+ $ view ->assign ('account ' , $ account );
28+ $ view ->assign ('desc ' , $ desc );
29+
30+ return $ view ->fetch ($ this ->path .'/templates/payment.html ' );
31+ }
32+
33+ protected function callbackInit ($ request )
34+ {
35+
36+ $ params = $ request ['params ' ];
37+ $ this ->order_id = $ params ['account ' ];
38+ return parent ::callbackInit ($ request );
39+ }
40+
41+ protected function callbackHandler ($ data )
42+ {
43+
44+
45+ $ method = '' ;
46+ $ params = [];
47+
48+ if ((isset ($ data ['params ' ])) && (isset ($ data ['method ' ])) && (isset ($ data ['params ' ]['signature ' ]))){
49+ $ params = $ data ['params ' ];
50+ $ method = $ data ['method ' ];
51+ $ signature = $ params ['signature ' ];
52+
53+ if (empty ($ signature )){
54+ $ status_sign = false ;
55+ }else {
56+ $ status_sign = $ this ->verifySignature ($ params , $ method );
57+ }
58+
59+ }else {
60+ $ status_sign = false ;
61+ }
62+
63+ if ($ status_sign ){
64+ switch ($ method ) {
65+ case 'check ' :
66+ $ result = $ this ->check ( $ params );
67+ break ;
68+ case 'pay ' :
69+ $ result = $ this ->pay ( $ params );
70+ break ;
71+ case 'error ' :
72+ $ result = $ this ->error ( $ params );
73+
74+ break ;
75+ default :
76+ $ result = array ('error ' =>
77+ array ('message ' => 'неверный метод ' )
78+ );
79+ break ;
80+ }
81+ }else {
82+ $ result = array ('error ' =>
83+ array ('message ' => 'неверная сигнатура ' )
84+ );
85+ }
86+
87+ $ this ->hardReturnJson ($ result );
88+
89+ }
90+
91+ function check ( $ params )
92+ {
93+ require_once __DIR__ .'/../../../../wa-apps/shop/lib/model/shopOrder.model.php ' ;
94+ $ order_model = new shopOrderModel ();
95+ $ order_id = $ this ->order_id ;
96+ $ order = $ order_model ->getById ( $ order_id );
97+
98+ if (is_null ($ order_id )){
99+ $ result = array ('error ' =>
100+ array ('message ' => '1заказа не существует ' )
101+ );
102+ }elseif ((float )$ order ['total ' ] != (float )$ params ['orderSum ' ]) {
103+ $ result = array ('error ' =>
104+ array ('message ' => 'не совпадает сумма заказа ' )
105+ );
106+ }elseif ($ order ['currency ' ] != $ params ['orderCurrency ' ]) {
107+ $ result = array ('error ' =>
108+ array ('message ' => 'не совпадает валюта заказа ' )
109+ );
110+ }
111+ else {
112+ $ result = array ('result ' =>
113+ array ('message ' => 'Запрос успешно обработан ' )
114+ );
115+ }
116+
117+ return $ result ;
118+
119+ }
120+
121+ function pay ( $ params )
122+ {
123+
124+ require_once __DIR__ .'/../../../../wa-apps/shop/lib/model/shopOrder.model.php ' ;
125+ $ order_model = new shopOrderModel ();
126+ $ order_id = $ this ->order_id ;
127+ $ order = $ order_model ->getById ( $ order_id );
128+
129+ if (is_null ($ order_id )){
130+ $ result = array ('error ' =>
131+ array ('message ' => 'заказа не существует ' )
132+ );
133+ }elseif ((float )$ order ['total ' ] != (float )$ params ['orderSum ' ]) {
134+ $ result = array ('error ' =>
135+ array ('message ' => 'не совпадает сумма заказа ' )
136+ );
137+ }elseif ($ order ['currency ' ] != $ params ['orderCurrency ' ]) {
138+ $ result = array ('error ' =>
139+ array ('message ' => 'не совпадает валюта заказа ' )
140+ );
141+ }
142+ else {
143+
144+ /*$transaction_data = $this->formalizeData($params);
145+ $transaction_data['order_id'] = $order_id;
146+ $transaction_data['amount'] = $order['total'];
147+ $transaction_data['currency_id'] = $order['currency'];
148+ $transaction_data['plugin'] = 'unitpay';
149+
150+ $this->execAppCallback(waPayment::CALLBACK_PAYMENT, $transaction_data);*/
151+
152+ $ update_order = [];
153+ $ update_order ['state_id ' ] = 'paid ' ;
154+ $ update_order = array_merge ($ update_order , [
155+ 'paid_date ' => date ('Y-m-d ' ),
156+ 'paid_year ' => date ('Y ' ),
157+ 'paid_quarter ' => floor ((date ('m ' ) - 1 ) / 3 ) + 1 ,
158+ 'paid_month ' => (int )date ('m ' ),
159+ ]);
160+
161+ $ order_model ->updateById ($ order_id , $ update_order );
162+
163+ $ logs [] = array (
164+ 'order_id ' => $ order_id ,
165+ 'action_id ' => 'pay ' ,
166+ 'before_state_id ' => $ order ['state_id ' ],
167+ 'after_state_id ' => $ update_order ['state_id ' ],
168+ 'text ' => '' ,
169+ // 'params' => array('merged_order_id' => $master_id),
170+ );
171+
172+ #add log records
173+ require_once __DIR__ .'/../../../../wa-apps/shop/lib/model/shopOrderLog.model.php ' ;
174+ $ log_model = new shopOrderLogModel ();
175+ foreach ($ logs as $ log ) {
176+ $ log_model ->add ($ log );
177+ }
178+
179+ $ result = array ('result ' =>
180+ array ('message ' => 'Запрос успешно обработан ' )
181+ );
182+
183+ }
184+
185+ return $ result ;
186+
187+ }
188+
189+
190+ function error ( $ params )
191+ {
192+ require_once __DIR__ .'/../../../../wa-apps/shop/lib/model/shopOrder.model.php ' ;
193+ $ order_model = new shopOrderModel ();
194+ $ order_id = $ this ->order_id ;
195+ $ order = $ order_model ->getById ( $ order_id );
196+
197+ if (is_null ($ order ['id ' ])){
198+ $ result = array ('error ' =>
199+ array ('message ' => 'заказа не существует ' )
200+ );
201+ }
202+ else {
203+
204+ /*$transaction_data = $this->formalizeData($params);
205+ $transaction_data['order_id'] = $order_id;
206+ $transaction_data['amount'] = $order['total'];
207+ $transaction_data['currency_id'] = $order['currency'];
208+ $transaction_data['plugin'] = 'unitpay';
209+
210+ $this->execAppCallback(waPayment::CALLBACK_DECLINE, $transaction_data);*/
211+
212+ $ result = array ('result ' =>
213+ array ('message ' => 'Запрос успешно обработан ' )
214+ );
215+
216+ }
217+
218+ return $ result ;
219+ }
220+
221+
222+ function verifySignature ($ params , $ method )
223+ {
224+
225+ require_once __DIR__ .'/../../../../wa-apps/shop/lib/model/shopPluginSettings.model.php ' ;
226+ require_once __DIR__ .'/../../../../wa-apps/shop/lib/model/shopSortable.model.php ' ;
227+ require_once __DIR__ .'/../../../../wa-apps/shop/lib/model/shopPlugin.model.php ' ;
228+ $ settings_model = new shopPluginSettingsModel ();
229+ $ plugin_model = new shopPluginModel ();
230+ $ plugin = $ plugin_model ->getByField ('plugin ' , $ this ->id );
231+
232+ $ secret = $ settings_model ->get ($ plugin ['id ' ], 'unit_secret_key ' );
233+
234+ return $ params ['signature ' ] == $ this ->getSignature ($ method , $ params , $ secret );
235+ }
236+
237+ function getSignature ($ method , array $ params , $ secretKey )
238+ {
239+ ksort ($ params );
240+ unset($ params ['sign ' ]);
241+ unset($ params ['signature ' ]);
242+ array_push ($ params , $ secretKey );
243+ array_unshift ($ params , $ method );
244+
245+ return hash ('sha256 ' , join ('{up} ' , $ params ));
246+ }
247+
248+ protected function hardReturnJson ( $ arr )
249+ {
250+ header ('Content-Type: application/json ' );
251+ $ result = json_encode ($ arr );
252+ die ($ result );
253+ }
254+ }
0 commit comments