src/Service/Billing/BillingAtolService.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Service\Billing;
  3. use App\Entity\Payment\Subscription;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use DateTime;
  7. use App\Entity\Centre;
  8. use App\Entity\Factures;
  9. use App\Entity\Plan;
  10. use App\Entity\PaymentMethod;
  11. use App\Entity\Audio;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. const URL_CLIENT_BILLING_ATOL "https://myaudio.vosfactures.fr/clients.json";
  14. const URL_INVOICE_BILLING_ATOL "https://myaudio.vosfactures.fr/invoices.json";
  15. class BillingAtolService
  16. {
  17.     private $entityManager;
  18.     private $publicFunction;
  19.     private $apiTokenBilling;
  20.     private $productIdMap = [
  21.         'yearly'  => '17325102572',  // Annual subscription price ID : Abonnement My Audio Pro Atol annuel pendant 12 mois (900€/an)
  22.     ];
  23.     public function __construct(EntityManagerInterface $entityManager,string $apiTokenBilling)
  24.     {
  25.         $this->entityManager $entityManager;
  26.         $this->apiToken $apiTokenBilling;
  27.     }
  28.     public function createClient(Centre $centre, array $data)
  29.     {
  30.         $curl curl_init();
  31.         curl_setopt_array($curl, array(
  32.         CURLOPT_URL => URL_CLIENT_BILLING_ATOL,
  33.         CURLOPT_RETURNTRANSFER => true,
  34.         CURLOPT_ENCODING => '',
  35.         CURLOPT_MAXREDIRS => 10,
  36.         CURLOPT_TIMEOUT => 0,
  37.         CURLOPT_FOLLOWLOCATION => true,
  38.         CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  39.         CURLOPT_CUSTOMREQUEST => 'POST',
  40.         CURLOPT_POSTFIELDS =>'{ 
  41.             "api_token": "'.$this->apiToken.'",
  42.             "client": { 
  43.                 "name":"'.$centre->getName().'", 
  44.                 "city": "'.$centre->getCity().'", 
  45.                 "country": "France", 
  46.                 "email": "'.$centre->getIdGerant()->getMail().'", 
  47.                 "post_code": "'.$centre->getPostale().'", 
  48.                 "street":"'.$centre->getAddress().'",
  49.                 "phone": "'.$centre->getPhone().'"
  50.             }
  51.         }',
  52.         CURLOPT_HTTPHEADER => array(
  53.             'Accept: application/json',
  54.             'Content-Type: application/json'
  55.         ),
  56.         ));
  57.         $response1 curl_exec($curl);
  58.         $rep =  json_decode($response1);
  59.         //dd($data);
  60.         $factureId $rep->{'id'};
  61.         $centre->setFactureId($rep->{'id'});
  62.         $this->entityManager->flush();
  63.         if($data['plan'] == "Multi-Audios")
  64.         {
  65.            
  66.                 $additionalProductId $data['period'] == "month" 
  67.                 $this->productIdMap['monthly'] : 
  68.                 $this->productIdMap['yearly'];
  69.                 
  70.             $productData = [
  71.                 [
  72.                     "product_id" => $additionalProductId,
  73.                     "quantity" => 1,
  74.                 ]
  75.             ];
  76.         }
  77.             else {
  78.               
  79.                         $additionalProductId $data['period'] == "month" 
  80.                         $this->productIdMap['monthly'] : 
  81.                         $this->productIdMap['yearly'];
  82.                         
  83.                     $productData = [
  84.                         [
  85.                             "product_id" => $additionalProductId,
  86.                             "quantity" => 1,
  87.                         ]
  88.                     ]; 
  89.                    
  90.             }
  91.          //  dd($productData);
  92.         $this->createBilling($centre,intval($centre->getFactureId()),$productData);
  93.     }
  94.     // for special new annual billing from 900 to 1000
  95.     public function createClientUpdatedYearly(Centre $centre, array $data)
  96.     {
  97.         $curl curl_init();
  98.         curl_setopt_array($curl, array(
  99.         CURLOPT_URL => URL_CLIENT_BILLING_ATOL,
  100.         CURLOPT_RETURNTRANSFER => true,
  101.         CURLOPT_ENCODING => '',
  102.         CURLOPT_MAXREDIRS => 10,
  103.         CURLOPT_TIMEOUT => 0,
  104.         CURLOPT_FOLLOWLOCATION => true,
  105.         CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  106.         CURLOPT_CUSTOMREQUEST => 'POST',
  107.         CURLOPT_POSTFIELDS =>'{ 
  108.             "api_token": "'.$this->apiToken.'",
  109.             "client": { 
  110.                 "name":"'.$centre->getName().'", 
  111.                 "city": "'.$centre->getCity().'", 
  112.                 "country": "France", 
  113.                 "email": "'.$centre->getIdGerant()->getMail().'", 
  114.                 "post_code": "'.$centre->getPostale().'", 
  115.                 "street":"'.$centre->getAddress().'",
  116.                 "phone": "'.$centre->getPhone().'"
  117.             }
  118.         }',
  119.         CURLOPT_HTTPHEADER => array(
  120.             'Accept: application/json',
  121.             'Content-Type: application/json'
  122.         ),
  123.         ));
  124.         $response1 curl_exec($curl);
  125.         $rep =  json_decode($response1);
  126.         //dd($data);
  127.         $factureId $rep->{'id'};
  128.         $centre->setFactureId($rep->{'id'});
  129.         $this->entityManager->flush();
  130.         if($data['plan'] == "Multi-Audios")
  131.         {
  132.            
  133.                 $additionalProductId =  $this->productIdMap['nextYearYearly'] ;
  134.             $productData = [
  135.                 [
  136.                     "product_id" => $additionalProductId,
  137.                     "quantity" => 1,
  138.                 ]
  139.             ];
  140.             if ($data['quantity'] > 1) {
  141.                 $additionalProductId =  $this->productIdMap['additionalAudioYearly'];
  142.                
  143.                 $quantity $data['quantity'] - 1;
  144.             
  145.                 $additionalProduct = [
  146.                     "product_id" => $additionalProductId,
  147.                     "quantity" => $quantity,
  148.                 ];
  149.                 $productData array_merge($productData, [$additionalProduct]);
  150.             }
  151.         }
  152.             else {
  153.               
  154.                         $additionalProductId $this->productIdMap['nextYearYearly'] ;
  155.                         
  156.                     $productData = [
  157.                         [
  158.                             "product_id" => $additionalProductId,
  159.                             "quantity" => 1,
  160.                         ]
  161.                     ];
  162.         
  163.                     if ($data['quantity'] > 1) {
  164.                         $additionalProductId $this->productIdMap['additionnalCenterYearly'];
  165.                        
  166.                         $quantity $data['quantity'] - 1;
  167.                     
  168.                         $additionalProduct = [
  169.                             "product_id" => $additionalProductId,
  170.                             "quantity" => $quantity,
  171.                         ];
  172.                         $productData array_merge($productData, [$additionalProduct]);
  173.                     }
  174.                     
  175.                    
  176.             }
  177.          //  dd($productData);
  178.         $this->createBilling($centre,intval($centre->getFactureId()),$productData);
  179.     }
  180.     function createInvoicePayload($apiTokenBilling$departmentId$clientId$productData) {
  181.         $positions = [];
  182.     
  183.         foreach ($productData as $product) {
  184.             $position = [
  185.                 "product_id" => $product['product_id'],
  186.                 "quantity" => $product['quantity']
  187.             ];
  188.     
  189.             if (isset($product['discount'])) {
  190.                 $position["discount"] = $product['discount'];
  191.             }
  192.     
  193.             $positions[] = $position;
  194.         }
  195.    // dd($positions);
  196.         $payload = [
  197.             "api_token" => $apiTokenBilling,
  198.             "invoice" => [
  199.                 "department_id" => $departmentId,
  200.                 "client_id" => $clientId,
  201.                 "test" => false,
  202.                 "discount_kind" => "amount",
  203.                 "show_discount" => false,
  204.                 "positions" => $positions,
  205.             ]
  206.         ];
  207.     
  208.         return json_encode($payload);
  209.     }
  210.     
  211.    
  212.     public function createBilling(Centre $center,int $client, array $data)
  213.     {
  214.     $apiTokenBilling $this->apiToken
  215.     //$departmentId = $data['departmentId'];      
  216.     $departmentId 937362;      
  217.     $clientId $client
  218.     /*$productData = [
  219.         ["product_id" => 102520343, "quantity" => 1, "discount" => $reduction],
  220.     ];*/
  221.     
  222.     $productData $data;
  223.     
  224.     $payload $this->createInvoicePayload($apiTokenBilling$departmentId$clientId$productData);
  225.     
  226.     // cURL setup
  227.     $curl curl_init();
  228.     curl_setopt_array($curl, array(
  229.         CURLOPT_URL => URL_INVOICE_BILLING_ATOL,
  230.         CURLOPT_RETURNTRANSFER => true,
  231.         CURLOPT_ENCODING => '',
  232.         CURLOPT_MAXREDIRS => 10,
  233.         CURLOPT_TIMEOUT => 0,
  234.         CURLOPT_FOLLOWLOCATION => true,
  235.         CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  236.         CURLOPT_CUSTOMREQUEST => 'POST',
  237.         CURLOPT_POSTFIELDS => $payload,
  238.         CURLOPT_HTTPHEADER => array(
  239.             'Accept: application/json',
  240.             'Content-Type: application/json'
  241.         ),
  242.     ));
  243.     
  244.     $fact curl_exec($curl);
  245.     $fact =  json_decode($fact);
  246.     $target = new DateTime('now');
  247.    // dd($fact);
  248.     $factId $fact->{'id'};
  249.     $tokenId $fact->{'token'};
  250.     $numeroFact $fact->{'number'};
  251.     $montantHT $fact->{'price_net'};
  252.     $montantTTC $fact->{'price_gross'};
  253.     $dateFact $target;
  254.      //POST FACTURE 
  255.      $facture = new Factures();
  256.      $facture->setId(intval($factId));
  257.      $facture->setToken($tokenId);
  258.      $facture->setDate($dateFact);
  259.      $facture->setCentre($center);        
  260.      $facture->setNumero($numeroFact);
  261.      $facture->setPrixHT($montantHT);
  262.      $facture->setPrixTTC($montantTTC);
  263.      $this->entityManager->persist($facture);
  264.      $this->entityManager->flush();
  265.      
  266.      // save the billing
  267.     $this->downloadInvoice($facture);
  268.     return [
  269.      "message" => "Facture create succesefully",
  270.      "status" => 200,
  271.     ];
  272.     }
  273.     public function downloadInvoice(Factures $invoice)
  274.     {
  275.             // cURL setup
  276. $curl curl_init();
  277. curl_setopt_array($curl, array(
  278.     CURLOPT_URL => "https://myaudio.vosfactures.fr/invoices/".$invoice->getId().".pdf?api_token=".$this->apiToken,
  279.     CURLOPT_RETURNTRANSFER => true,  
  280.     CURLOPT_FOLLOWLOCATION => true,  
  281.     CURLOPT_HTTPGET => true          
  282. ));
  283. $response curl_exec($curl);
  284. if (curl_errno($curl)) {
  285.     echo 'cURL error: ' curl_error($curl);
  286. }
  287. curl_close($curl);
  288. /*$filename = str_replace('/', '-', $invoice->getNumero());
  289. $outputPath = 'assets/partner/facture' . $filename . '.pdf';*/
  290. // Your specified output path
  291. $outputPath 'assets/partner/facture' $invoice->getToken() . '.pdf';
  292. // Check if the directory exists, if not create it
  293. if (!file_exists(dirname($outputPath))) {
  294.     mkdir(dirname($outputPath), 0755true);
  295. }
  296. // Save the PDF file
  297. file_put_contents($outputPath$response);
  298.     }
  299. }