Пример #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_gratuit_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)));
    }
    // verifier que la commande a bien un total nul, sinon ce mode de paiement n'est pas autorise
    if (intval($row['montant']) > 0 or floatval($row['montant']) > 0.0) {
        return bank_transaction_invalide($id_transaction, array('mode' => $mode, 'erreur' => "id_transaction {$id_transaction}, montant " . $row['montant'] . ">0 interdit", 'log' => bank_shell_args($response)));
    }
    // OK, on peut accepter le reglement
    $set = array("mode" => $mode, "montant_regle" => $row['montant'], "date_paiement" => date('Y-m-d H:i:s'), "statut" => 'ok', "reglee" => 'oui');
    sql_updateq("spip_transactions", $set, "id_transaction=" . intval($id_transaction));
    spip_log("call_resonse : id_transaction {$id_transaction}, reglee", $mode);
    $regler_transaction = charger_fonction('regler_transaction', 'bank');
    $regler_transaction($id_transaction, array('row_prec' => $row));
    return array($id_transaction, true);
}
Пример #2
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_simu_call_response_dist($config, $response = null)
{
    include_spip('inc/bank');
    $mode = $config['presta'];
    // recuperer la reponse en post et la decoder, en verifiant la signature
    if (!$response) {
        $response = bank_response_simple($mode);
    }
    // est-ce une simulation d'echec ?
    if (_request('status') == 'fail') {
        $response['fail'] = "Simulation echec paiement";
    }
    // generer un numero d'abonne simule si besoin (sauf si on en a deja un)
    if (_request('abo')) {
        if ($response['id_transaction'] and $abo_uid = sql_getfetsel("abo_uid", "spip_transactions", "id_transaction=" . intval($response['id_transaction']))) {
            $response['abo_uid'] = $abo_uid;
        } else {
            $response['abo_uid'] = substr(md5($response['id_transaction'] . "-" . time()), 0, 10);
        }
    }
    return bank_simple_call_response($config, $response);
}
Пример #3
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);
}
Пример #4
0
/**
 * Verifier le statut d'une transaction lors du retour de l'internaute
 *
 * @param array $config
 * @param null|array $response
 * @return array
 */
