|
@@ -11,6 +11,7 @@ use App\Models\Payment;
|
|
|
use App\Models\ShopProduct;
|
|
|
use App\Models\User;
|
|
|
use App\Models\Coupon;
|
|
|
+use App\Traits\Coupon as CouponTrait;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
use Illuminate\Support\Facades\Redirect;
|
|
@@ -28,6 +29,8 @@ use PayPalHttp\HttpException;
|
|
|
*/
|
|
|
class PayPalExtension extends AbstractExtension
|
|
|
{
|
|
|
+ use CouponTrait;
|
|
|
+
|
|
|
public static function getConfig(): array
|
|
|
{
|
|
|
return [
|
|
@@ -36,23 +39,27 @@ class PayPalExtension extends AbstractExtension
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- static function PaypalPay(Request $request): void
|
|
|
+ public function PaypalPay(Request $request): void
|
|
|
{
|
|
|
/** @var User $user */
|
|
|
$user = Auth::user();
|
|
|
$shopProduct = ShopProduct::findOrFail($request->shopProduct);
|
|
|
$discount = PartnerDiscount::getDiscount();
|
|
|
- $discountPrice = $request->get('discountPrice');
|
|
|
+ $couponCode = $request->input('couponCode');
|
|
|
+ $isValidCoupon = $this->validateCoupon($request->user(), $couponCode, $request->shopProduct);
|
|
|
|
|
|
- dd($discountPrice);
|
|
|
+ if (is_string($shopProduct->price)) {
|
|
|
+ $shopProduct->price = floatval($shopProduct->price);
|
|
|
+ }
|
|
|
|
|
|
- // Partner Discount.
|
|
|
- $price = $shopProduct->price - ($shopProduct->price * $discount / 100);
|
|
|
+ $price = $shopProduct->price;
|
|
|
|
|
|
- // Coupon Discount.
|
|
|
- // if ($discountPrice) {
|
|
|
- // $price = $price - ($price * floatval($coupon_percentage) / 100);
|
|
|
- // }
|
|
|
+ if ($isValidCoupon->getStatusCode() == 200) {
|
|
|
+ $price = $this->calcDiscount($price, $isValidCoupon->getData());
|
|
|
+ }
|
|
|
+
|
|
|
+ // Partner Discount.
|
|
|
+ $price = $price - ($price * $discount / 100);
|
|
|
|
|
|
// create a new payment
|
|
|
$payment = Payment::create([
|
|
@@ -78,17 +85,15 @@ class PayPalExtension extends AbstractExtension
|
|
|
[
|
|
|
"reference_id" => uniqid(),
|
|
|
"description" => $shopProduct->display . ($discount ? (" (" . __('Discount') . " " . $discount . '%)') : ""),
|
|
|
- "amount" => [
|
|
|
- "value" => $shopProduct->getTotalPrice(),
|
|
|
+ "amount" => [
|
|
|
+ "value" => $shopProduct->getTotalPrice(),
|
|
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
|
|
'breakdown' => [
|
|
|
- 'item_total' =>
|
|
|
- [
|
|
|
+ 'item_total' => [
|
|
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
|
|
'value' => number_format($price, 2),
|
|
|
],
|
|
|
- 'tax_total' =>
|
|
|
- [
|
|
|
+ 'tax_total' => [
|
|
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
|
|
'value' => $shopProduct->getTaxValue(),
|
|
|
]
|
|
@@ -98,7 +103,7 @@ class PayPalExtension extends AbstractExtension
|
|
|
],
|
|
|
"application_context" => [
|
|
|
"cancel_url" => route('payment.Cancel'),
|
|
|
- "return_url" => route('payment.PayPalSuccess', ['payment' => $payment->id, 'couponCode' => $coupon_code]),
|
|
|
+ "return_url" => route('payment.PayPalSuccess', ['payment' => $payment->id, 'couponCode' => $couponCode]),
|
|
|
'brand_name' => config('app.name', 'CtrlPanel.GG'),
|
|
|
'shipping_preference' => 'NO_SHIPPING'
|
|
|
]
|
|
@@ -138,7 +143,7 @@ class PayPalExtension extends AbstractExtension
|
|
|
|
|
|
$payment = Payment::findOrFail($laravelRequest->payment);
|
|
|
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
|
|
- $coupon_code = $laravelRequest->input('couponCode');
|
|
|
+ $coupon_code = $laravelRequest->input('couponCode');
|
|
|
|
|
|
$request = new OrdersCaptureRequest($laravelRequest->input('token'));
|
|
|
$request->prefer('return=representation');
|
|
@@ -153,11 +158,11 @@ class PayPalExtension extends AbstractExtension
|
|
|
'payment_id' => $response->result->id,
|
|
|
]);
|
|
|
|
|
|
- // increase the use of the coupon when the payment is confirmed.
|
|
|
- if ($coupon_code) {
|
|
|
- $coupon = new Coupon;
|
|
|
- $coupon->incrementUses($coupon_code);
|
|
|
- }
|
|
|
+ // increase the use of the coupon when the payment is confirmed.
|
|
|
+ if ($coupon_code) {
|
|
|
+ $coupon = new Coupon;
|
|
|
+ $coupon->incrementUses($coupon_code);
|
|
|
+ }
|
|
|
|
|
|
event(new UserUpdateCreditsEvent($user));
|
|
|
event(new PaymentEvent($user, $payment, $shopProduct));
|