router = $container->get('router'); $this->transactionManager = $container->get('net15.transaction.manager'); } private function redirectURL(TransactionInterface $transaction): array { return [ 'urlConfirmation' => $this->router->generate ( 'api_payline3x_confirmation_paiement', ['publicKey'=>$transaction->getPublicKey()], UrlGeneratorInterface::ABSOLUTE_URL ), 'urlCancel' => $this->router->generate ( $transaction->getPaymentMode()->getUrlReturnError(), [], UrlGeneratorInterface::ABSOLUTE_URL ), 'urlSuccess' => $this->router->generate ( $transaction->getPaymentMode()->getUrlReturnSuccess(), [], UrlGeneratorInterface::ABSOLUTE_URL ) ]; } /** * @param TransactionInterface $transaction * @return array * @throws \Exception */ public function initPayment(TransactionInterface $transaction){ // Init Variables $paymentMode = $transaction->getPaymentMode(); $environment = ($paymentMode->getContext() == 'TEST')? 'HOMO' : 'PROD'; $totalAmount = number_format($transaction->getTotal() , 2, '.', '') * 100; //montant en centimes // Init Phone Format $phoneFormat = preg_replace('/[^0-9]/', '', $transaction->getUser()->getCellPhone()); if (mb_strlen($phoneFormat) <= 10){ $phoneFormat = preg_replace('/^(?!00[^0])0/i', '+33', $phoneFormat); } // init redirect url $urls = $this->redirectURL($transaction); // Init BirthDate $birthDate = $transaction->getUser()->getBirthdate(); // Init gender switch ($transaction->getUser()->getGender()){ case 0: $gender = 'M'; break; case 1: $gender = 'MME'; break; case 2: $gender = 'MLLE'; break; default: $gender = 'N/A'; break; } // Init a Payment $payment = [ 'amount' => $totalAmount, 'currency' => $paymentMode->getCurrency(), 'action' => $paymentMode->getRang(), 'mode' => 'CPT', 'contractNumber' => $paymentMode->getCertificate() ]; // Init Order $orderDate = new \DateTime(); $order = [ 'ref' => $transaction->getUser()->getLastName().$transaction->getUser()->getId(), 'amount' => $totalAmount, 'currency' => $paymentMode->getCurrency(), 'date' => $orderDate->format('d/m/Y H:i') ]; // Int Return URL $returnURL = $urls['urlConfirmation']; $cancelURL = $urls['urlCancel']; // Init Bayer $buyer = [ 'title' => $gender, 'lastName' => $transaction->getUser()->getLastName(), 'firstName' => $transaction->getUser()->getFirstName(), 'email' => $transaction->getUser()->getEmail(), 'billingAddress' => $transaction->getUser()->getAddress(), 'zipCode' => $transaction->getUser()->getZipCode(), 'cityName' => $transaction->getUser()->getCity(), 'walletId' => 'W_RG1', 'mobilePhone' => $phoneFormat, 'customerId' => 'RG1', 'legalStatus' => 1, 'legalDocument' => 5, 'birthDate' => $birthDate->format('d/m/Y'), 'fingerprintID' => $transaction->getUser()->getLastName().$transaction->getUser()->getId(), ]; // Int Merchant $merchantName = $paymentMode->getCompanyName(); /**** create an instance of PaylineSDK ****/ $paylineSDK = new PaylineSDK($paymentMode->getShopKey(), $paymentMode->getIdentifier(), '','','','', $environment); /**** Configure a webservice parameters ****/ $doWebPaymentRequest = array_merge_recursive([ 'payment' => $payment, 'order' => $order, 'returnURL' => $returnURL, 'cancelURL' => $cancelURL, 'merchantName' => $merchantName, 'buyer' => $buyer ]); /**** Call a webservice and recuperate a response ****/ return $paylineSDK->doWebPayment($doWebPaymentRequest); } /** * @param Transaction $_transaction * @param string $_paylineToken * @return mixed * @throws \Exception */ public function confirmationPayment(Transaction $_transaction, string $_paylineToken){ // Init Variables $urlsRedirection = $this->redirectURL($_transaction); $paymentMode = $_transaction->getPaymentMode(); $environment = ($paymentMode->getContext() == 'TEST')? 'HOMO' : 'PROD'; /**** create an instance of PaylineSDK ****/ $paylineSDK = new PaylineSDK($paymentMode->getShopKey(), $paymentMode->getIdentifier(), '','','','', $environment); // first, we get the payment details $paymentResult = $paylineSDK->getWebPaymentDetails(['token'=>$_paylineToken]); // Add private data foreach ($paymentResult['privateDataList']['privateData'] as $key=>$value){ $paylineSDK->addPrivateData(['key' => $key, 'value' => $value]); } // Update Transaction $_transaction->setInformation($paymentResult["result"]["shortMessage"].': '.$paymentResult["result"]["longMessage"]); switch ($paymentResult['result']['code']){ case '00000' : $redirect = $urlsRedirection['urlSuccess']; $_transaction->setStatus(TransactionStatusAction::TRANSACTION_PAID); $_transaction->setTotalPayer($paymentResult["payment"]["amount"]); $_transaction->setIdTransactionExternal($paymentResult["transaction"]["id"]); $_transaction->setDateTimePayment(\DateTime::createFromFormat('d/m/Y H:i', $paymentResult["transaction"]["date"])); break; default : $redirect = $urlsRedirection['urlCancel']; $_transaction->setIdTransactionExternal($paymentResult["result"]["code"]); $_transaction->setStatus(TransactionStatusAction::TRANSACTION_ERROR); break; } $this->transactionManager->update($_transaction); return $redirect; } }