src/Service/PaytweakService.php line 494

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Billing;
  4. use App\Entity\BillingItem;
  5. use App\Entity\Client;
  6. use App\Entity\Company;
  7. use App\Model\Invoice;
  8. use App\Traits\LoggerTrait;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  11. use Symfony\Component\HttpFoundation\File\UploadedFile;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Component\Mime\Email;
  15. use Symfony\Contracts\HttpClient\HttpClientInterface;
  16. class PaytweakService
  17. {
  18.     const NOTICE_PAYMENT 'payment';
  19.     const NOTICE_MANDATE_SIGNED 'MANDATE_SIGNED';
  20.     const PAYMENT_STATUS_INCOMPLETE 0;
  21.     const PAYMENT_STATUS_AUTHORIZED_REFUSED 2;
  22.     const PAYMENT_STATUS_REFUSED_FRAUD 3;
  23.     const PAYMENT_STATUS_REFUSED 4;
  24.     const PAYMENT_STATUS_AUTHORIZED 5;
  25.     const PAYMENT_STATUS_WAITING 51;
  26.     const PAYMENT_STATUS_CANCEL 7;
  27.     const PAYMENT_STATUS_CANCEL_WAITING 71;
  28.     const PAYMENT_STATUS_REFUNDED 8;
  29.     const PAYMENT_STATUS_REFUND_WAITING 81;
  30.     const PAYMENT_STATUS_PAYMENT_EXECUTED 9;
  31.     const PAYMENT_STATUS_PAYMENT_UNCERTAINED 91;
  32.     const PAYMENT_STATUS_PAYMENT_REFUSED 93;
  33.     const BASE_URL 'https://api.paytweak.cloud/v1/';
  34.     const CODE_NOT_FOUND "404";
  35.     const CODE_OK "OK";
  36.     private $client;
  37.     private $parameterBag;
  38.     use LoggerTrait;
  39.     public function __construct(HttpClientInterface $clientParameterBagInterface $parameterBag)
  40.     {
  41.         $this->client $client;
  42.         $this->parameterBag $parameterBag;
  43.     }
  44.     /**
  45.      * @param Company $company
  46.      * @return mixed
  47.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  48.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  49.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  50.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  51.      */
  52.     public function hello(Company $company)
  53.     {
  54.         $apiKey $this->parameterBag->get('API_KEY_PAYTWEAK_FRESSIA');
  55.         $url $this->parameterBag->get('API_PAYTWEAK_URL');
  56.         if ($company->getName() === 'SILVER BEAUTÉ') {
  57.             $apiKey $this->parameterBag->get('API_KEY_PAYTWEAK_SILVER');
  58.             $url $this->parameterBag->get('API_PAYTWEAK_URL_SILVER');
  59.         }
  60.         $response $this->client->request(
  61.             Request::METHOD_GET,
  62.             $url 'hello/',
  63.             [
  64.                 "body" => '',
  65.                 "headers" => [
  66.                     'Paytweak-API-KEY' => $apiKey,
  67.                 ]
  68.             ]
  69.         );
  70.         return json_decode($response->getContent(), true);
  71.     }
  72.     /**
  73.      * @param $token
  74.      * @param Company $company
  75.      * @return mixed
  76.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  77.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  78.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  79.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  80.      */
  81.     public function verify($tokenCompany $company)
  82.     {
  83.         $private_key $this->parameterBag->get('PRIVATE_PAYTWEAK_FRESSIA'); // fressia
  84.         $url $this->parameterBag->get('API_PAYTWEAK_URL');
  85.         if ($company->getName() === 'SILVER BEAUTÉ') {
  86.             //$private_key = '389f01e98d02992c';
  87.             $private_key $this->parameterBag->get('PRIVATE_PAYTWEAK_SILVER');
  88.             $url $this->parameterBag->get('API_PAYTWEAK_URL_SILVER');
  89.         }
  90.         $user_token base64_encode(trim($token) . $private_key);
  91.         $response $this->client->request(
  92.             Request::METHOD_GET,
  93.             $url 'verify/',
  94.             [
  95.                 "body" => '',
  96.                 "headers" => [
  97.                     'Paytweak-USER-TOKEN' => $user_token,
  98.                 ]
  99.             ]
  100.         );
  101.         return json_decode($response->getContent(), true);
  102.     }
  103.     /**
  104.      * Problem when no address
  105.      * @param Invoice $invoice
  106.      * @param Company $company
  107.      * @return string
  108.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  109.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  110.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  111.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  112.      */
  113.     public function createBilling(Invoice $invoiceCompany $company)
  114.     {
  115.         $token $this->hello($company);
  116.         $url $this->parameterBag->get('API_PAYTWEAK_URL');
  117.         if ($company->getName() === 'SILVER BEAUTÉ') {
  118.             $url $this->parameterBag->get('API_PAYTWEAK_URL_SILVER');
  119.         }
  120.         $workToken $this->verify($token['Paytweak-Security-Token'], $company);
  121.         $response $this->client->request(
  122.             Request::METHOD_POST,
  123.             $url 'invoices/',
  124.             [
  125.                 "body" => $invoice->toArray(),
  126.                 "headers" => [
  127.                     'Paytweak-Token' => $workToken['Paytweak-Work-Token'],
  128.                 ]
  129.             ]
  130.         );
  131.         $statusCode $response->getStatusCode();
  132.         // $statusCode = 200
  133.         $content $response->getContent();
  134.         //dump($content);die;
  135.         $content $response->toArray();
  136.         return $content;
  137.     }
  138.     /**
  139.      * Problem when no address
  140.      * @param Invoice $invoice
  141.      * @param Company $company
  142.      * @return string
  143.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  144.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  145.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  146.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  147.      */
  148.     public function reSend(Company $company$orderId$email)
  149.     {
  150.         $token $this->hello($company);
  151.         $url $this->parameterBag->get('API_PAYTWEAK_URL');
  152.         if ($company->getName() === 'SILVER BEAUTÉ') {
  153.             $url $this->parameterBag->get('API_PAYTWEAK_URL_SILVER');
  154.         }
  155.         $workToken $this->verify($token['Paytweak-Security-Token'], $company);
  156.         $response $this->client->request(
  157.             Request::METHOD_POST,
  158.             $url 'emails/',
  159.             [
  160.                 "body" => [
  161.                     'order_id' => $orderId,
  162.                     'email' => $email,
  163.                     'transactional' => 1,
  164.                     'template' => $company->getId() === '0JS6HBV' 'GVB1WBK'
  165.                 ],
  166.                 "headers" => [
  167.                     'Paytweak-Token' => $workToken['Paytweak-Work-Token'],
  168.                 ]
  169.             ]
  170.         );
  171.         $statusCode $response->getStatusCode();
  172.         // $statusCode = 200
  173.         $content $response->toArray();
  174.         return $content;
  175.     }
  176.     /**
  177.      * Problem when no address
  178.      * @param Invoice $invoice
  179.      * @param Company $company
  180.      * @return string
  181.      * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  182.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  183.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  184.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  185.      */
  186.     public function getLink(Company $company$orderId)
  187.     {
  188.         $token $this->hello($company);
  189.         $url $this->parameterBag->get('API_PAYTWEAK_URL');
  190.         if ($company->getName() === 'SILVER BEAUTÉ') {
  191.             $url $this->parameterBag->get('API_PAYTWEAK_URL_SILVER');
  192.         }
  193.         $workToken $this->verify($token['Paytweak-Security-Token'], $company);
  194.         $response $this->client->request(
  195.             Request::METHOD_GET,
  196.             $url 'links',
  197.             [
  198.                 "query" => [
  199.                     'order_id' => $orderId,
  200.                 ],
  201.                 "headers" => [
  202.                     'Paytweak-Token' => $workToken['Paytweak-Work-Token'],
  203.                 ]
  204.             ]
  205.         );
  206.         $statusCode $response->getStatusCode();
  207.         // $statusCode = 200
  208.         $content $response->toArray();
  209.         return $content;
  210.     }
  211.     public function listBilling(Company $company)
  212.     {
  213.         $token $this->hello($company);
  214.         $url $this->parameterBag->get('API_PAYTWEAK_URL');
  215.         if ($company->getName() === 'SILVER BEAUTÉ') {
  216.             $url $this->parameterBag->get('API_PAYTWEAK_URL_SILVER');
  217.         }
  218.         $workToken $this->verify($token['Paytweak-Security-Token'], $company);
  219.         $response $this->client->request(
  220.             Request::METHOD_GET,
  221.             $url 'invoices/',
  222.             [
  223.                 'query' => [
  224.                     'year' => date('Y'),
  225.                     'month' => date('m')
  226.                 ],
  227.                 "headers" => [
  228.                     'Paytweak-Token' => $workToken['Paytweak-Work-Token'],
  229.                 ]
  230.             ]
  231.         );
  232.         $statusCode $response->getStatusCode();
  233.         // $statusCode = 200
  234.         $content $response->getContent();
  235.         $content $response->toArray();
  236.         return $content;
  237.     }
  238.     public function getBilling($orderIdCompany $company)
  239.     {
  240.         $token $this->hello($company);
  241.         $url $this->parameterBag->get('API_PAYTWEAK_URL');
  242.         if ($company->getName() === 'SILVER BEAUTÉ') {
  243.             $url $this->parameterBag->get('API_PAYTWEAK_URL_SILVER');
  244.         }
  245.         $workToken $this->verify($token['Paytweak-Security-Token'], $company);
  246.         $response $this->client->request(
  247.             Request::METHOD_GET,
  248.             $url 'links/',
  249.             [
  250.                 'query' => [
  251.                     'order_id' => $orderId,
  252.                 ],
  253.                 "headers" => [
  254.                     'Paytweak-Token' => $workToken['Paytweak-Work-Token'],
  255.                 ]
  256.             ]
  257.         );
  258.         $statusCode $response->getStatusCode();
  259.         // $statusCode = 200
  260.         $content $response->getContent();
  261.         $content $response->toArray();
  262.         return $content;
  263.     }
  264.     public function createInvoiceFromClient(Client $clientBilling $billing)
  265.     {
  266.         $invoice = new Invoice();
  267.         $cityFill = !!$client->getCity();
  268.         $invoice->setCity($client->getCity());
  269.         if (!$cityFill) {
  270.             $invoice->setCity($billing->getClient()->getSite()->getAddress()->getCity());
  271.         }
  272.         if ($client->getBillingInfos() && $client->getBillingInfos()->getEmail()) {
  273.             $invoice->setEmail($client->getBillingInfos()->getEmail());
  274.         } else {
  275.             if ($client->getEmail()) {
  276.                 $invoice->setEmail($client->getEmail());
  277.             }
  278.             //            else {
  279.             //                $invoice->setEmail($client->getSite()->getContactBilling()->getEmail());
  280.             //            }
  281.         }
  282.         $invoice->setFirstname($client->getFirstname());
  283.         $invoice->setLastname($client->getLastname());
  284.         $invoice->setZip($client->getZipcode());
  285.         if (!$cityFill) {
  286.             $invoice->setZip($billing->getClient()->getSite()->getAddress()->getZipcode());
  287.         }
  288.         $invoice->setCur('EUR');
  289.         $invoice->setFirstname($client->getFirstname());
  290.         $invoice->setLastname($client->getLastname());
  291.         $genre 'M';
  292.         if ($client->getGender() === 'Madame') {
  293.             $genre 'Me';
  294.         }
  295.         $invoice->setGender($genre);
  296.         $invoice->setPhone($client->getPhone());
  297.         $invoice->setAddressCustomer($client->getAddressTuteur());
  298.         if (!$cityFill) {
  299.             $invoice->setAddressCustomer($billing->getClient()->getSite()->getAddress()->getStreet());
  300.         }
  301.         $invoice->setCustomerRef($client->getIdentifier());
  302.         $invoice->setReference($billing->getCode());
  303.         $invoice->setAmount($billing->getTtc());
  304.         $invoice->setDate($billing->getDateBilling()->format('d/m/Y'));
  305.         $invoice->setFreetext($billing->getComment());
  306.         //$invoice->setDiscountName('Remise');
  307.         //$invoice->setDiscountAmount($billing->get);
  308.         $items = [];
  309.         /** @var BillingItem $item */
  310.         foreach ($billing->getBillingItems() as $item) {
  311.             $rdv $billing->getRDV();
  312.             $date $item->getDateRDV() ? $item->getDateRDV()->format('d/m/Y') : '';
  313.             $items[] = [
  314.                 'ref' => $item->getId(),
  315.                 'desc' => $date ' - ' $item->getLibelleProduct(),
  316.                 'quantity' => $item->getQuantity(),
  317.                 'vat' => '20',
  318.                 'price' => $item->getPrice(),
  319.                 'discount' => $item->getDiscount()
  320.             ];
  321.         }
  322.         $invoice->setItems(json_encode($items));
  323.         $invoice->setEmailTemplate('B7EV3PN');
  324.         if($client->getBillingInfos() && $client->getBillingInfos()->getPayMode() === Client\BillingInfos::PAY_MODE_SEPA){
  325.             $invoice->setEmailTemplate('HURNEAN');
  326.         }
  327.         if ($_SERVER['SERVER_NAME'] === 'emmaconnect.fr') {
  328.             $invoice->setTemplate('classic_SilverBeaute_discount');
  329.             $invoice->setSend(true);
  330.         }
  331.         return $invoice;
  332.     }
  333.     /**
  334.      * method hello
  335.      * @return mixed
  336.      */
  337.     public function helloReservation()
  338.     {
  339.         $apiKey $this->parameterBag->get('API_KEY_PAYTWEAK');
  340.         $url $this->parameterBag->get('API_PAYTWEAK_URL_DEV');
  341.         $response $this->client->request(
  342.             Request::METHOD_GET,
  343.             $url 'hello/',
  344.             [
  345.                 "body" => '',
  346.                 "headers" => [
  347.                     'Paytweak-API-KEY' => $apiKey,
  348.                 ]
  349.             ]
  350.         );
  351.         return json_decode($response->getContent(), true);
  352.     }
  353.     /**
  354.      * method verify
  355.      * @param mixed $token
  356.      *
  357.      * @return mixed
  358.      */
  359.     public function verifyReservation($token)
  360.     {
  361.         $private_key $this->parameterBag->get('PRIVATE_PAYTWEAK');
  362.         $url $this->parameterBag->get('API_PAYTWEAK_URL_DEV');
  363.         $user_token base64_encode(trim($token) . $private_key);
  364.         $response $this->client->request(
  365.             Request::METHOD_GET,
  366.             $url 'verify/',
  367.             [
  368.                 "body" => '',
  369.                 "headers" => [
  370.                     'Paytweak-USER-TOKEN' => $user_token,
  371.                 ]
  372.             ]
  373.         );
  374.         return json_decode($response->getContent(), true);
  375.     }
  376.     /**
  377.      * create payment link
  378.      * @return array
  379.      */
  380.     public function createLink($orderIdPaytweak null$scenario null)
  381.     {
  382.         $token $this->helloReservation();
  383.         $url $this->parameterBag->get('API_PAYTWEAK_URL_DEV');
  384.         $workToken $this->verifyReservation($token['Paytweak-Security-Token']);
  385.         $requestBody = [
  386.             'operation' => 'TOK',
  387.             'amount' => '0'
  388.         ];
  389.         if ($scenario) {
  390.             $requestBody['scenario'] = $scenario;
  391.         }
  392.         if ($orderIdPaytweak !== null) {
  393.             //$requestBody['order_id'] = $orderIdPaytweak;
  394.         }
  395.         try {
  396.             $response $this->client->request(
  397.                 Request::METHOD_POST,
  398.                 $url 'links',
  399.                 [
  400.                     "body" => $requestBody,
  401.                     "headers" => [
  402.                         'Paytweak-Token' => $workToken['Paytweak-Work-Token'],
  403.                     ]
  404.                 ]
  405.             );
  406.             $this->logger->error(json_encode($response->getContent()));
  407.         } catch (\Exception $exception) {
  408.             $this->logger->error($exception->getMessage());
  409.         }
  410.         return json_decode($response->getContent(), true);
  411.     }
  412.     public function createLinkScenario(Company $company,
  413.                                                $orderIdPaytweak null,
  414.                                                $scenario null,
  415.                                                $amount,
  416.                                                $email,
  417.                                                $prenom,
  418.                                                $nom,
  419.                                                $gender,
  420.                                                $order_id,
  421.     $rum null,
  422.     $template
  423.     )
  424.     {
  425.         $token $this->hello($company);
  426.         $url $this->parameterBag->get('API_PAYTWEAK_URL_DEV'); ////api.silver-beaute.paytweak.com/v1/
  427.         $workToken $this->verify($token['Paytweak-Security-Token'], $company);
  428.         $requestBody = [
  429.             'operation' => 'TOK',
  430.             'amount' => $amount,
  431.             'email' => $email,
  432.             'firstname' => $prenom,
  433.             'lastname' => $nom,
  434.             'mandat' => $rum,
  435.             'lng' => 'fr_FR',
  436.             'cur' => 'EUR',
  437.             'template' => $template,
  438.             'gender' => $gender,
  439.             'order_id' => $order_id
  440.         ];
  441.         if ($scenario) {
  442.             $requestBody['scenario'] = $scenario;
  443.         }
  444.         if ($orderIdPaytweak !== null) {
  445.             //$requestBody['order_id'] = $orderIdPaytweak;
  446.         }
  447.         try {
  448.             $response $this->client->request(
  449.                 Request::METHOD_POST,
  450.                 $url 'emails',
  451.                 [
  452.                     "body" => $requestBody,
  453.                     "headers" => [
  454.                         'Paytweak-Token' => $workToken['Paytweak-Work-Token'],
  455.                     ]
  456.                 ]
  457.             );
  458.             $this->logger->error(json_encode($response->getContent()));
  459.         } catch (\Exception $exception) {
  460.             $this->logger->error($exception->getMessage());
  461.         }
  462.         return json_decode($response->getContent(), true);
  463.     }
  464.     /**
  465.      * Get payment information
  466.      * @return array
  467.      */
  468.     public function checkPayment($linkId)
  469.     {
  470.         $token $this->helloReservation();
  471.         $url $this->parameterBag->get('API_PAYTWEAK_URL_DEV');
  472.         $workToken $this->verifyReservation($token['Paytweak-Security-Token']);
  473.         $response $this->client->request(
  474.             Request::METHOD_GET,
  475.             $url 'links/' $linkId,
  476.             [
  477.                 "body" => [],
  478.                 "headers" => [
  479.                     'Paytweak-Token' => $workToken['Paytweak-Work-Token'],
  480.                 ]
  481.             ]
  482.         );
  483.         return json_decode($response->getContent(), true);
  484.     }
  485.     /**
  486.      * Trigger payment
  487.      * @return array
  488.      */
  489.     public function triggerPayment(string $linkfloat $amount$orderCode)
  490.     {
  491.         $token $this->helloReservation();
  492.         $url $this->parameterBag->get('API_PAYTWEAK_URL_DEV');
  493.         $amount number_format($amount2'.''');
  494.         $workToken $this->verifyReservation($token['Paytweak-Security-Token']);
  495.         preg_match("/[^\/]+$/"$link$matches);
  496.         $response $this->client->request(
  497.             Request::METHOD_POST,
  498.             $url 'm2m/token',
  499.             [
  500.                 "body" => [
  501.                     'order_id' => $orderCode,
  502.                     'amount' => "{$amount}",
  503.                     'token' => $matches[0],
  504.                 ],
  505.                 "headers" => [
  506.                     'Paytweak-Token' => $workToken['Paytweak-Work-Token'],
  507.                 ]
  508.             ]
  509.         );
  510.         return json_decode($response->getContent(), true);
  511.     }
  512.     /**
  513.      * Generate password
  514.      * @param int $length
  515.      *
  516.      * @return string
  517.      */
  518.     private function passwordGenerate($length): string
  519.     {
  520.         $data '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  521.         return substr(str_shuffle($data), 0$length);
  522.     }
  523. }