ソースを参照

fix: 🐛 PartnerDiscount expected auth login -> not possible on webhooks

IceToast 2 年 前
コミット
6216892594

+ 3 - 5
app/Extensions/PaymentGateways/PayPal/index.php

@@ -4,13 +4,10 @@ use App\Events\PaymentEvent;
 use App\Events\UserUpdateCreditsEvent;
 use App\Events\UserUpdateCreditsEvent;
 use App\Models\PartnerDiscount;
 use App\Models\PartnerDiscount;
 use App\Models\Payment;
 use App\Models\Payment;
-use App\Models\Product;
 use App\Models\ShopProduct;
 use App\Models\ShopProduct;
 use App\Models\User;
 use App\Models\User;
-use Illuminate\Http\RedirectResponse;
 use Illuminate\Http\Request;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Auth;
-use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Redirect;
 use Illuminate\Support\Facades\Redirect;
 use PayPalCheckoutSdk\Core\PayPalHttpClient;
 use PayPalCheckoutSdk\Core\PayPalHttpClient;
 use PayPalCheckoutSdk\Core\ProductionEnvironment;
 use PayPalCheckoutSdk\Core\ProductionEnvironment;
@@ -30,6 +27,7 @@ function PaypalPay(Request $request)
     /** @var User $user */
     /** @var User $user */
     $user = Auth::user();
     $user = Auth::user();
     $shopProduct = ShopProduct::findOrFail($request->shopProduct);
     $shopProduct = ShopProduct::findOrFail($request->shopProduct);