function presta_stripe_call_response_dist($config, $response = null)
{
    include_spip('inc/bank');
    $mode = $config['presta'];
    if (isset($config['mode_test']) and $config['mode_test']) {
        $mode .= "_test";
    }
    // recuperer la reponse en post et la decoder, en verifiant la signature
    if (!$response) {
        $response = bank_response_simple($mode);
    }
    // Stripe token
    $token = '';
    if (isset($_REQUEST['stripeToken'])) {
        $token = $_REQUEST['stripeToken'];
    }
    if (!$response or !$token and !$response['charge']) {
        spip_log("call_response : token/charge invalide", $mode . _LOG_ERREUR);
        return array(0, false);
    }
    if ($token) {
        $response['token'] = $token;
    }
    if (isset($_REQUEST['stripeTokenType'])) {
        $response['token_type'] = $_REQUEST['stripeTokenType'];
    }
    $recurence = false;
    // c'est une reconduction d'abonnement ?
    if ($response['charge_id'] and $response['abo_uid']) {
        // verifier qu'on a pas deja traite cette recurrence !
        if ($t = sql_fetsel("*", "spip_transactions", "autorisation_id LIKE " . sql_quote("%/" . $response['charge_id']))) {
            $response['id_transaction'] = $t['id_transaction'];
            $response['transaction_hash'] = $t['transaction_hash'];
        } elseif ($preparer_echeance = charger_fonction('preparer_echeance', 'abos', true)) {
            $abo_uid = $response['abo_uid'];
            $id_transaction = $preparer_echeance("uid:" . $abo_uid);
            // on reinjecte le bon id de transaction ici si fourni
            if ($id_transaction) {
                $response['id_transaction'] = $id_transaction;
                $response['transaction_hash'] = sql_getfetsel('transaction_hash', 'spip_transactions', 'id_transaction=' . intval($id_transaction));
            } else {
                return bank_transaction_invalide($response['abo_uid'] . '/' . $response['charge_id'], array('mode' => $mode, 'sujet' => 'Echec creation transaction echeance', 'erreur' => "uid:" . $response['abo_uid'] . ' inconnu de $preparer_echeance', 'log' => bank_shell_args($response), 'update' => false, 'send_mail' => true));
            }
        }
        $recurence = true;
    }
    // depouillement de la transaction
    // stripe_traite_reponse_transaction modifie $response
    list($id_transaction, $success) = stripe_traite_reponse_transaction($config, $response);
    if (($recurence or $response['abo']) and $abo_uid = $response['abo_uid'] and $id_transaction) {
        // c'est un premier paiement d'abonnement, l'activer
        if (!$recurence) {
            if ($success) {
                // date de fin de mois de validite de la carte
                $date_fin = "0000-00-00 00:00:00";
                if (isset($response['validite'])) {
                    list($year, $month) = explode('-', $response['validite']);
                    $date_fin = bank_date_fin_mois($year, $month);
                }
                #spip_log('response:'.var_export($response,true),$mode.'db');
                #spip_log('date_fin:'.$date_fin,$mode.'db');
                if ($activer_abonnement = charger_fonction('activer_abonnement', 'abos', true)) {
                    $activer_abonnement($id_transaction, $abo_uid, $mode, $date_fin);
                }
            }
        } else {
            // reussi, il faut repercuter sur l'abonnement
            if ($success) {
                if ($renouveler_abonnement = charger_fonction('renouveler_abonnement', 'abos', true)) {
                    $renouveler_abonnement($id_transaction, $response['abo'], $mode);
                }
            }
            // echoue, il faut resilier l'abonnement
            if (!$success) {
                if ($resilier = charger_fonction('resilier', 'abos', true)) {
                    $options = array('notify_bank' => false, 'immediat' => true, 'message' => "[bank] Transaction #{$id_transaction} refusee");
                    $resilier("uid:{$abo_uid}", $options);
                }
            }
        }
    }
    return array($id_transaction, $success);
}
Пример #5
0
/**
 * Call response simple (cheque, virement, simu)
 * 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 bank_simple_call_response($config, $response = null)
{
    $mode = $config['presta'];
    $config_id = bank_config_id($config);
    // 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' => "transaction inconnue", 'log' => var_export($response, true)));
    }
    $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 non trouvee", 'log' => var_export($response, true)));
    }
    if ($transaction_hash != $row['transaction_hash']) {
        return bank_transaction_invalide($id_transaction, array('mode' => $mode, 'erreur' => "hash {$transaction_hash} non conforme", 'log' => var_export($response, true)));
    }
    $autorisation = isset($response['autorisation_id']) ? $response['autorisation_id'] : '';
    if ($autorisation === "wait") {
        // c'est un reglement en attente, on le note
        $set = array("mode" => "{$mode}/{$config_id}", 'autorisation_id' => date('d/m/Y-H:i:s') . "/" . $GLOBALS['ip'], "date_paiement" => date('Y-m-d H:i:s'), "statut" => 'attente');
    } else {
        // si rien fourni l'autorisation refere l'id_auteur et le nom de celui qui accepte le cheque|virement
        if (!$autorisation) {
            $autorisation = $GLOBALS['visiteur_session']['id_auteur'] . "/" . $GLOBALS['visiteur_session']['nom'];
        }
        include_spip("inc/autoriser");
        if (!autoriser('utilisermodepaiement', $mode)) {
            return bank_transaction_invalide($id_transaction, array('mode' => $mode, 'erreur' => "{$mode} pas autorisee"));
        }
        if (!autoriser('encaisser' . $mode, 'transaction', $id_transaction)) {
            return bank_transaction_invalide($id_transaction, array('mode' => $mode, 'erreur' => "tentative d'encaisser un {$mode} par auteur #{$autorisation} pas autorise"));
        }
        // est-ce une demande d'echec ? (cas de la simulation)
        if (isset($response['fail']) and $response['fail']) {
            // sinon enregistrer l'absence de paiement et l'erreur
            include_spip('inc/bank');
            return bank_transaction_echec($id_transaction, array('mode' => $mode, 'config_id' => $config_id, 'code_erreur' => 'fail', 'erreur' => $response['fail']));
        }
        // OK, on peut accepter le reglement
        $set = array("mode" => "{$mode}/{$config_id}", "autorisation_id" => $autorisation, "montant_regle" => $row['montant'], "date_paiement" => date('Y-m-d H:i:s'), "statut" => 'ok', "reglee" => 'oui');
    }
    // est-ce un abonnement ?
    if (isset($response['abo_uid']) and $response['abo_uid']) {
        $set['abo_uid'] = $response['abo_uid'];
    }
    sql_updateq("spip_transactions", $set, "id_transaction=" . intval($id_transaction));
    // si ok on regle
    if ($set['statut'] === 'ok') {
        spip_log("call_resonse : id_transaction {$id_transaction}, reglee", $mode);
        $regler_transaction = charger_fonction('regler_transaction', 'bank');
        $regler_transaction($id_transaction, array('row_prec' => $row));
        $res = true;
    } else {
        // cela permet de factoriser le code
        $row = sql_fetsel('*', 'spip_transactions', 'id_transaction=' . intval($id_transaction));
        pipeline('trig_bank_reglement_en_attente', array('args' => array('statut' => 'attente', 'mode' => $row['mode'], 'type' => $row['abo_uid'] ? 'abo' : 'acte', 'id_transaction' => $id_transaction, 'row' => $row), 'data' => ''));
        $res = 'wait';
    }
    // Si c'est un abonnnement, activer ou resilier
    if ($id_transaction and $row = sql_fetsel("*", "spip_transactions", "id_transaction=" . intval($id_transaction)) and $abo_uid = $row['abo_uid']) {
        // c'est un paiement reussi ou en 'wait'
        if ($res) {
            // date de fin de mois de validite de la carte
            $date_fin = "0000-00-00 00:00:00";
            if ($row['validite']) {
                list($year, $month) = explode('-', $row['validite']);
                $date_fin = bank_date_fin_mois($year, $month);
            }
            if ($activer_abonnement = charger_fonction('activer_abonnement', 'abos', true)) {
                $activer_abonnement($id_transaction, $abo_uid, $mode, $date_fin);
            }
        }
        // c'est un echec, il faut le resilier, que ce soit la premiere ou la Nieme transaction
        if (!$res) {
            if ($resilier = charger_fonction('resilier', 'abos', true)) {
                $options = array('notify_bank' => false, 'immediat' => true, 'message' => "[bank] Transaction #{$id_transaction} refusee");
                $resilier("uid:" . $abo_uid, $options);
            }
        }
    }
    return array($id_transaction, $res);
}