예제 #1
0
/**
 * il faut avoir un id_transaction et un transaction_hash coherents
 * pour se premunir d'une tentative d'appel exterieur
 *
 * @param array $config
 * @param null|array $response
 * @return array
 */
function presta_credit_call_response_dist($config, $response = null)
{
    $mode = $config['presta'];
    // recuperer la reponse en post et la decoder, en verifiant la signature
    if (!$response) {
        $response = bank_response_simple($mode);
    }
    if (!isset($response['id_transaction']) or !isset($response['transaction_hash'])) {
        return bank_transaction_invalide(0, array('mode' => $mode, 'erreur' => "id_transaction ou transaction_hash absent", 'log' => bank_shell_args($response)));
    }
    $id_transaction = $response['id_transaction'];
    $transaction_hash = $response['transaction_hash'];
    if (!($row = sql_fetsel('*', 'spip_transactions', 'id_transaction=' . intval($id_transaction)))) {
        return bank_transaction_invalide($id_transaction, array('mode' => $mode, 'erreur' => "transaction inconnue", 'log' => bank_shell_args($response)));
    }
    if ($transaction_hash != $row['transaction_hash']) {
        return bank_transaction_invalide($id_transaction, array('mode' => $mode, 'erreur' => "id_transaction {$id_transaction}, hash {$transaction_hash} non conforme", 'log' => bank_shell_args($response)));
    }
    // Obtenir la devise.
    $auteur = $row['auteur'];
    include_spip('reservations_credits_fonctions');
    /*$credit = credit_client('', $row['auteur'], $devise);
    	spip_log("credit : $credit, montant $montant,auteur $auteur", 'credit');*/
    if ($id_reservation = $row['id_reservation']) {
        $donnees = sql_fetsel('spip_reservations_details.devise,reference,email,id_auteur', 'spip_reservations LEFT JOIN spip_reservations_details USING (id_reservation)', 'spip_reservations.id_reservation=' . $id_reservation);
        $devise = $donnees['devise'];
        $descriptif = _T('reservation_bank:paiement_reservation', array('id_reservation' => $id_reservation));
    } elseif ($id_commande = $row['id_commande']) {
        $devise = 'EUR';
        $descriptif = _T('reservation_bank:paiement_commande', array('id_commande' => $id_commande));
        $id_objet = $id_commande;
        $objet = 'commande';
    } else {
        return bank_transaction_invalide($id_transaction, array('mode' => $mode, 'erreur' => "id_transaction {$id_transaction}, hash {$transaction_hash} objet non connu", 'log' => bank_shell_args($response)));
    }
    // Si on trouve un crédit
    if (isset($row['auteur']) and $email = $row['auteur'] and $credit = credit_client('', $row['auteur'], $devise) and (intval($credit) >= 0 or floatval($var) >= 0.0)) {
        if (!($montant_reservations_detail_total = _request('montant_reservations_detail_total'))) {
            include_spip('inc/reservation_bank');
            $montant_reservations_detail_total = montant_reservations_detail_total($id_reservation);
        }
        $paiement_detail = array();
        foreach (array_keys($montant_reservations_detail_total) as $id_reservation_detail) {
            $paiement_detail[$id_reservation_detail] = _request('montant_reservations_detail_' . $id_reservation_detail);
        }
        if (!($montant_regle = array_sum($paiement_detail))) {
            $montant_regle = $transaction['montant'];
        }
        $set = array("mode" => $mode, "montant_regle" => $montant_regle, "date_paiement" => date('Y-m-d H:i:s'), "statut" => 'ok', "reglee" => 'oui');
        if (intval($credit) >= intval($row['montant']) or floatval($credit) >= floatval($row['montant'])) {
            // OK, on peut accepter le reglement
            $statut = 'reglée';
            $res = true;
        } else {
            // Le crédit n'est pas suffisant
            $set['montant_regle'] = $montant_regle;
            $set['statut'] = 'attente';
            $set['reglee'] = 'par';
            $statut = 'reglée acompte';
            $res = 'wait';
        }
        sql_updateq("spip_transactions", $set, "id_transaction=" . intval($id_transaction));
        spip_log("call_response : id_transaction {$id_transaction}, {$statut}", $mode);
        // Enregistrer un mouvement crédit
        $action = charger_fonction('editer_objet', 'action');
        $reference = $donnes['reference'];
        $set = array('type' => 'debit', 'email' => $email, 'descriptif' => $descriptif, 'id_reservation' => $id_reservation, 'id_objet' => $id_objet, 'objet' => $objet, 'montant' => $montant_regle, 'devise' => $donnees['devise']);
        $action('new', 'reservation_credit_mouvement', $set);
    } else {
        return bank_transaction_invalide($id_transaction, array('mode' => $mode, 'erreur' => "id_transaction {$id_transaction}, montant " . $row['montant'] . "> pas de crédit diponible", 'log' => bank_shell_args($response)));
    }
    $regler_transaction = charger_fonction('regler_transaction', 'bank');
    $regler_transaction($id_transaction, array('row_prec' => $row));
    return array($id_transaction, $res);
}
/**
 * permet de modifier le tableau de valeurs envoyé par la fonction charger d’un formulaire CVT
 *
 * @pipeline formulaire_charger
 *
 * @param array $flux
 *        	Données du pipeline
 * @return array Données du pipeline
 */