+    $discount = PartnerDiscount::getDiscount();
 
 
     // create a new payment
     // create a new payment
     $payment = Payment::create([
     $payment = Payment::create([
@@ -39,7 +37,7 @@ function PaypalPay(Request $request)
         'type' => $shopProduct->type,
         'type' => $shopProduct->type,
         'status' => 'open',
         'status' => 'open',
         'amount' => $shopProduct->quantity,
         'amount' => $shopProduct->quantity,
-        'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
+        'price' => $shopProduct->price - ($shopProduct->price * $discount / 100),
         'tax_value' => $shopProduct->getTaxValue(),
         'tax_value' => $shopProduct->getTaxValue(),
         'tax_percent' => $shopProduct->getTaxPercent(),
         'tax_percent' => $shopProduct->getTaxPercent(),
         'total_price' => $shopProduct->getTotalPrice(),
         'total_price' => $shopProduct->getTotalPrice(),
@@ -54,7 +52,7 @@ function PaypalPay(Request $request)
         "purchase_units" => [
         "purchase_units" => [
             [
             [
                 "reference_id" => uniqid(),
                 "reference_id" => uniqid(),
-                "description" => $shopProduct->display . (PartnerDiscount::getDiscount() ? (" (" . __('Discount') . " " . PartnerDiscount::getDiscount() . '%)') : ""),
+                "description" => $shopProduct->display . ($discount ? (" (" . __('Discount') . " " . $discount . '%)') : ""),
                 "amount"       => [
                 "amount"       => [
                     "value"         => $shopProduct->getTotalPrice(),
                     "value"         => $shopProduct->getTotalPrice(),
                     'currency_code' => strtoupper($shopProduct->currency_code),
                     'currency_code' => strtoupper($shopProduct->currency_code),

+ 17 - 9
app/Extensions/PaymentGateways/Stripe/index.php

@@ -4,14 +4,11 @@ use App\Events\PaymentEvent;
 use App\Events\UserUpdateCreditsEvent;
 use App\Events\UserUpdateCreditsEvent;
 use App\Models\PartnerDiscount;
 use App\Models\PartnerDiscount;
 use App\Models\Payment;
 use App\Models\Payment;
-use App\Models\Product;
 use App\Models\ShopProduct;
 use App\Models\ShopProduct;
 use App\Models\User;
 use App\Models\User;
 use App\Notifications\ConfirmPaymentNotification;
 use App\Notifications\ConfirmPaymentNotification;
-use Illuminate\Http\RedirectResponse;
 use Illuminate\Http\Request;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Auth;
-use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Redirect;
 use Illuminate\Support\Facades\Redirect;
 use Stripe\Exception\SignatureVerificationException;
 use Stripe\Exception\SignatureVerificationException;
 use Stripe\Stripe;
 use Stripe\Stripe;
@@ -19,6 +16,7 @@ use Stripe\StripeClient;
 
 
 
 
 
 
+
 /**
 /**
  * @param  Request  $request
  * @param  Request  $request
  * @param  ShopProduct  $shopProduct
  * @param  ShopProduct  $shopProduct
@@ -34,6 +32,8 @@ function StripePay(Request $request)
         return;
         return;
     }
     }
 
 
+    $discount = PartnerDiscount::getDiscount();
+
 
 
     // create payment
     // create payment
     $payment = Payment::create([
     $payment = Payment::create([
@@ -43,7 +43,7 @@ function StripePay(Request $request)
         'type' => $shopProduct->type,
         'type' => $shopProduct->type,
         'status' => 'open',
         'status' => 'open',
         'amount' => $shopProduct->quantity,
         'amount' => $shopProduct->quantity,
-        'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
+        'price' => $shopProduct->price - ($shopProduct->price * $discount / 100),
         'tax_value' => $shopProduct->getTaxValue(),
         'tax_value' => $shopProduct->getTaxValue(),
         'total_price' => $shopProduct->getTotalPrice(),
         'total_price' => $shopProduct->getTotalPrice(),
         'tax_percent' => $shopProduct->getTaxPercent(),
         'tax_percent' => $shopProduct->getTaxPercent(),
@@ -58,7 +58,7 @@ function StripePay(Request $request)
                 'price_data' => [
                 'price_data' => [
                     'currency' => $shopProduct->currency_code,
                     'currency' => $shopProduct->currency_code,
                     'product_data' => [
                     'product_data' => [
-                        'name' => $shopProduct->display . (PartnerDiscount::getDiscount() ? (' (' . __('Discount') . ' ' . PartnerDiscount::getDiscount() . '%)') : ''),
+                        'name' => $shopProduct->display . ($discount ? (' (' . __('Discount') . ' ' . $discount . '%)') : ''),
                         'description' => $shopProduct->description,
                         'description' => $shopProduct->description,
                     ],
                     ],
                     'unit_amount_decimal' => round($shopProduct->getPriceAfterDiscount() * 100, 2),
                     'unit_amount_decimal' => round($shopProduct->getPriceAfterDiscount() * 100, 2),
@@ -82,6 +82,11 @@ function StripePay(Request $request)
         'payment_method_types' => str_getcsv(config('SETTINGS::PAYMENTS:STRIPE:METHODS')),
         'payment_method_types' => str_getcsv(config('SETTINGS::PAYMENTS:STRIPE:METHODS')),
         'success_url' => route('payment.StripeSuccess', ['payment' => $payment->id]) . '&session_id={CHECKOUT_SESSION_ID}',
         'success_url' => route('payment.StripeSuccess', ['payment' => $payment->id]) . '&session_id={CHECKOUT_SESSION_ID}',
         'cancel_url' => route('payment.Cancel'),
         'cancel_url' => route('payment.Cancel'),
+        'payment_intent_data' => [
+            'metadata' => [
+                'payment_id' => $payment->id,
+            ],
+        ],
     ]);
     ]);
 
 
     Redirect::to($request->url)->send();
     Redirect::to($request->url)->send();
@@ -98,6 +103,8 @@ function StripeSuccess(Request $request)
     $shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
     $shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
 
 
 
 
+    Redirect::route('home')->with('success', 'Please wait for success')->send();
+
     $stripeClient = getStripeClient();
     $stripeClient = getStripeClient();
     try {
     try {
         //get stripe data
         //get stripe data
@@ -162,16 +169,17 @@ function StripeSuccess(Request $request)
 function handleStripePaymentSuccessHook($paymentIntent)
 function handleStripePaymentSuccessHook($paymentIntent)
 {
 {
     try {
     try {
-        // Get payment db entry
-        $payment = Payment::where('payment_id', $paymentIntent->id)->first();
+        $payment = Payment::where('id', $paymentIntent->metadata->payment_id)->with('user')->first();
         $user = User::where('id', $payment->user_id)->first();
         $user = User::where('id', $payment->user_id)->first();
         $shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
         $shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
 
 
         if ($paymentIntent->status == 'succeeded' && $payment->status == 'processing') {
         if ($paymentIntent->status == 'succeeded' && $payment->status == 'processing') {
 
 
-
             //update payment db entry status
             //update payment db entry status
-            $payment->update(['status' => 'paid']);
+            $payment->update([
+                'payment_id' => $payment->payment_id ?? $paymentIntent->id,
+                'status' => 'paid'
+            ]);
 
 
             //payment notification
             //payment notification
             $user->notify(new ConfirmPaymentNotification($payment));
             $user->notify(new ConfirmPaymentNotification($payment));

+ 3 - 3
app/Models/PartnerDiscount.php

@@ -18,11 +18,11 @@ class PartnerDiscount extends Model
         'referral_system_commission',
         'referral_system_commission',
     ];
     ];
 
 
-    public static function getDiscount()
+    public static function getDiscount(int $user_id = null)
     {
     {
-        if ($partnerDiscount = PartnerDiscount::where('user_id', Auth::user()->id)->first()) {
+        if ($partnerDiscount = PartnerDiscount::where('user_id', $user_id ?? Auth::user()->id)->first()) {
             return $partnerDiscount->partner_discount;
             return $partnerDiscount->partner_discount;
-        } elseif ($ref_user = DB::table('user_referrals')->where('registered_user_id', '=', Auth::user()->id)->first()) {
+        } elseif ($ref_user = DB::table('user_referrals')->where('registered_user_id', '=', $user_id ?? Auth::user()->id)->first()) {
             if ($partnerDiscount = PartnerDiscount::where('user_id', $ref_user->referral_id)->first()) {
             if ($partnerDiscount = PartnerDiscount::where('user_id', $ref_user->referral_id)->first()) {
                 return $partnerDiscount->registered_user_discount;
                 return $partnerDiscount->registered_user_discount;
             }
             }