<?php
namespace App\Service;
use App\CustomInterface\PaymentInterface;
use App\Enum\QuoteStatusEnum;
use App\Traits\SentryNotifyTrait;
use Evo\Infrastructure\MappingORM\Quote;
use Stripe;
use Stripe\Charge;
use Stripe\Checkout\Session;
use Stripe\Customer;
use Stripe\Dispute;
use Stripe\Exception\ApiErrorException;
use Stripe\PaymentIntent;
use Stripe\Refund;
use Stripe\StripeClient;
use Stripe\Webhook;
use Symfony\Component\HttpFoundation\Request;
class StripeAPI implements PaymentInterface
{
use SentryNotifyTrait;
private $secretKey;
private $webhookSecret;
public function __construct($secretKey, $webhookSecret)
{
$this->secretKey = $secretKey;
$this->webhookSecret = $webhookSecret;
Stripe\Stripe::setApiKey($this->secretKey);
}
/**
* @throws ApiErrorException
*/
public function createQuoteCheckout(Quote $quote)
{
if (QuoteStatusEnum::SENT === $quote->getStatus()) {
Session::create([
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
'payment_method_types' => ['card'],
'line_items' => [
[
'name' => 'T-shirt',
'description' => 'Comfortable cotton t-shirt',
'amount' => 1500,
'currency' => 'usd',
'quantity' => 2,
],
],
]);
}
}
public function createCustomer(string $email, string $name)
{
try {
return Customer::create(['email' => $email, 'name' => $name]);
} catch (\Exception $e) {
echo $e->getMessage();
return null;
}
}
public function getCustomer(string $email)
{
try {
return Customer::all(['email' => $email]);
} catch (\Exception $e) {
echo $e->getMessage();
return null;
}
}
public function attachPaymentMethodToCustomer(string $paymentMethodID, string $customerID)
{
try {
$stripe = new StripeClient($this->secretKey);
return $stripe->paymentMethods->attach(
$paymentMethodID,
['customer' => $customerID]
);
} catch (\Exception $e) {
echo $e->getMessage();
return null;
}
}
/**
* @return Stripe\Event|null
*/
public function webhookCheck($body, Request $request)
{
$signature_header = $request->server->get('HTTP_STRIPE_SIGNATURE');
try {
return Webhook::constructEvent(
$body,
$signature_header,
$this->webhookSecret
);
} catch (\Exception $e) {
echo $e->getMessage();
return null;
}
}
public function getCharge(string $id)
{
try {
return Charge::retrieve($id);
} catch (\Exception $e) {
$this->captureSentryException($e);
}
return null;
}
public function createRefund(string $paymentID, float $amount)
{
try {
return Refund::create([
'payment_intent' => $paymentID,
'amount' => $amount * 100,
]);
} catch (\Exception $e) {
$this->captureSentryException($e);
}
return null;
}
public function createPaymentIntent(array $params)
{
try {
return PaymentIntent::create($params);
} catch (\Exception $e) {
$this->sendSentryMessage(sprintf('[STRIPE API]:: payment intent error %s ', $e->getMessage()));
$this->captureSentryException($e);
}
}
public function getDispute(string $id)
{
try {
return Dispute::retrieve($id);
} catch (\Exception $e) {
}
return null;
}
public function getPayment(string $id)
{
try {
return PaymentIntent::retrieve($id);
} catch (\Exception $e) {
$this->captureSentryException($e);
}
}
}