function reservation_bank_formulaire_charger($flux)
{
    $form = $flux['args']['form'];
    if ($form == 'reservation') {
        $flux['data']['checkout'] = _request('checkout');
        if ($flux['data']['checkout'] = _request('checkout')) {
            $flux['data']['message_ok'] .= recuperer_fond('inclure/paiement_reservation', array('id_reservation' => session_get('id_reservation'), 'cacher_paiement_public' => FALSE));
            $flux['data']['editable'] = FALSE;
        }
    }
    if ($form == 'encaisser_reglement') {
        $id_transaction = $flux['data']['_id_transaction'];
        // Les infos supplémentaires de la transaction
        $transaction = sql_fetsel('id_reservation,montant,auteur', 'spip_transactions', 'id_transaction=' . $id_transaction);
        $id_reservation = $flux['id_reservation'] = $transaction['id_reservation'];
        $montant_transaction = $flux['montant'] = $transaction['montant'];
        // Cas spécial pour les crédits
        if ($flux['data']['_mode'] == 'credit' and $credit = credit_client('', $transaction['auteur'])) {
            $flux['data']['credit'] = '';
            $flux['data']['email_client'] = $email_client = $transaction['auteur'];
            $flux['_hidden'] .= '<input name="email_client" value="' . $email_client . '" type="hidden"/>';
        }
        // Définir les champs pour les détails de réservation.
        $sql = sql_select('id_reservations_detail,prix,prix_ht,quantite,devise,taxe,descriptif,montant_paye', 'spip_reservations_details', 'id_reservation=' . $id_reservation);
        $montant_detail = array();
        $montant_reservations_detail_defaut = array();
        $montant_reservations_detail_total = array();
        $count = sql_count($sql);
        $montant_transaction_detail = '';
        if ($count > 0) {
            $montant_transaction_detail = $montant_transaction / $count;
        }
        $montant_ouvert = '';
        $montant_defaut = '';
        while ($data = sql_fetch($sql)) {
            $id_reservations_detail = $data['id_reservations_detail'];
            $devise = $data['devise'];
            $montant_paye[$id_reservations_detail] = $data['montant_paye'];
            if ($montant = $data['prix'] <= 0) {
                $montant = $data['prix_ht'] + $data['taxe'];
            }
            $montant_reservations_detail_total[$id_reservations_detail] = $montant;
            $montant_ouvert = $montant_defaut = $montant - $data['montant_paye'];
            if ($montant_ouvert < $montant_transaction_detail and $montant_ouvert >= 0) {
                if (!($montant_defaut = _request('montant_reservations_detail_' . $id_reservations_detail))) {
                    $montant_defaut = $montant_ouvert;
                }
            }
            if ($credit[$devise] > 0 and $credit[$devise] / $count <= $montant_defaut) {
                $montant_defaut = $credit[$devise] / $count;
            }
            if ($montant_defaut > 0) {
                $montant_detail[] = array('saisie' => 'input', 'options' => array('nom' => 'montant_reservations_detail_' . $id_reservations_detail, 'label' => $data['descriptif'], 'defaut' => $montant_defaut, 'size' => 20));
            }
            $flux['data']['montant_reservations_detail_' . $id_reservations_detail] = '';
            $montant_reservations_detail_defaut[$id_reservations_detail] = $montant_ouvert;
        }
        if ($credit) {
            $flux['credit'] = '';
            $flux['_hidden'] .= '<input name="credit" value="' . $credit[$devise] . '" type="hidden"/>';
        }
        $flux['_mes_saisies'] = array(array('saisie' => 'fieldset', 'options' => array('nom' => 'specifier', 'label' => _T('reservation_bank:label_fieldset_specifier')), 'saisies' => array(array('saisie' => 'oui_non', 'options' => array('nom' => 'specifier_montant', 'label' => _T('reservation_bank:label_specifier_montant'), 'defaut' => _request('specifier_montant'))))), array('saisie' => 'fieldset', 'options' => array('nom' => 'montant', 'label' => _T('reservation_bank:label_fieldset_montant_detail', array('devise' => $devise)), 'afficher_si' => '@specifier_montant@ == "on"'), 'saisies' => $montant_detail));
        $flux['data']['specifier_montant'] = _request('specifier_montant');
        $flux['data']['montant_reservations_detail_defaut'] = '';
        $flux['data']['montant_paye'] = $montant_paye;
        $flux['_hidden'] .= '<input name="id_reservation" value="' . $id_reservation . '" type="hidden"/>';
        $flux['_hidden'] .= '<input name="id_transaction" value="' . $id_transaction . '" type="hidden"/>';
        $montant_reservations_detail_defaut = serialize($montant_reservations_detail_defaut);
        $montant_reservations_detail_total = serialize($montant_reservations_detail_total);
        $flux['_hidden'] .= "<input name='montant_reservations_detail_defaut' value='{$montant_reservations_detail_defaut}' type='hidden'/>";
        $flux['_hidden'] .= "<input name='montant_reservations_detail_total' value='{$montant_reservations_detail_total}' type='hidden'/>";
    }
    return $flux;
}