Handle canceled payment transactions.
[platal.git] / modules / payment.php
index 1703917..4fa0641 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2013 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -127,7 +127,7 @@ class PaymentModule extends PLModule
             'admin/reconcile'              => $this->make_hook('adm_reconcile',    AUTH_PASSWD, 'admin'),
             'admin/reconcile/importlogs'   => $this->make_hook('adm_importlogs',   AUTH_PASSWD, 'admin'),
             'admin/reconcile/transfers'    => $this->make_hook('adm_transfers',    AUTH_PASSWD, 'admin'),
-            'admin/payments/bankaccounts' => $this->make_hook('adm_bankaccounts', AUTH_PASSWD, 'admin'),
+            'admin/payments/bankaccounts'  => $this->make_hook('adm_bankaccounts', AUTH_PASSWD, 'admin'),
         );
     }
 
@@ -251,7 +251,7 @@ class PaymentModule extends PLModule
         }
 
         /* on extrait la reference de la commande */
-        if (!ereg('-([0-9]+)$', Env::v('vads_order_id'), $matches)) {
+        if (!preg_match('/-([0-9]+)$/', Env::v('vads_order_id'), $matches)) {
             cb_erreur("référence de commande invalide");
         }
 
@@ -338,7 +338,7 @@ class PaymentModule extends PLModule
         /* raison */
         $reason = ($status == 'Pending') ? Env::s('pending_reason') : Env::s('reason_code');
         /* reference complete de la commande */
-        $fullref = Env::s('cm');
+        $fullref = str_replace('%2d','-',Env::s('cm'));
         /* montant de la transaction */
         $montant = Env::s('amt');
         /* devise */
@@ -362,7 +362,7 @@ class PaymentModule extends PLModule
         }
 
         /* on extrait la reference de la commande */
-        if (!ereg('-xorg-([0-9]+)$', $fullref, $matches)) {
+        if (!preg_match('/-xorg-([0-9]+)$/', $fullref, $matches)) {
             paypal_erreur("référence de commande invalide");
         }
 
@@ -632,14 +632,15 @@ class PaymentModule extends PLModule
         $table_editor->describe('asso_id', 'ID du groupe', false, true);
         $table_editor->describe('owner', 'titulaire', true);
         $table_editor->add_option_table('groups', 'groups.id = t.asso_id');
-        $table_editor->add_option_field('groups.diminutif', 'group_name', 'groupe', 'varchar','account');
+        $table_editor->add_option_field('groups.diminutif', 'group_name', 'groupe', 'varchar','iban');
 
-        // check RIB key
+        /* check RIB key     FIXME: the column format (and name) changed
         if ($action == 'update' && Post::has('account') && !check_rib(Post::v('account'))) {
             $page->trigError("Le RIB n'est pas valide");
             $table_editor->apply($page, 'edit', $id);
             return;
         }
+        */
 
         $table_editor->apply($page, $action, $id);
     }
@@ -710,12 +711,26 @@ class PaymentModule extends PLModule
                                   $recongp['id']);
                 $recongp['recons'] = $res->fetchAllAssoc();
 
