feat: Migrate other payment gateways to new payment pattern

This commit is contained in:
IceToast 2023-05-28 02:27:39 +02:00 committed by IceToast
parent 4873992c48
commit 48e5cae9a1
4 changed files with 19 additions and 105 deletions

View file

@ -2,7 +2,7 @@
namespace App\Extensions\PaymentGateways\Mollie;
use App\Helpers\AbstractExtension;
use App\Classes\AbstractExtension;
use App\Events\PaymentEvent;
use App\Events\UserUpdateCreditsEvent;
use App\Models\PartnerDiscount;
@ -37,43 +37,10 @@ class MollieExtension extends AbstractExtension
];
}
public function pay(Request $request): void
public static function getRedirectUrl(Payment $payment, ShopProduct $shopProduct, string $totalPriceString): string
{
$url = 'https://api.mollie.com/v2/payments';
$settings = new MollieSettings();
$user = Auth::user();
$shopProduct = ShopProduct::findOrFail($request->shopProduct);
$discount = PartnerDiscount::getDiscount();
$couponCode = $request->input('couponCode');
$isValidCoupon = $this->validateCoupon($request->user(), $couponCode, $request->shopProduct);
$price = $shopProduct->price;
// Coupon Discount.
if ($isValidCoupon->getStatusCode() == 200) {
$price = $this->calcDiscount($price, $isValidCoupon->getData());
}
// Partner Discount.
$price = $price - ($price * $discount / 100);
$price = number_format($price, 2, thousands_separator: '');
// create a new payment
$payment = Payment::create([
'user_id' => $user->id,
'payment_id' => null,
'payment_method' => 'mollie',
'type' => $shopProduct->type,
'status' => 'open',
'amount' => $shopProduct->quantity,
'price' => $price,
'tax_value' => $shopProduct->getTaxValue(),
'tax_percent' => $shopProduct->getTaxPercent(),
'total_price' => $shopProduct->getTotalPrice(),
'currency_code' => $shopProduct->currency_code,
'shop_item_product_id' => $shopProduct->id,
]);
try {
$response = Http::withHeaders([
'Content-Type' => 'application/json',
@ -81,10 +48,10 @@ class MollieExtension extends AbstractExtension
])->post($url, [
'amount' => [
'currency' => $shopProduct->currency_code,
'value' => $price,
'value' => $totalPriceString,
],
'description' => "Order #{$payment->id} - " . $shopProduct->name,
'redirectUrl' => route('payment.MollieSuccess', ['couponCode' => $couponCode]),
'redirectUrl' => route('payment.MollieSuccess'),
'cancelUrl' => route('payment.Cancel'),
'webhookUrl' => url('/extensions/payment/MollieWebhook'),
'metadata' => [
@ -94,24 +61,13 @@ class MollieExtension extends AbstractExtension
if ($response->status() != 201) {
Log::error('Mollie Payment: ' . $response->body());
$payment->delete();
Redirect::route('store.index')->with('error', __('Payment failed'))->send();
return;
throw new Exception('Payment failed');
}
$payment->update([
'payment_id' => $response->json()['id'],
]);
Redirect::away($response->json()['_links']['checkout']['href'])->send();
return;
return $response->json()['_links']['checkout']['href'];
} catch (Exception $ex) {
Log::error('Mollie Payment: ' . $ex->getMessage());
$payment->delete();
Redirect::route('store.index')->with('error', __('Payment failed'))->send();
return;
throw new Exception('Payment failed');
}
}
@ -122,7 +78,7 @@ class MollieExtension extends AbstractExtension
$couponCode = $request->input('couponCode');
if ($couponCode) {
event(new CouponUsedEvent(new Coupon, $couponCode));
event(new CouponUsedEvent($couponCode));
}
Redirect::route('home')->with('success', 'Your payment is being processed')->send();

View file

@ -156,7 +156,6 @@ class PayPalExtension extends PaymentExtension
static function getPayPalClient(): PayPalHttpClient
{
error_log(config('app.env'));
$environment = config('app.env') == 'local'
? new SandboxEnvironment(self::getPaypalClientId(), self::getPaypalClientSecret())

View file

@ -2,7 +2,7 @@
namespace App\Extensions\PaymentGateways\Stripe;
use App\Helpers\AbstractExtension;
use App\Classes\AbstractExtension;
use App\Events\PaymentEvent;
use App\Events\CouponUsedEvent;
use App\Events\UserUpdateCreditsEvent;
@ -36,52 +36,14 @@ class StripeExtension extends AbstractExtension
];
}
/**
* @param Request $request
* @param ShopProduct $shopProduct
*/
public function StripePay(Request $request)
public static function getRedirectUrl(Payment $payment, ShopProduct $shopProduct, string $totalPriceString): string
{
$user = Auth::user();
$shopProduct = ShopProduct::findOrFail($request->shopProduct);
$price = $shopProduct->price;
// check if the price is valid for stripe
if (!self::checkPriceAmount($shopProduct->getTotalPrice(), strtoupper($shopProduct->currency_code), 'stripe')) {
Redirect::route('home')->with('error', __('The product you chose can\'t be purchased with this payment method. The total amount is too small. Please buy a bigger amount or try a different payment method.'))->send();
return;
// check if the total price is valid for stripe
$totalPriceNumber = floatval($totalPriceString);
if (!self::checkPriceAmount($totalPriceNumber, strtoupper($shopProduct->currency_code), 'stripe')) {
throw new Exception('Invalid price amount');
}
$discount = PartnerDiscount::getDiscount();
$couponCode = $request->input('couponCode');
$isValidCoupon = $this->validateCoupon($request->user(), $couponCode, $request->shopProduct);
// Coupon Discount.
if ($isValidCoupon->getStatusCode() == 200) {
$price = $this->calcDiscount($price, $isValidCoupon->getData());
}
// Partner Discount.
$price = $price - ($price * $discount / 100);
$price = number_format($price, 2);
// create payment
$payment = Payment::create([
'user_id' => $user->id,
'payment_id' => null,
'payment_method' => 'stripe',
'type' => $shopProduct->type,
'status' => 'open',
'amount' => $shopProduct->quantity,
'price' => $price,
'tax_value' => $shopProduct->getTaxValue(),
'total_price' => $shopProduct->getTotalPrice(),
'tax_percent' => $shopProduct->getTaxPercent(),
'currency_code' => $shopProduct->currency_code,
'shop_item_product_id' => $shopProduct->id,
]);
$stripeClient = self::getStripeClient();
$request = $stripeClient->checkout->sessions->create([
'line_items' => [
@ -89,10 +51,10 @@ class StripeExtension extends AbstractExtension
'price_data' => [
'currency' => $shopProduct->currency_code,
'product_data' => [
'name' => $shopProduct->display . ($discount ? (' (' . __('Discount') . ' ' . $discount . '%)') : ''),
'name' => $shopProduct->display,
'description' => $shopProduct->description,
],
'unit_amount_decimal' => $price,
'unit_amount_decimal' => $totalPriceString,
],
'quantity' => 1,
],
@ -110,7 +72,7 @@ class StripeExtension extends AbstractExtension
],
'mode' => 'payment',
'success_url' => route('payment.StripeSuccess', ['payment' => $payment->id, 'couponCode' => $couponCode]) . '&session_id={CHECKOUT_SESSION_ID}',
'success_url' => route('payment.StripeSuccess', ['payment' => $payment->id]) . '&session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => route('payment.Cancel'),
'payment_intent_data' => [
'metadata' => [
@ -119,7 +81,7 @@ class StripeExtension extends AbstractExtension
],
]);
Redirect::to($request->url)->send();
return $request->url;
}
/**
@ -302,7 +264,7 @@ class StripeExtension extends AbstractExtension
* @return bool
* @description check if the amount is higher than the minimum amount for the stripe gateway
*/
public static function checkPriceAmount($amount, $currencyCode, $payment_method)
public static function checkPriceAmount(float $amount, string $currencyCode, string $payment_method)
{
$minimums = [
"USD" => [

View file

@ -184,9 +184,6 @@ class PaymentController extends Controller
return redirect()->away($redirectUrl);
}
/**
* @param Request $request
*/