fix: 🐛 Payment Creation and use paymentEvent instead of manual invoice creation

This commit is contained in:
IceToast 2023-01-14 23:34:54 +01:00
parent 1f79a19943
commit 59f8e8c696

View file

@ -4,12 +4,14 @@ 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\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\DB;
use Illuminate\Support\Facades\Redirect;
use PayPalCheckoutSdk\Core\PayPalHttpClient; use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\ProductionEnvironment; use PayPalCheckoutSdk\Core\ProductionEnvironment;
use PayPalCheckoutSdk\Core\SandboxEnvironment; use PayPalCheckoutSdk\Core\SandboxEnvironment;
@ -22,12 +24,29 @@ use PayPalHttp\HttpException;
/** /**
* @param Request $request * @param Request $request
* @param ShopProduct $shopProduct * @param ShopProduct $shopProduct
* @return RedirectResponse
*/ */
function PaypalPay(Request $request) function PaypalPay(Request $request)
{ {
/** @var User $user */
$user = Auth::user();
$shopProduct = ShopProduct::findOrFail($request->shopProduct); $shopProduct = ShopProduct::findOrFail($request->shopProduct);
// create a new payment
$payment = Payment::create([
'user_id' => $user->id,
'payment_id' => null,
'payment_method' => 'paypal',
'type' => $shopProduct->type,
'status' => 'open',
'amount' => $shopProduct->quantity,
'price' => $shopProduct->price - ($shopProduct->price*PartnerDiscount::getDiscount()/100),
'tax_value' => $shopProduct->getTaxValue(),
'tax_percent' => $shopProduct->getTaxPercent(),
'total_price' => $shopProduct->getTotalPrice(),
'currency_code' => $shopProduct->currency_code,
'shop_item_product_id' => $shopProduct->id,
]);
$request = new OrdersCreateRequest(); $request = new OrdersCreateRequest();
$request->prefer('return=representation'); $request->prefer('return=representation');
$request->body = [ $request->body = [
@ -56,21 +75,27 @@ function PaypalPay(Request $request)
], ],
"application_context" => [ "application_context" => [
"cancel_url" => route('payment.Cancel'), "cancel_url" => route('payment.Cancel'),
"return_url" => route('payment.PayPalSuccess'), "return_url" => route('payment.PayPalSuccess', ['payment' => $payment->id]),
'brand_name' => config('app.name', 'Laravel'), 'brand_name' => config('app.name', 'Laravel'),
'shipping_preference' => 'NO_SHIPPING' 'shipping_preference' => 'NO_SHIPPING'
] ]
]; ];
try { try {
// Call API with your client and get a response for your call // Call API with your client and get a response for your call
$response = getPayPalClient()->execute($request); $response = getPayPalClient()->execute($request);
return redirect()->away($response->result->links[1]->href);
// If call returns body in response, you can get the deserialized version from the result attribute of the response Redirect::away($response->result->links[1]->href)->send();
} catch (HttpException $ex) { } catch (HttpException $ex) {
echo $ex->statusCode; error_log($ex->statusCode);
dd(json_decode($ex->getMessage())); error_log($ex->getMessage());
$payment->delete();
return Redirect::route('payment.Cancel');
} }
} }
/** /**
@ -78,12 +103,13 @@ function PaypalPay(Request $request)
*/ */
function PaypalSuccess(Request $laravelRequest) function PaypalSuccess(Request $laravelRequest)
{ {
/** @var ShopProduct $shopProduct */
$shopProduct = ShopProduct::findOrFail($laravelRequest->input('product'));
/** @var User $user */
$user = Auth::user(); $user = Auth::user();
$payment = Payment::findOrFail($laravelRequest->payment);
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
$request = new OrdersCaptureRequest($laravelRequest->input('token')); $request = new OrdersCaptureRequest($laravelRequest->input('token'));
$request->prefer('return=representation'); $request->prefer('return=representation');
try { try {
// Call API with your client and get a response for your call // Call API with your client and get a response for your call
$response = getPayPalClient()->execute($request); $response = getPayPalClient()->execute($request);
@ -136,38 +162,41 @@ function PaypalSuccess(Request $laravelRequest)
} }
} }
//store payment
$payment = Payment::create([ //update payment
'user_id' => $user->id, $payment->update([
'payment_id' => $response->result->id, 'status' => 'success',
'payment_method' => 'paypal', 'payment_id' => $response->result->id,
'type' => $shopProduct->type,
'status' => 'paid',
'amount' => $shopProduct->quantity,
'price' => $shopProduct->price - ($shopProduct->price*PartnerDiscount::getDiscount()/100),
'tax_value' => $shopProduct->getTaxValue(),
'tax_percent' => $shopProduct->getTaxPercent(),
'total_price' => $shopProduct->getTotalPrice(),
'currency_code' => $shopProduct->currency_code,
'shop_item_product_id' => $shopProduct->id,
]); ]);
event(new UserUpdateCreditsEvent($user)); event(new UserUpdateCreditsEvent($user));
event(new PaymentEvent($payment)); event(new PaymentEvent($payment));
//redirect back to home error_log("Payment successfull");
return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
} // redirect to the payment success page with success message
// If call returns body in response, you can get the deserialized version from the result attribute of the response Redirect::route('home')->with('success', 'Payment successful')->send();
if (env('APP_ENV') == 'local') { } elseif(env('APP_ENV') == 'local') {
// If call returns body in response, you can get the deserialized version from the result attribute of the response
$payment->delete();
dd($response); dd($response);
} else { } else {
$payment->update([
'status' => 'failed',
'payment_id' => $response->result->id,
]);
abort(500); abort(500);
} }
} catch (HttpException $ex) { } catch (HttpException $ex) {
if (env('APP_ENV') == 'local') { if (env('APP_ENV') == 'local') {
echo $ex->statusCode; echo $ex->statusCode;
$payment->delete();
dd($ex->getMessage()); dd($ex->getMessage());
} else { } else {
$payment->update([
'status' => 'failed',
'payment_id' => $response->result->id,
]);
abort(422); abort(422);
} }
} }