router = $router; $this->transactionManager = $transactionManager; } public function generate_customer_xml(UserBasketInterface $user, string $companyName){ // $gender = ($user->getGender() == 0)?'M':'Mme'; $data = [ 'Custom'=>'preScoringCode=PS2208130&birthDate='.$user->getBirthdate()->format('Y-m-d') ]; // Create XLM $xml = new \DOMDocument('1.0','utf-8'); $tag = $xml->createElement('Customer'); $xml->appendChild($tag); foreach ($data as $key=>$value){ $tag_chield = $xml->createElement($key, $value); $tag->appendChild($tag_chield); } return str_replace("\n","",$xml->saveXML()); } public function generate_billing_xml(UserBasketInterface $user){ $data = [ 'FirstName'=>$user->getFirstName(), 'LastName'=>$user->getLastName(), 'Address1'=>$user->getAddress(), 'ZipCode'=>preg_replace('/[^0-9]/', '', $user->getZipCode()), 'City'=>$user->getCity(), 'CountryCode'=>250, 'CountryName'=>$user->getCountry(), 'CountryCodeHomePhone'=>'+33', 'HomePhone'=>preg_replace('/[^0-9]/', '', $user->getPhone()), 'CountryCodeMobilePhone'=>'+33', 'MobilePhone'=>preg_replace('/[^0-9]/', '', $user->getCellPhone()), ]; $xml = new \DOMDocument('1.0','utf-8'); $tag = $xml->createElement('Billing'); $tag_address = $xml->createElement('Address'); $tag->appendChild($tag_address); $xml->appendChild($tag); foreach ($data as $key=>$value){ $tag_chield = $xml->createElement($key, $value); $tag_address->appendChild($tag_chield); } return str_replace("\n","",$xml->saveXML()); } public function init(TransactionInterface $transaction): array { // Init Variables $amount = number_format($transaction->getTotal() , 2, '.', '') * 100; //montant en centimes // $date1 = date('d/m/Y', strtotime('+1 month')); // $date2 = date('d/m/Y', strtotime('+2 month')); $pbx_hash = 'SHA512'; // Paramétre de base $PBX_SITE = $transaction->getPaymentMode()->getShopKey(); $PBX_RANG = $transaction->getPaymentMode()->getRang(); $PBX_IDENTIFIANT = $transaction->getPaymentMode()->getIdentifier(); $PBX_PORTEUR = $transaction->getUser()->getEmail(); // email $PBX_CMD = 'CMD' . substr($transaction->getPublicKey(),0, 12); // numero du panier $devise = $transaction->getPaymentMode()->getCurrency(); $PBX_EFFECTUE = $this->router->generate ( $transaction->getPaymentMode()->getUrlReturnSuccess(), [], UrlGeneratorInterface::ABSOLUTE_URL ); $PBX_ANNULE = $this->router->generate ( $transaction->getPaymentMode()->getUrlReturnError(), [], UrlGeneratorInterface::ABSOLUTE_URL ); $PBX_REFUSE = $this->router->generate ( $transaction->getPaymentMode()->getUrlReturnError(), [], UrlGeneratorInterface::ABSOLUTE_URL ); $PBX_REPONDRE_A = $this->router->generate ( 'api_3xssfrais_ipn_paiement', array ('publicKey' => $transaction->getPublicKey()), UrlGeneratorInterface::ABSOLUTE_URL ); // $PBX_TYPEPAIEMENT = 'LIMONETIK'; $PBX_TYPECARTE = 'COF3X'; //'SOF3X';//'SOF3XSF';//'COF3X'; $PBX_CUSTOMER = $this->generate_customer_xml($transaction->getUser(), $transaction->getPaymentMode()->getCompanyName()); $PBX_BILLING = $this->generate_billing_xml($transaction->getUser()); // On récupère la date au format ISO-8601 $datetime = date ('c'); // On crée la chaîne à hacher sans URL encodage $msg = 'PBX_SITE=' .$PBX_SITE . '&PBX_RANG=' . $PBX_RANG . '&PBX_IDENTIFIANT=' . $PBX_IDENTIFIANT . '&PBX_TOTAL=' . $amount . '&PBX_DEVISE=' . $devise . '&PBX_CMD=' . $PBX_CMD . '&PBX_PORTEUR=' . $PBX_PORTEUR . '&PBX_RETOUR=montant:M;invoice:R;auto:A;erreur:E' . '&PBX_EFFECTUE=' . $PBX_EFFECTUE . '&PBX_ANNULE=' . $PBX_ANNULE . '&PBX_REFUSE=' . $PBX_REFUSE . '&PBX_REPONDRE_A=' . $PBX_REPONDRE_A . '&PBX_HASH=' . $pbx_hash . '&PBX_TIME=' . $datetime . '&PBX_TYPEPAIEMENT=' . $PBX_TYPEPAIEMENT . '&PBX_TYPECARTE=' . $PBX_TYPECARTE . '&PBX_CUSTOMER=' . $PBX_CUSTOMER . '&PBX_BILLING=' . $PBX_BILLING ; // On récupère la clé secrète HMAC et que on renseigne dans la variable $keyTest; // Si la clé est en ASCII, On la transforme en binaire $keyTest = $transaction->getPaymentMode()->getCertificate(); $binKey = pack ( 'H*', $keyTest ); // On calcule l’empreinte (à renseigner dans le paramètre PBX_HMAC) grâce à la fonction hash_hmac et // la clé binaire // On envoie via la variable PBX_HASH algorithme de hachage qui a été utilisé (SHA512 dans ce cas) $hmac = strtoupper ( hash_hmac ( $pbx_hash, $msg, $binKey ) ); // La chaîne sera envoyée en majuscules, d où utilisation de strtoupper() // On crée le formulaire à envoyer à PAYBOX System // ATTENTION : ordre des champs est extrêmement important, il doit // correspondre exactement à ordre des champs dans la chaîne hachée return [ 'publicKey' => $transaction->getPublicKey(), 'pbx_hmac' => $hmac, 'datetime' => $datetime, 'PBX_HASH' => $pbx_hash, 'PBX_PORTEUR' => $PBX_PORTEUR, 'PBX_CMD' => $PBX_CMD, 'PBX_TOTAL' => $amount, 'PBX_DEVISE' => $devise, 'PBX_IDENTIFIANT' => $PBX_IDENTIFIANT, 'PBX_RANG' => $PBX_RANG, 'PBX_EFFECTUE' => $PBX_EFFECTUE, 'PBX_ANNULE' => $PBX_ANNULE, 'PBX_REFUSE' => $PBX_REFUSE, 'PBX_REPONDRE_A' => $PBX_REPONDRE_A, 'PBX_SITE' => $PBX_SITE, 'url_paybox' => $transaction->getPaymentMode()->getUrlPayment(), 'PBX_TYPEPAIEMENT' => $PBX_TYPEPAIEMENT, 'PBX_TYPECARTE' => $PBX_TYPECARTE , 'PBX_CUSTOMER' => $PBX_CUSTOMER , 'PBX_BILLING' => $PBX_BILLING, ] ; } /** * @param TransactionInterface $transaction * @param string $id_transaction * @param string $message * @param float $amount * @param bool $status * @return bool */ public function ipn(TransactionInterface $transaction, string $id_transaction, string $message, float $amount, bool $status) { // Change transaction status on paid if($status){ $transaction->setStatus(TransactionStatusAction::TRANSACTION_PAID); $transaction->setTotalPayer($amount); } else $transaction->setStatus(TransactionStatusAction::TRANSACTION_ERROR); $transaction->setInformation($message); $transaction->setIdTransactionExternal($id_transaction); $this->transactionManager->update($transaction); return true; } }