fix: 🐛 Coupon frontend, calculation and submitting
This commit is contained in:
parent
2161464ee3
commit
d365b122db
1 changed files with 177 additions and 178 deletions
|
@ -22,104 +22,87 @@
|
||||||
<!-- END CONTENT HEADER -->
|
<!-- END CONTENT HEADER -->
|
||||||
|
|
||||||
<!-- MAIN CONTENT -->
|
<!-- MAIN CONTENT -->
|
||||||
<section class="content">
|
<section class="content" x-data="couponForm()">
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
<form
|
<form id="payment_form" action="{{ route('payment.pay') }}" method="POST">
|
||||||
id="payment_form"
|
|
||||||
action="{{ route('payment.pay') }}"
|
|
||||||
method="POST"
|
|
||||||
x-data="{
|
|
||||||
payment_method: '',
|
|
||||||
coupon_code: '',
|
|
||||||
clicked: false,
|
|
||||||
setCouponCode(event) {
|
|
||||||
this.coupon_code = event.target.value
|
|
||||||
}
|
|
||||||
}"
|
|
||||||
>
|
|
||||||
@csrf
|
@csrf
|
||||||
@method('post')
|
@method('post')
|
||||||
<div class="row d-flex justify-content-center flex-wrap">
|
<div class="row d-flex justify-content-center flex-wrap">
|
||||||
@if (!$productIsFree)
|
@if (!$productIsFree)
|
||||||
<div class="col-xl-4">
|
<div class="col-xl-4">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header">
|
|
||||||
<h4 class="mb-0">
|
|
||||||
<i class="fas fa-money-check-alt"></i>
|
|
||||||
Payment Methods
|
|
||||||
</h4>
|
|
||||||
</div>
|
|
||||||
<div class="card-body">
|
|
||||||
<input type="hidden" name="product_id" value="{{ $product->id }}">
|
|
||||||
<input type="hidden" name="payment_method" :value="payment_method"
|
|
||||||
x-model="payment_method">
|
|
||||||
<div class="row">
|
|
||||||
<div class="col-lg-12">
|
|
||||||
@foreach ($paymentGateways as $gateway)
|
|
||||||
<div class="row checkout-gateways">
|
|
||||||
<div class="col-12 d-flex justify-content-between">
|
|
||||||
<label class="form-check-label h4 checkout-gateway-label"
|
|
||||||
for="{{ $gateway->name }}">
|
|
||||||
<span class="mr-3">{{ $gateway->name }}</span>
|
|
||||||
</label>
|
|
||||||
<button class="btn btn-primary rounded" type="button"
|
|
||||||
name="payment-method" id="{{ $gateway->name }}"
|
|
||||||
value="{{ $gateway->name }}"
|
|
||||||
:class="payment_method === '{{ $gateway->name }}' ?
|
|
||||||
'active' : ''"
|
|
||||||
@click="payment_method = '{{ $gateway->name }}'; clicked = true;"
|
|
||||||
x-text="payment_method == '{{ $gateway->name }}' ? 'Selected' : 'Select'">Select</button>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
|
<div class="card-body">
|
||||||
|
<ul class="list-group ">
|
||||||
|
<li class="list-group-item">
|
||||||
|
<div class="row">
|
||||||
|
<input type="hidden" name="product_id" value="{{ $product->id }}">
|
||||||
|
<input type="hidden" name="payment_method" :value="payment_method"
|
||||||
|
x-model="payment_method">
|
||||||
|
<div class="col-lg-12">
|
||||||
|
<span class="h4">{{ __('Payment Methods') }}</span>
|
||||||
|
<div class="mt-2">
|
||||||
|
@foreach ($paymentGateways as $gateway)
|
||||||
|
<div
|
||||||
|
class="row checkout-gateways @if (!$loop->last) mb-2 @endif">
|
||||||
|
<div class="col-12 d-flex justify-content-between">
|
||||||
|
<label
|
||||||
|
class="form-check-label h5 checkout-gateway-label"
|
||||||
|
for="{{ $gateway->name }}">
|
||||||
|
<span class="mr-3">{{ $gateway->name }}</span>
|
||||||
|
</label>
|
||||||
|
<button class="btn btn-primary rounded" type="button"
|
||||||
|
name="payment-method" id="{{ $gateway->name }}"
|
||||||
|
value="{{ $gateway->name }}"
|
||||||
|
:class="payment_method === '{{ $gateway->name }}' ?
|
||||||
|
'active' : ''"
|
||||||
|
@click="payment_method = '{{ $gateway->name }}'; submitted = true;"
|
||||||
|
x-text="payment_method == '{{ $gateway->name }}' ? 'Selected' : 'Select'">Select</button>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endforeach
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@endforeach
|
</div>
|
||||||
</div>
|
</li>
|
||||||
</div>
|
|
||||||
|
<li class="list-group-item">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-12">
|
||||||
|
@if ($isCouponsEnabled)
|
||||||
|
<span class="h4">{{ __('Coupon') }}</span>
|
||||||
|
|
||||||
|
<div class="d-flex mt-2">
|
||||||
|
<input type="text" id="coupon_code" name="coupon_code"
|
||||||
|
value="{{ old('coupon_code') }}" :value="coupon_code"
|
||||||
|
class="form-control @error('coupon_code') is_invalid @enderror"
|
||||||
|
placeholder="{{ __('Enter your coupon here...') }}"
|
||||||
|
x-on:change.debounce="setCouponCode($event)"
|
||||||
|
x-model="coupon_code" />
|
||||||
|
<button type="button" id="send_coupon_code"
|
||||||
|
@click="checkCoupon()" class="btn btn-success ml-3"
|
||||||
|
:disabled="!coupon_code.length"
|
||||||
|
:class="!coupon_code.length ? 'disabled' : ''"
|
||||||
|
:value="coupon_code">
|
||||||
|
{{ __('Submit') }}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
@error('coupon_code')
|
||||||
|
<div class="text-danger">
|
||||||
|
{{ $message }}
|
||||||
|
</div>
|
||||||
|
@enderror
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@if ($isCouponsEnabled)
|
|
||||||
<div class="col-xl-4">
|
|
||||||
<div class="card">
|
|
||||||
<div class="card-header">
|
|
||||||
<h4 class="mb-0">
|
|
||||||
Coupon Code
|
|
||||||
</h4>
|
|
||||||
</div>
|
|
||||||
<div class="card-body">
|
|
||||||
<div class="d-flex">
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
id="coupon_code"
|
|
||||||
name="coupon_code"
|
|
||||||
value="{{ old('coupon_code') }}"
|
|
||||||
:value="coupon_code"
|
|
||||||
class="form-control @error('coupon_code') is_invalid @enderror"
|
|
||||||
placeholder="SUMMER"
|
|
||||||
x-on:change.debounce="setCouponCode($event)"
|
|
||||||
x-model="coupon_code"
|
|
||||||
/>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
id="send_coupon_code"
|
|
||||||
class="btn btn-success ml-3"
|
|
||||||
:disabled="!coupon_code.length"
|
|
||||||
:class="!coupon_code.length ? 'disabled' : ''"
|
|
||||||
:value="coupon_code"
|
|
||||||
>
|
|
||||||
{{ __('Submit') }}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
@error('coupon_code')
|
|
||||||
<div class="text-danger">
|
|
||||||
{{ $message }}
|
|
||||||
</div>
|
|
||||||
@enderror
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
@endif
|
|
||||||
@endif
|
@endif
|
||||||
<div class="col-xl-3">
|
<div class="col-xl-3">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
|
@ -173,7 +156,7 @@
|
||||||
<li class="d-flex justify-content-between">
|
<li class="d-flex justify-content-between">
|
||||||
<span class="text-muted d-inline-block">{{ __('Subtotal') }}</span>
|
<span class="text-muted d-inline-block">{{ __('Subtotal') }}</span>
|
||||||
<span class="text-muted d-inline-block">
|
<span class="text-muted d-inline-block">
|
||||||
{{ $product->formatToCurrency($discountedprice) }}</span>
|
{{ $product->formatToCurrency($product->price) }}</span>
|
||||||
</li>
|
</li>
|
||||||
<div class="d-flex justify-content-between">
|
<div class="d-flex justify-content-between">
|
||||||
<span class="text-muted d-inline-block">{{ __('Tax') }}
|
<span class="text-muted d-inline-block">{{ __('Tax') }}
|
||||||
|
@ -184,18 +167,19 @@
|
||||||
<span class="text-muted d-inline-block">
|
<span class="text-muted d-inline-block">
|
||||||
+ {{ $product->formatToCurrency($taxvalue) }}</span>
|
+ {{ $product->formatToCurrency($taxvalue) }}</span>
|
||||||
</div>
|
</div>
|
||||||
<div id="coupon_discount_details" class="d-flex justify-content-between" style="display: none !important;">
|
<div id="coupon_discount_details" class="d-flex justify-content-between"
|
||||||
<span class="text-muted d-inline-block">
|
style="display: none !important;">
|
||||||
{{ __('Coupon Discount') }}
|
<span class="text-muted d-inline-block">
|
||||||
</span>
|
{{ __('Coupon Discount') }}
|
||||||
<span id="coupon_discount_value" class="text-muted d-inline-block">
|
</span>
|
||||||
|
<span id="coupon_discount_value" class="text-muted d-inline-block">
|
||||||
|
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
@if ($discountpercent && $discountvalue)
|
@if ($discountpercent && $discountvalue)
|
||||||
<div class="d-flex justify-content-between">
|
<div class="d-flex justify-content-between">
|
||||||
<span class="text-muted d-inline-block">{{ __('Discount') }}
|
<span class="text-muted d-inline-block">{{ __('Discount') }}
|
||||||
({{ $discountpercent }}%):</span>
|
({{ $discountpercent }}%)</span>
|
||||||
<span
|
<span
|
||||||
class="text-muted d-inline-block">-{{ $product->formatToCurrency($discountvalue) }}</span>
|
class="text-muted d-inline-block">-{{ $product->formatToCurrency($discountvalue) }}</span>
|
||||||
</div>
|
</div>
|
||||||
|
@ -203,40 +187,36 @@
|
||||||
<hr class="text-white border-secondary">
|
<hr class="text-white border-secondary">
|
||||||
<div class="d-flex justify-content-between">
|
<div class="d-flex justify-content-between">
|
||||||
<span class="text-muted d-inline-block">{{ __('Total') }}</span>
|
<span class="text-muted d-inline-block">{{ __('Total') }}</span>
|
||||||
<input id="total_price_input" type="hidden" value="{{ $product->getTotalPrice() }}">
|
<input id="total_price_input" type="hidden" x-model="totalPrice">
|
||||||
<span
|
<span class="text-muted d-inline-block" x-text="totalPrice">
|
||||||
id="total_price"
|
|
||||||
class="text-muted d-inline-block"
|
|
||||||
>
|
|
||||||
{{ $product->formatToCurrency($total) }}
|
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<template x-if="payment_method">
|
<template x-if="payment_method">
|
||||||
<div class="d-flex justify-content-between">
|
<div class="d-flex justify-content-between">
|
||||||
<span class="text-muted d-inline-block">{{ __('Pay with') }}</span>
|
<span class="text-muted d-inline-block">{{ __('Pay with') }}</span>
|
||||||
<span class="text-muted d-inline-block" x-text="payment_method"></span>
|
<span class="text-muted d-inline-block"
|
||||||
|
x-text="payment_method"></span>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<button :disabled="(!payment_method || !clicked || coupon_code ? true : false) && {{ !$productIsFree }}"
|
<button
|
||||||
|
:disabled="(!payment_method || !clicked || coupon_code) &&
|
||||||
|
{{ !$productIsFree }}"
|
||||||
id="submit_form_button"
|
id="submit_form_button"
|
||||||
:class="(!payment_method || !clicked || coupon_code ? true : false) && {{ !$productIsFree }} ? 'disabled' : ''"
|
:class="(!payment_method || !clicked || coupon_code) &&
|
||||||
:x-text="coupon_code"
|
{{ !$productIsFree }} ? 'disabled' : ''"
|
||||||
class="btn btn-success float-right w-100">
|
:x-text="coupon_code" class="btn btn-success float-right w-100">
|
||||||
|
|
||||||
<i class="far fa-credit-card mr-2" @click="clicked == true"></i>
|
<i class="far fa-credit-card mr-2" @click="clicked == true"></i>
|
||||||
@if ($productIsFree)
|
@if ($productIsFree)
|
||||||
{{ __('Get for free') }}
|
{{ __('Get for free') }}
|
||||||
@else
|
@else
|
||||||
{{ __('Submit Payment') }}
|
{{ __('Submit Payment') }}
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
</button>
|
</button>
|
||||||
<script>
|
<script></script>
|
||||||
</script>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -248,81 +228,100 @@
|
||||||
<!-- END CONTENT -->
|
<!-- END CONTENT -->
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
$(document).ready(function() {
|
function couponForm() {
|
||||||
const productId = $("[name='product_id']").val()
|
console.log("{{ $discountedprice }}", " {{ $discountpercent }}", "{{ $discountvalue }}",
|
||||||
let hasCouponCodeValue = $('#coupon_code').val().trim() !== ''
|
" {{ $taxpercent }}", "{{ $taxvalue }}", "{{ $productIsFree }}", "{{ $total }}")
|
||||||
|
return {
|
||||||
|
// Get the product id from the url
|
||||||
|
productId: window.location.pathname.split('/').pop(),
|
||||||
|
payment_method: '',
|
||||||
|
coupon_code: '',
|
||||||
|
submitted: false,
|
||||||
|
totalPrice: {{ $discountedprice }},
|
||||||
|
|
||||||
$('#coupon_code').on('change', function(e) {
|
|
||||||
hasCouponCodeValue = e.target.value !== ''
|
|
||||||
})
|
|
||||||
|
|
||||||
function calcPriceWithCouponDiscount(couponValue, couponType) {
|
setCouponCode(event) {
|
||||||
let totalPrice = $('#total_price_input').val()
|
this.coupon_code = event.target.value
|
||||||
|
console.log(event.target.value)
|
||||||
|
},
|
||||||
|
|
||||||
if (typeof totalPrice == 'string') {
|
async checkCoupon() {
|
||||||
totalPrice = parseFloat(totalPrice)
|
console.log(this.coupon_code)
|
||||||
}
|
const response = await (fetch(
|
||||||
|
"{{ route('admin.coupon.redeem') }}", {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr(
|
||||||
|
'content')
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
couponCode: this.coupon_code,
|
||||||
|
productId: this.productId
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.then(response => response.json()).catch((error) => {
|
||||||
|
Swal.fire({
|
||||||
|
icon: 'error',
|
||||||
|
title: 'Oops...',
|
||||||
|
text: "{{ __('The coupon code you entered is invalid.') }}"
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
|
||||||
if (couponType === 'percentage') {
|
if (response.isValid && response.couponCode) {
|
||||||
totalPrice = totalPrice - (totalPrice * couponValue / 100)
|
Swal.fire({
|
||||||
$('#coupon_discount_value').text("- " + couponValue + "%")
|
icon: 'success',
|
||||||
} else if (couponType === 'amount') {
|
text: "{{ __('The coupon was successfully added to your purchase.') }}"
|
||||||
totalPrice = totalPrice - couponValue
|
|
||||||
$('#coupon_discount_value').text(totalPrice)
|
|
||||||
}
|
|
||||||
|
|
||||||
$('#total_price').text(totalPrice)
|
})
|
||||||
$('#total_price_input').val(totalPrice)
|
|
||||||
|
this.calcPriceWithCouponDiscount(response.couponValue, response
|
||||||
|
.couponType)
|
||||||
|
|
||||||
|
$('#submit_form_button').prop('disabled', false).removeClass(
|
||||||
|
'disabled')
|
||||||
|
$('#send_coupon_code').prop('disabled', true)
|
||||||
|
$('#coupon_discount_details').prop('disabled', false).show()
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Swal.fire({
|
||||||
|
icon: 'error',
|
||||||
|
title: 'Oops...',
|
||||||
|
text: "{{ __('The coupon code you entered is invalid.') }}"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
calcPriceWithCouponDiscount(couponValue, couponType) {
|
||||||
|
let newTotalPrice = this.totalPrice
|
||||||
|
|
||||||
|
if (couponType === 'percentage') {
|
||||||
|
newTotalPrice = newTotalPrice - (newTotalPrice * couponValue / 100)
|
||||||
|
$('#coupon_discount_value').text("- " + couponValue + "%")
|
||||||
|
} else if (couponType === 'amount') {
|
||||||
|
newTotalPrice = totanewTotalPricelPrice - couponValue
|
||||||
|
$('#coupon_discount_value').text(this.totalPrice)
|
||||||
|
}
|
||||||
|
|
||||||
|
// get language for formatting currency
|
||||||
|
const lang = "{{ app()->getLocale() }}"
|
||||||
|
console.log(lang)
|
||||||
|
// format totalPrice to currency
|
||||||
|
this.totalPrice = newTotalPrice.toLocaleString(lang, {
|
||||||
|
style: 'currency',
|
||||||
|
currency: "{{ $product->currency_code }}",
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(newTotalPrice)
|
||||||
|
console.log(this.totalPrice)
|
||||||
|
|
||||||
|
$('#total_price_input').val(this.totalPrice)
|
||||||
|
},
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkCoupon() {
|
|
||||||
const couponCode = $('#coupon_code').val()
|
|
||||||
|
|
||||||
$.ajax({
|
|
||||||
url: "{{ route('admin.coupon.redeem') }}",
|
|
||||||
method: 'POST',
|
|
||||||
data: { couponCode: couponCode, productId: productId },
|
|
||||||
success: function(response) {
|
|
||||||
if (response.isValid && response.couponCode) {
|
|
||||||
Swal.fire({
|
|
||||||
icon: 'success',
|
|
||||||
text: 'The coupon was successfully added to your purchase.',
|
|
||||||
}).then(function(isConfirmed) {
|
|
||||||
calcPriceWithCouponDiscount(response.couponValue, response.couponType)
|
|
||||||
$('#submit_form_button').prop('disabled', false).removeClass('disabled')
|
|
||||||
$('#send_coupon_code').prop('disabled', true)
|
|
||||||
$('#coupon_discount_details').prop('disabled', false).show()
|
|
||||||
})
|
|
||||||
|
|
||||||
} else {
|
|
||||||
console.log('Invalid Coupon')
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: function(response) {
|
|
||||||
const responseJson = response.responseJSON
|
|
||||||
|
|
||||||
if (!responseJson.isValid) {
|
|
||||||
Swal.fire({
|
|
||||||
icon: 'error',
|
|
||||||
title: 'Oops...',
|
|
||||||
text: responseJson.error,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
$('#payment_form').on('submit', function(e) {
|
|
||||||
if (hasCouponCodeValue) {
|
|
||||||
checkCoupon()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
$('#send_coupon_code').click(function(e) {
|
|
||||||
if (hasCouponCodeValue) {
|
|
||||||
checkCoupon()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
</script>
|
</script>
|
||||||
@endsection
|
@endsection
|
||||||
|
|
Loading…
Reference in a new issue