@@ -48,156 +48,77 @@ public function payment($payment_form_data, $order_data, $auto_submit = false)
4848 protected function callbackInit ($ request )
4949 {
5050 $ params = $ request ['params ' ];
51- list ($ this ->order_id , $ this ->merchant_id ) = explode ( unitpayPayment::DELIMITER , $ params ['account ' ],2 );
51+ list ($ this ->order_id , $ this ->merchant_id ) = explode (unitpayPayment::DELIMITER , $ params ['account ' ], 2 );
5252 return parent ::callbackInit ($ request );
5353 }
5454
5555 protected function callbackHandler ($ data )
5656 {
57- $ method = '' ;
58- $ params = [];
59-
60- if ((isset ($ data ['params ' ])) && (isset ($ data ['method ' ])) && (isset ($ data ['params ' ]['signature ' ]))) {
61- $ params = $ data ['params ' ];
62- $ method = $ data ['method ' ];
63- $ signature = $ params ['signature ' ];
64-
65- if (empty ($ signature )) {
66- $ status_sign = false ;
67- } else {
68- $ status_sign = $ this ->verifySignature ($ params , $ method );
69- }
70-
71- } else {
72- $ status_sign = false ;
57+ if (!isset ($ data ['params ' ]) || !isset ($ data ['method ' ]) || !isset ($ data ['params ' ]['signature ' ])) {
58+ $ result = array ('error ' => array ('message ' => 'Не переданы обязательные параметры запроса ' ));
59+ return $ this ->returnJson ($ result );
7360 }
7461
75- if ($ status_sign ) {
76- switch ($ method ) {
77- case 'check ' :
78- $ result = $ this ->check ($ params );
79- break ;
80- case 'pay ' :
81- $ result = $ this ->pay ($ params );
82- break ;
83- case 'error ' :
84- $ result = $ this ->error ($ params );
85- break ;
86- default :
87- $ result = array ('error ' =>
88- array ('message ' => 'неверный метод ' )
89- );
90- break ;
91- }
92- } else {
93- $ result = array ('error ' =>
94- array ('message ' => 'неверная сигнатура ' )
95- );
96- }
62+ $ params = $ data ['params ' ];
63+ $ method = $ data ['method ' ];
9764
98- $ this ->hardReturnJson ($ result );
99- }
65+ if (!$ this ->verifySignature ($ params , $ method )) {
66+ $ result = array ('error ' => array ('message ' => 'Неверная сигнатура ' ));
67+ return $ this ->returnJson ($ result );
68+ }
10069
101- public function check ($ params )
102- {
103- $ order_model = new shopOrderModel ();
104- $ order_id = $ this ->order_id ;
105- $ order = $ order_model ->getById ($ order_id );
106-
107- if (is_null ($ order_id )) {
108- $ result = array ('error ' =>
109- array ('message ' => 'заказа не существует ' )
110- );
111- } elseif ((float )$ order ['total ' ] != (float )$ params ['orderSum ' ]) {
112- $ result = array ('error ' =>
113- array ('message ' => 'не совпадает сумма заказа ' )
114- );
115- } elseif ($ order ['currency ' ] != $ params ['orderCurrency ' ]) {
116- $ result = array ('error ' =>
117- array ('message ' => 'не совпадает валюта заказа ' )
118- );
119- } else {
120- $ result = array ('result ' =>
121- array ('message ' => 'Запрос успешно обработан ' )
122- );
70+ switch ($ method ) {
71+ case 'check ' :
72+ $ result = array ('result ' => array ('message ' => 'Запрос успешно обработан ' ));
73+ break ;
74+ case 'pay ' :
75+ $ result = $ this ->pay ($ params );
76+ break ;
77+ case 'error ' :
78+ $ result = array ('result ' => array ('message ' => 'Произошла ошибка при обработке платежа ' ));
79+ break ;
80+ default :
81+ $ result = array ('error ' => array ('message ' => 'Неверный метод ' ));
82+ break ;
12383 }
12484
125- return $ result ;
85+ return $ this -> returnJson ( $ result) ;
12686 }
12787
12888 public function pay ($ params )
12989 {
130- $ order_model = new shopOrderModel ();
131- $ order_id = $ this ->order_id ;
132- $ order = $ order_model ->getById ($ order_id );
133-
134- if (is_null ($ order_id )) {
135- $ result = array ('error ' =>
136- array ('message ' => 'заказа не существует ' )
137- );
138- } elseif ((float )$ order ['total ' ] != (float )$ params ['orderSum ' ]) {
139- $ result = array ('error ' =>
140- array ('message ' => 'не совпадает сумма заказа ' )
141- );
142- } elseif ($ order ['currency ' ] != $ params ['orderCurrency ' ]) {
143- $ result = array ('error ' =>
144- array ('message ' => 'не совпадает валюта заказа ' )
145- );
146- } else {
147- $ update_order = [];
148- $ update_order ['state_id ' ] = 'paid ' ;
149- $ update_order = array_merge ($ update_order , [
150- 'paid_date ' => date ('Y-m-d ' ),
151- 'paid_year ' => date ('Y ' ),
152- 'paid_quarter ' => floor ((date ('m ' ) - 1 ) / 3 ) + 1 ,
153- 'paid_month ' => (int )date ('m ' ),
154- ]);
155-
156- $ order_model ->updateById ($ order_id , $ update_order );
157-
158- $ logs [] = array (
159- 'order_id ' => $ order_id ,
160- 'action_id ' => 'pay ' ,
161- 'before_state_id ' => $ order ['state_id ' ],
162- 'after_state_id ' => $ update_order ['state_id ' ],
163- 'text ' => '' ,
164- );
165-
166- #add log records
167- $ log_model = new shopOrderLogModel ();
168- foreach ($ logs as $ log ) {
169- $ log_model ->add ($ log );
170- }
171-
172- $ result = array ('result ' =>
173- array ('message ' => 'Запрос успешно обработан ' )
174- );
90+ $ transaction_data = $ this ->formalizeData ($ params );
91+
92+ $ transaction_data = $ this ->saveTransaction ($ transaction_data , $ params );
93+
94+ $ result = $ this ->execAppCallback (self ::CALLBACK_PAYMENT , $ transaction_data );
95+
96+ if (empty ($ result ['result ' ])) {
97+ $ message = !empty ($ result ['error ' ]) ? $ result ['error ' ] : 'wa transaction error ' ;
98+ return array ('error ' => array ('message ' => $ message ));
17599 }
176100
177- return $ result ;
101+ return array ( ' result ' => array ( ' message ' => ' Запрос успешно обработан ' )) ;
178102 }
179103
180-
181- public function error ($ params )
104+ public function formalizeData ($ params )
182105 {
183- $ order_model = new shopOrderModel ();
184- $ order_id = $ this ->order_id ;
185- $ order = $ order_model ->getById ($ order_id );
186-
187- if (is_null ($ order ['id ' ])) {
188- $ result = array ('error ' =>
189- array ('message ' => 'заказа не существует ' )
190- );
191- } else {
192- $ result = array ('result ' =>
193- array ('message ' => 'Запрос успешно обработан ' )
194- );
106+ $ transaction_data = parent ::formalizeData ($ params );
107+ $ transaction_data ['order_id ' ] = $ this ->order_id ;
108+ $ transaction_data ['amount ' ] = $ params ['sum ' ];
109+ $ transaction_data ['native_id ' ] = $ params ['unitpayId ' ];
110+ $ transaction_data ['type ' ] = self ::OPERATION_CAPTURE ;
111+ $ transaction_data ['state ' ] = self ::STATE_CAPTURED ;
112+ $ transaction_data ['currency_id ' ] = $ params ['orderCurrency ' ];
113+
114+ $ isTest = ifempty ($ params ['test ' ]);
115+ if ($ isTest ) {
116+ $ transaction_data ['view_data ' ] = 'Тестовый режим ' ;
195117 }
196118
197- return $ result ;
119+ return $ transaction_data ;
198120 }
199121
200-
201122 public function verifySignature ($ params , $ method )
202123 {
203124 return $ params ['signature ' ] == $ this ->getSignature ($ this ->unit_secret_key , $ params , $ method );
@@ -214,10 +135,13 @@ public function getSignature($secretKey, array $params, $method)
214135 return hash ('sha256 ' , join ('{up} ' , $ params ));
215136 }
216137
217- protected function hardReturnJson ( $ arr )
138+ protected function returnJson ( $ message )
218139 {
219- header ('Content-Type: application/json ' );
220- $ result = json_encode ($ arr );
221- die ($ result );
140+ return array (
141+ 'header ' => array (
142+ 'Content-Type ' => 'application/json '
143+ ),
144+ 'message ' => json_encode ($ message )
145+ );
222146 }
223147}
0 commit comments