-                $res = XDB::query('SELECT  t.id, t.payment_id, t.amount, b.owner, t.message, t.date
+                $res = XDB::query('SELECT  t.id, t.payment_id, t.amount, t.message, t.date
                                      FROM  payment_transfers    AS t
-                                LEFT JOIN  payment_bankaccounts AS b ON (t.account_id=b.id)
                                     WHERE  recongroup_id = {?}',
                                   $recongp['id']);
-                $recongp['transfers'] = $res->fetchAllAssoc();
+                $transfers = $res->fetchAllAssoc();
+                foreach ($transfers as $id => $t) {
+                    if ($t['date'] == NULL)  { // si le virement n'est pas fait, on va récupérer le rib associé au paiment
+                        $ownertmp = XDB::fetchOneCell('SELECT  b.owner
+                                                         FROM  payment_bankaccounts AS b
+                                                    LEFT JOIN  payments             AS p ON (p.rib_id = b.id)
+                                                        WHERE  p.id = {?}', $t['payment_id']);
+                    } else { // sinon on prend celui associé au virement
+                        $ownertmp = XDB::fetchOneCell('SELECT  b.owner
+                                                         FROM  payment_bankaccounts AS b
+                                                    LEFT JOIN  payment_transfers    AS t ON (t.account_id = b.id)
+                                                        WHERE  t.id = {?}', $t['id']);
+                    }
+                    $transfers[$id]['owner'] = $ownertmp;
+                }
+                $recongp['transfers'] = $transfers;
 
                 $recongps[] = $recongp;
             }
@@ -915,11 +930,11 @@ class PaymentModule extends PLModule
 
             // create transfers
             XDB::execute('INSERT INTO  payment_transfers
-                               SELECT  NULL, {?}, t.ref, SUM(t.amount+t.commission), p.rib_id, p.text, NULL
+                               SELECT  NULL, {?}, t.ref, SUM(t.amount+t.commission), NULL, p.text, NULL
                                  FROM  payment_transactions AS t
                             LEFT JOIN  payments             AS p ON (t.ref = p.id)
                             LEFT JOIN  groups               AS g ON (p.asso_id = g.id)
-                                WHERE  t.recon_id IN {?}
+                                WHERE  t.recon_id IN {?} AND t.status = "confirmed"
                              GROUP BY  t.ref',
                          $recongp_id, $recon_ids);
 
@@ -942,9 +957,13 @@ class PaymentModule extends PLModule
 
         } elseif ($action == "confirm") {
             S::assert_xsrf_token();
+            $account_id = XDB::fetchOneCell('SELECT  rib_id
+                                               FROM  payments          AS p 
+                                          LEFT JOIN  payment_transfers AS t ON (t.payment_id = p.id)
+                                              WHERE  t.id = {?}', $id);
             XDB::execute('UPDATE  payment_transfers
-                             SET  date = NOW()
-                           WHERE  id = {?}', $id);
+                             SET  date = NOW(), account_id = {?}
+                           WHERE  id = {?}', $account_id, $id);
 
             $page->trigSuccess('Virement ' . $id . ' confirmé.');
             $this->handler_adm_reconcile($page);
@@ -986,7 +1005,7 @@ class PaymentLogsImporter extends CSVImporter {
 
     static public function compute_systempay_commission($line, $key, $relation) {
         static $EEE_countries = array(
-            'France', 'Allemagne', 'Autriche', 'Belgique', 'Bulgarie', 'Chypre', 'Suisse',
+            'France', 'Allemagne', 'Autriche', 'Belgique', 'Bulgarie', 'Chypre', 'Croatie',
             'Danemark', 'Espagne', 'Estonie', 'Finlande', 'Grèce', 'Hongrie', 'Irlande', 'Islande', 'Italie',
             'Lettonie', 'Liechtenstein', 'Lituanie', 'Luxembourg', 'Malte', 'Norvège', 'Pays-Bas', 'Pologne',
             'Portugal', 'Roumanie', 'Royaume-Uni', 'Slovaquie', 'Slovénie', 'Suède', 'République Tchèque'
@@ -999,7 +1018,7 @@ class PaymentLogsImporter extends CSVImporter {
         if (in_array($line['pays carte'], $EEE_countries)) {
             return -0.20 - round($amount * 0.005, 2);
         } else {
-            return -0.20 - round($amount * 0.005, 2) - 0.76;
+            return -0.20 - round($amount * 0.005, 2) - 0.75;
         }
     }
 
@@ -1008,7 +1027,7 @@ class PaymentLogsImporter extends CSVImporter {
             return null;
         }
         $reference = self::getValue($line, 'reference', $relation['reference']);
-        if (ereg('-([0-9]+)$', $reference, $matches)) {
+        if (preg_match('/-([0-9]+)$/', $reference, $matches)) {
             return $matches[1];
         } else {
             return null;
@@ -1016,5 +1035,5 @@ class PaymentLogsImporter extends CSVImporter {
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>