src/Service/Billing/BillingService.php line 43

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