@@ -97,16 +97,25 @@ public function getLanguages(){
9797
9898 }
9999
100- public function getCurrency (){
101- $ currency = Mage:: getStoreConfig ( ' payment/redsys/currency ' , Mage:: app ( )->getStore () );
100+ public function getCurrency ($ order ){
101+ $ currency = $ order -> getOrderCurrency ( )->getCurrencyCode ( );
102102
103103 switch ($ currency ) {
104- case '3 ' :
105- return '392 ' ;
106- case '2 ' :
107- return '826 ' ;
108- case '1 ' :
104+ case 'AUD ' :
105+ return '036 ' ;
106+ case 'CAD ' :
107+ return '124 ' ;
108+ case 'USD ' :
109109 return '840 ' ;
110+ case 'GBP ' :
111+ return '826 ' ;
112+ case 'CHF ' :
113+ return '756 ' ;
114+ case 'JPY ' :
115+ return '392 ' ;
116+ case 'CNY ' :
117+ return '156 ' ;
118+ case 'EUR ' :
110119 default :
111120 return '978 ' ;
112121 }
@@ -118,127 +127,127 @@ public function stateInTpv($_order){
118127 $ state = 'new ' ;
119128 $ comment = $ this ->__ ('enters TPV ' );
120129
121- $ isCustomerNotified = false ;
122- $ isVisibleOnFront = false ;
123-
124- if (Mage::getStoreConfig ('payment/redsys/notify_clients_states ' , Mage::app ()->getStore ()) && Mage::getStoreConfig ('payment/redsys/notify_by_email ' , Mage::app ()->getStore ())){
125- $ isCustomerNotified = true ;
126- }
127-
128- if (Mage::getStoreConfig ('payment/redsys/notify_clients_states ' , Mage::app ()->getStore ()) && Mage::getStoreConfig ('payment/redsys/notify_by_frontend ' , Mage::app ()->getStore ())){
129- $ isVisibleOnFront = true ;
130- }
131-
132- $ this ->setCustomState ($ _order ,$ state , $ status , $ comment , $ isCustomerNotified ,$ isVisibleOnFront );
130+ $ this ->setCustomState ($ _order ,$ state , $ status , $ comment , false , false );
133131 $ _order ->save ();
134132
135- if ($ isCustomerNotified ){
136- $ _order ->sendOrderUpdateEmail ($ isCustomerNotified , $ comment );
137- }
138133 }
139134
140135 public function stateConfirmTpv ($ _order ,$ comment ){
141136 $ this ->fixCreditCustomer ();
142137 $ status = Mage::getStoreConfig ('payment/redsys/confirm_status ' , Mage::app ()->getStore ());
143138 $ state = 'processing ' ;
144139
145- $ isCustomerNotified = false ;
146- $ isVisibleOnFront = false ;
140+ // Notificacion privada
141+ $ this -> setCustomState ( $ _order , $ state , $ status , $ comment , false , false ) ;
147142
148- if (Mage::getStoreConfig ('payment/redsys/notify_clients_states ' , Mage::app ()->getStore ()) && Mage::getStoreConfig ('payment/redsys/notify_by_email ' , Mage::app ()->getStore ())){
149- $ isCustomerNotified = true ;
150- }
143+ $ _order ->save ();
151144
152- if (Mage::getStoreConfig ('payment/redsys/notify_clients_states ' , Mage::app ()->getStore ()) && Mage::getStoreConfig ('payment/redsys/notify_by_frontend ' , Mage::app ()->getStore ())){
153- $ isVisibleOnFront = true ;
154- }
145+ $ allowStatuses = explode (', ' , Mage::getStoreConfig ('payment/redsys/notify_order_status ' ,Mage::app ()->getStore ()));
155146
156- $ this ->setCustomState ($ _order ,$ state , $ status , $ comment , $ isCustomerNotified ,$ isVisibleOnFront );
157- $ _order ->save ();
147+ if (in_array ($ state , $ allowStatuses ) && Mage::getStoreConfig ('payment/redsys/notify_clients_states ' , Mage::app ()->getStore ())){
148+
149+ $ comment = $ this ->__ ('TPV payment accepted. ' );
150+
151+ $ isCustomerNotified = false ;
152+ $ isVisibleOnFront = false ;
153+
154+ if (Mage::getStoreConfig ('payment/redsys/notify_by_email ' , Mage::app ()->getStore ())){
155+ $ isCustomerNotified = true ;
156+ }
157+
158+ if (Mage::getStoreConfig ('payment/redsys/notify_by_frontend ' , Mage::app ()->getStore ())){
159+ $ isVisibleOnFront = true ;
160+ }
161+
162+ $ this ->fixCreditCustomer ();
163+
164+ // Notificación privada de cancelación
165+ $ this ->setCustomState ($ _order ,$ state , $ status , $ comment , $ isCustomerNotified , $ isVisibleOnFront );
166+
167+ $ _order ->save ();
158168
159- if ($ isCustomerNotified ){
160169 $ _order ->sendOrderUpdateEmail ($ isCustomerNotified , $ comment );
170+
161171 }
172+
162173 }
163174
164- public function stateErrorTpv ($ _order ,$ errorMessage= null ){
175+ public function stateErrorTpv ($ _order ,$ errorMessage = null ){
165176 $ this ->fixCreditCustomer ();
166- $ status = Mage:: getStoreConfig ( ' payment/redsys/error_status ' , Mage:: app ()-> getStore ()) ;
167- $ state = 'canceled ' ;
177+ $ state = ' canceled ' ;
178+ $ status = 'canceled ' ;
168179 $ comment = $ this ->__ ('Error in TPV order canceled. ' );
169180
170- $ isCustomerNotified = false ;
171- $ isVisibleOnFront = false ;
172-
173- if (Mage::getStoreConfig ('payment/redsys/notify_clients_states ' , Mage::app ()->getStore ()) && Mage::getStoreConfig ('payment/redsys/notify_by_email ' , Mage::app ()->getStore ())){
174- $ isCustomerNotified = true ;
175- }
176-
177- if (Mage::getStoreConfig ('payment/redsys/notify_clients_states ' , Mage::app ()->getStore ()) && Mage::getStoreConfig ('payment/redsys/notify_by_frontend ' , Mage::app ()->getStore ())){
178- $ isVisibleOnFront = true ;
179- }
180-
181181 if ($ errorMessage ){
182182 $ comment = $ this ->__ ('Failed: %s ' ,$ errorMessage );
183183 }
184184
185- $ this -> setCustomState ( $ _order , $ state , $ status , $ comment , $ isCustomerNotified , $ isVisibleOnFront );
186- $ _order -> registerCancellation ( "" )-> save ( );
185+ // Notificacion privada
186+ $ this -> setCustomState ( $ _order , null , $ status , $ comment , false , false );
187187 $ _order ->save ();
188188
189- if ($ isCustomerNotified ){
190- $ _order ->sendOrderUpdateEmail ($ isCustomerNotified , $ comment );
189+ $ allowStatuses = explode (', ' , Mage::getStoreConfig ('payment/redsys/notify_order_status ' ,Mage::app ()->getStore ()));
190+
191+ if (in_array ($ state , $ allowStatuses )
192+ && Mage::getStoreConfig ('payment/redsys/notify_clients_states ' , Mage::app ()->getStore ())){
193+
194+ $ isCustomerNotified = false ;
195+ $ isVisibleOnFront = false ;
196+
197+ if ( Mage::getStoreConfig ('payment/redsys/notify_by_email ' , Mage::app ()->getStore ())){
198+ $ isCustomerNotified = true ;
199+ }
200+
201+ if (Mage::getStoreConfig ('payment/redsys/notify_by_frontend ' , Mage::app ()->getStore ())){
202+ $ isVisibleOnFront = true ;
203+ }
204+
205+ $ this ->fixCreditCustomer ();
206+
207+ $ comment = $ this ->__ ('Cancelled payment from the payment gateway TPV. ' );
208+
209+ // Notificación privada de cancelación
210+ //$this->setCustomState($_order,$state, $status, $comment, $isCustomerNotified , $isVisibleOnFront);
211+ $ history = $ _order ->addStatusHistoryComment ($ comment , false );
212+ $ history ->setIsCustomerNotified ($ isCustomerNotified );
213+ $ history ->setIsVisibleOnFront ($ isVisibleOnFront );
214+
215+ $ _order ->save ();
216+
217+ $ _order ->sendOrderUpdateEmail ();
218+
191219 }
192220 }
193-
194- public function recoveryCart (){
195- $ recoveryCart = Mage::getStoreConfig ('payment/redsys/recover_cart ' , Mage::app ()->getStore ());
196221
222+ public function recoveryCart ($ _order ){
223+
224+
225+ $ recoveryCart = Mage::getStoreConfig ('payment/redsys/recover_cart ' , Mage::app ()->getStore ());
197226 if ($ recoveryCart ){
198- $ _order = new Mage_Sales_Model_Order ();
199- $ orderId = Mage::getSingleton ('checkout/session ' )->getLastRealOrderId ();
200- $ _order ->loadByIncrementId ($ orderId );
201-
202- $ items = $ _order ->getAllVisibleItems ();
203- $ cart = Mage::getSingleton ('checkout/cart ' );
204- foreach ($ items as $ itemId => $ item ){
205- $ cart ->addOrderItem ($ item );
206- }
207- $ cart ->save ();
227+ try {
228+ $ quote = Mage::getModel ('sales/quote ' )->load ($ _order ->getQuoteId ());
208229
209- //@todo Volver a poner la direccion y reenviar al checkout
210- }
230+ $ quote -> getBillingAddress ()-> setSaveInAddressBook ( 0 );
231+ $ quote -> getShippingAddress ()-> setSaveInAddressBook ( 0 );
211232
233+ if ($ quote ->getId ()) {
212234
213- }
235+ $ quote ->setIsActive (1 )
236+ ->setReservedOrderId (null )
237+ ->save ();
214238
215- public function restoreStock ($ order ){
216- $ items = $ order ->getAllItems ();
217- if ($ items )
218- {
219- foreach ($ items as $ item )
220- {
221- $ quantity = $ item ->getQtyOrdered ();
222- $ product_id = $ item ->getProductId ();
223- $ stock = Mage::getModel ('cataloginventory/stock_item ' )->loadByProduct ($ product_id );
224- if ($ stock ->getManageStock ()){
225- $ stockQty = $ stock ->getQty ();
226- $ stock ->setQty ($ stockQty + $ quantity );
227-
228- if ($ stockQty + $ quantity <= 0 && $ stock ->getBackorders ()==0 )
229- {
230- $ stock ->setIsInStock (false );
231- }elseif ($ stockQty + $ quantity > 0 ){
232- $ stock ->setIsInStock (true );
233- }
234-
235- $ stock ->save ();
236- }
239+ Mage::getSingleton ('checkout/session ' )
240+ ->replaceQuote ($ quote )
241+ ->unsLastRealOrderId ();
237242
243+ }
244+ }catch (exception $ e ){
245+ Mage::log ($ e ->getMessage (),null ,"redsys.log " );
238246 }
239247 }
240248 }
241249
250+
242251 public function createInvoice ($ order ){
243252 $ autoinvoice = Mage::getStoreConfig ('payment/redsys/autoinvoice ' , Mage::app ()->getStore ());
244253 if ($ autoinvoice ){
@@ -430,6 +439,13 @@ public function isCurrentVersionLatest(){
430439 }
431440
432441 public function setCustomState ($ order ,$ state , $ status = false , $ comment = '' , $ isCustomerNotified = null , $ isVisibleOnFront =false ){
442+ if ($ state =="canceled " ){
443+ $ order ->getPayment ()->cancel ();
444+ $ order ->registerCancellation ($ comment );
445+ Mage::dispatchEvent ('order_cancel_after ' , array ('order ' => $ order ));
446+ return $ this ;
447+ }
448+
433449 $ order ->setData ('state ' , $ state );
434450
435451 // add status history
@@ -445,46 +461,4 @@ public function setCustomState($order ,$state, $status = false, $comment = '', $
445461
446462 return $ this ;
447463 }
448-
449- public function notifyEmailCustomer ($ message , $ email ){
450-
451- $ data = array ('message ' => $ message );
452- $ emailTemplate = Mage::getModel ('core/email_template ' )->loadDefault ('redsys_notify_customer ' );
453- $ senderName = Mage::getStoreConfig ('trans_email/ident_general/name ' );
454- $ senderEmail = Mage::getStoreConfig ('trans_email/ident_general/email ' );
455- $ processedTemplate = $ emailTemplate ->getProcessedTemplate ($ data );
456-
457- $ mail = Mage::getModel ('core/email ' )
458- ->setToName ($ senderName )
459- ->setReplyTo ($ senderEmail )
460- ->setToEmail ($ email )
461- ->setBody ($ processedTemplate )
462- ->setSubject ($ this ->__ ('Order canceled from TPV ' ))
463- ->setFromEmail ($ senderEmail )
464- ->setFromName ($ senderName )
465- ->setType ('html ' );
466-
467- $ mail ->send ();
468- }
469-
470- public function notifyEmailAdmin ($ message , $ email ){
471-
472- $ data = array ('message ' => $ message , 'email ' => $ email );
473- $ emailTemplate = Mage::getModel ('core/email_template ' )->loadDefault ('redsys_notify_admin ' );
474- $ senderName = Mage::getStoreConfig ('trans_email/ident_general/name ' );
475- $ senderEmail = Mage::getStoreConfig ('trans_email/ident_general/email ' );
476- $ processedTemplate = $ emailTemplate ->getProcessedTemplate ($ data );
477-
478- $ mail = Mage::getModel ('core/email ' )
479- ->setToName ($ senderName )
480- ->setReplyTo ($ email )
481- ->setToEmail ($ senderEmail )
482- ->setBody ($ processedTemplate )
483- ->setSubject ($ this ->__ ('Order canceled from TPV ' ))
484- ->setFromEmail ($ senderEmail )
485- ->setFromName ($ senderName )
486- ->setType ('html ' );
487-
488- $ mail ->send ();
489- }
490464}
0 commit comments