production/Controleur/ControleurAjaxtesterpaiementmobile.php
2025-12-02 11:29:44 +00:00

307 lines
9.4 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once 'Framework/Controleur.php';
require_once 'Modele/Societeuser.php';
require_once 'Modele/Societeusercentral.php';
require_once 'Modele/Facture.php';
class ControleurAjaxtesterpaiementmobile extends Controleur {
private $societeuser;
private $societeusercentral;
private $facture;
private $parammobile;
public function __construct()
{
$this->societeuser = new Societeuser();
$this->societeusercentral = new Societeusercentral();
$this->facture = new Facture();
$codeSociete = $_SESSION['codeSociete'];
$societe = $this->societeuser->getSocieteSimple($codeSociete);
$this->parammobile = $this->societeusercentral->getSocieteusersPaiementMobile($codeSociete);
$parammobile = $this->parammobile;
$this->fournisseurPaiementMobile = $parammobile['fournisseurPaiementMobile'];
$this->apikeyPaiementMobile = $parammobile['apikeyPaiementMobile'];
$this->passwordPaiementMobile = $parammobile['passwordPaiementMobile'];
$this->emailPaiementMobile = $parammobile['emailPaiementMobile'];
$this->paiementMobileSandBox = $parammobile['paiementMobileSandBox'];
$this->codePays = $societe['codePays'];
$this->indicatifTelephone = $societe['indicatifTelephone'];
$this->nomProduitSante = $societe['nomProduitSante'];
}
public function index()
{
}
public function envoyerargent()
{
$fournisseurPaiementMobile = $this->fournisseurPaiementMobile;
$destinataires = $this->requete->getParametreFormulaire("destinataires");
$idReglement = $this->requete->getParametreFormulaire("idReglement");
$withdrawModePaiementMobile = $this->requete->getParametreFormulaire("withdrawModePaiementMobile");
if($fournisseurPaiementMobile>" ")
{
$idReglement = $this->requete->getParametreFormulaire("idReglement");
if($fournisseurPaiementMobile=="cinetpay")
{
$this->envoyerargentcinetpay($idReglement, $withdrawModePaiementMobile, $destinataires);
}
elseif($fournisseurPaiementMobile=="paydunya")
{
$this->envoyerargentpaydunya($idReglement, $withdrawModePaiementMobile, $destinataires);
}
}
else
{
echo "Pas de Fournisseur de paiement mobile";
}
}
public function envoyerargentcinetpay($idReglement, $withdrawModePaiementMobile, $destinataires)
{
$reglement = $this->facture->getPaiementMobile($idReglement);
$codeSociete = $_SESSION['codeSociete'];
$notify_url = $_SESSION['lienProduction_C'];
$name = $reglement['beneficiaireReglement'];
$surname = $codeSociete;
$phone = $reglement['mobilePaiement'];
$phone = $destinataires; // Pour les tests
$prefix = $reglement['prefixePaiementMobile'];
$amount = $reglement['montantApayer'];
$email = $this->emailPaiementMobile;
$token = null;
$apikey = $this->apikeyPaiementMobile;
$password = $this->passwordPaiementMobile;
if($apikey>" " && $password>" ")
{
$ch = curl_init('https://client.cinetpay.com/v1/auth/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'apikey' => $apikey,
'password' => $password,
'lang' => 'fr'
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded'
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if(isset($data['data']['token'])){
$token = $data['data']['token'];
} else {
echo "Erreur auth: ".print_r($data, true);
exit();
}
if($token)
{
// Ajout contact à cinetpay
$payload = json_encode([
[
"prefix" => $prefix,
"phone" => $phone, // numéro local sans +225 dans "phone"
"name" => $name,
"surname"=> $surname,
"email" => $email
]
]);
$ch = curl_init("https://client.cinetpay.com/v1/transfer/contact?token={$token}&lang=fr");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['data' => $payload]));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
curl_exec($ch);
curl_close($ch);
// Envoyer largent au contact — ordre de transfert
$amount = 200; // pour les tests => enlever en production
$data = [
[
"prefix" => $prefix,
"phone" => $phone,
"amount" => $amount,
"client_transaction_id" => "$codeSociete-$idReglement"."-".uniqid(), // empêche le paiement 2 fois du même décompte
"notify_url" => $notify_url
]
];
$payload = json_encode($data);
$ch = curl_init("https://client.cinetpay.com/v1/transfer/money/send/contact?token={$token}&lang=fr");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['data' => $payload]));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
$response = curl_exec($ch);
curl_close($ch);
// action à faire si le paiement est passé => enregitsrer paiement décompte dans INTER-SANTE
$data = json_decode($response, true);
$codeOp = $data['data'][0][0]["code"];
if(isset($codeOp) and $codeOp=="0"){ // Succès
$transaction_id = $data['data'][0][0]["transaction_id"];
$idPaiementDecompte = $this->facture->payerParMobile($idReglement, $amount, $transaction_id, $this->fournisseurPaiementMobile);
var_dump
(
array
(
"transaction_id" => $transaction_id,
"idPaiementDecompte" => $idPaiementDecompte,
)
);
exit();
} else {
echo "Erreur auth: ".print_r($data, true);
exit();
}
}
}
else
{
echo "Fournisseur de paiement mobile inconnu!";
}
}
public function envoyerargentpaydunya($idReglement, $withdrawModePaiementMobile, $destinataires)
{
$reglement = $this->facture->getPaiementMobile($idReglement);
$codeSociete = $_SESSION['codeSociete'];
$notify_url = $_SESSION['lienProduction_C'];
$name = $reglement['beneficiaireReglement'];
$surname = $codeSociete;
$phone = $reglement['mobilePaiement'];
$phone = $destinataires; // Pour les tests
$prefix = $reglement['prefixePaiementMobile'];
$amount = $reglement['montantApayer'];
$email = $this->emailPaiementMobile;
$PD_MASTER_KEY = $this->parammobile['apikeyPaiementMobile'];
$PD_PRIVATE_KEY = $this->parammobile['clePrivePaiementMobile'];
$PD_PUBLIC_KEY = $this->parammobile['clePubliquePaiementMobile'];
$PD_TOKEN = $this->parammobile['passwordPaiementMobile'];
$paiementMobileSandBox = $this->parammobile['paiementMobileSandBox'];
$amount = 200; // pour les tests => enlever en production
$payloadlnvoice =
[
"account_alias" => $phone, // numéro local sans code pays
"amount" => $amount,
"withdraw_mode" => $withdrawModePaiementMobile,
"callback_url" => $notify_url,
];
$urlInvoice = 'https://app.paydunya.com/api/v2/disburse/get-invoice';
$ch = curl_init($urlInvoice);
$headers = [
'Content-Type: application/json',
"PAYDUNYA-MASTER-KEY: $PD_MASTER_KEY",
"PAYDUNYA-PRIVATE-KEY: $PD_PRIVATE_KEY",
"PAYDUNYA-PUBLIC-KEY: $PD_PUBLIC_KEY",
"PAYDUNYA-TOKEN: $PD_TOKEN",
];
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => $headers,
// CURLOPT_TIMEOUT => 45,
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payloadlnvoice, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if(isset($data['disburse_token'])){
$response_code = $data['response_code'];
$disburse_token = $data['disburse_token'];
} else {
echo "Erreur auth: ".print_r($data, true);
exit();
}
if($disburse_token && $response_code=="00")
{
$payloadPayment =
[
"disburse_invoice" => $disburse_token, // le token reçu de get-invoice
"disburse_id" => "$codeSociete-$idReglement"."-".uniqid(), // empêche le paiement 2 fois du même décompte
];
$urlPayment = "https://app.paydunya.com/api/v2/disburse/submit-invoice";
$ch = curl_init($urlPayment);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => $headers,
// CURLOPT_TIMEOUT => 45,
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payloadPayment, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if(isset($data['transaction_id']) && $data['response_code']=="00"){
$transaction_id = $data['transaction_id'];
$idPaiementDecompte = $this->facture->payerParMobile($idReglement, $amount, $transaction_id, $this->fournisseurPaiementMobile);
var_dump
(
array
(
"transaction_id" => $transaction_id,
"idPaiementDecompte" => $idPaiementDecompte,
)
);
exit();
} else {
echo "Erreur auth: ".print_r($data, true);
exit();
}
}
}
}