src/Service/StripeAPI.php line 99

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\CustomInterface\PaymentInterface;
  4. use App\Enum\QuoteStatusEnum;
  5. use App\Traits\SentryNotifyTrait;
  6. use Evo\Infrastructure\MappingORM\Quote;
  7. use Stripe;
  8. use Stripe\Charge;
  9. use Stripe\Checkout\Session;
  10. use Stripe\Customer;
  11. use Stripe\Dispute;
  12. use Stripe\Exception\ApiErrorException;
  13. use Stripe\PaymentIntent;
  14. use Stripe\Refund;
  15. use Stripe\StripeClient;
  16. use Stripe\Webhook;
  17. use Symfony\Component\HttpFoundation\Request;
  18. class StripeAPI implements PaymentInterface
  19. {
  20.     use SentryNotifyTrait;
  21.     private $secretKey;
  22.     private $webhookSecret;
  23.     public function __construct($secretKey$webhookSecret)
  24.     {
  25.         $this->secretKey $secretKey;
  26.         $this->webhookSecret $webhookSecret;
  27.         Stripe\Stripe::setApiKey($this->secretKey);
  28.     }
  29.     /**
  30.      * @throws ApiErrorException
  31.      */
  32.     public function createQuoteCheckout(Quote $quote)
  33.     {
  34.         if (QuoteStatusEnum::SENT === $quote->getStatus()) {
  35.             Session::create([
  36.                 'success_url' => 'https://example.com/success',
  37.                 'cancel_url' => 'https://example.com/cancel',
  38.                 'payment_method_types' => ['card'],
  39.                 'line_items' => [
  40.                     [
  41.                         'name' => 'T-shirt',
  42.                         'description' => 'Comfortable cotton t-shirt',
  43.                         'amount' => 1500,
  44.                         'currency' => 'usd',
  45.                         'quantity' => 2,
  46.                     ],
  47.                 ],
  48.             ]);
  49.         }
  50.     }
  51.     public function createCustomer(string $emailstring $name)
  52.     {
  53.         try {
  54.             return Customer::create(['email' => $email'name' => $name]);
  55.         } catch (\Exception $e) {
  56.             echo $e->getMessage();
  57.             return null;
  58.         }
  59.     }
  60.     public function getCustomer(string $email)
  61.     {
  62.         try {
  63.             return Customer::all(['email' => $email]);
  64.         } catch (\Exception $e) {
  65.             echo $e->getMessage();
  66.             return null;
  67.         }
  68.     }
  69.     public function attachPaymentMethodToCustomer(string $paymentMethodIDstring $customerID)
  70.     {
  71.         try {
  72.             $stripe = new StripeClient($this->secretKey);
  73.             return $stripe->paymentMethods->attach(
  74.                 $paymentMethodID,
  75.                 ['customer' => $customerID]
  76.             );
  77.         } catch (\Exception $e) {
  78.             echo $e->getMessage();
  79.             return null;
  80.         }
  81.     }
  82.     /**
  83.      * @return Stripe\Event|null
  84.      */
  85.     public function webhookCheck($bodyRequest $request)
  86.     {
  87.         $signature_header $request->server->get('HTTP_STRIPE_SIGNATURE');
  88.         try {
  89.             return Webhook::constructEvent(
  90.                 $body,
  91.                 $signature_header,
  92.                 $this->webhookSecret
  93.             );
  94.         } catch (\Exception $e) {
  95.             echo $e->getMessage();
  96.             return null;
  97.         }
  98.     }
  99.     public function getCharge(string $id)
  100.     {
  101.         try {
  102.             return Charge::retrieve($id);
  103.         } catch (\Exception $e) {
  104.             $this->captureSentryException($e);
  105.         }
  106.         return null;
  107.     }
  108.     public function createRefund(string $paymentIDfloat $amount)
  109.     {
  110.         try {
  111.             return Refund::create([
  112.                 'payment_intent' => $paymentID,
  113.                 'amount' => $amount 100,
  114.             ]);
  115.         } catch (\Exception $e) {
  116.             $this->captureSentryException($e);
  117.         }
  118.         return null;
  119.     }
  120.     public function createPaymentIntent(array $params)
  121.     {
  122.         try {
  123.             return PaymentIntent::create($params);
  124.         } catch (\Exception $e) {
  125.             $this->sendSentryMessage(sprintf('[STRIPE API]:: payment intent error %s '$e->getMessage()));
  126.             $this->captureSentryException($e);
  127.         }
  128.     }
  129.     public function getDispute(string $id)
  130.     {
  131.         try {
  132.             return Dispute::retrieve($id);
  133.         } catch (\Exception $e) {
  134.         }
  135.         return null;
  136.     }
  137.     public function getPayment(string $id)
  138.     {
  139.         try {
  140.             return PaymentIntent::retrieve($id);
  141.         } catch (\Exception $e) {
  142.             $this->captureSentryException($e);
  143.         }
  144.     }
  145. }