I'm just getting page refresh. nothing other.
Problem with event - not working
Hey!
I just registered an event to handle if the Stripe Subscription creation has been successful.
namespace App\Events;
use App\Providers\PaymentReceived;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Laravel\Cashier\Cashier;
use App\Models\StripeProducts;
class PaymentEventListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct($user, $payment_method)
{
$this->user = $user;
$this->payment_method = $payment_method;
}
/**
* Handle the event.
*
* @param \App\Providers\PaymentReceived $event
* @return void
*/
public function handle(PaymentReceived $event)
{
$companyData = DB::table('companies')->where('user_id', $this->user->id)->first();
$companySetup = DB::table('company_set_ups')->where('company_id', $companyData->id)->with('plan', 'method')->first();
$stripe = StripeProducts::where('plan_id', $companySetup->plan->id)->where('billing_cycle_months', $companySetup->selected_payment_plan_cycle)->first();
$user = Cashier::findBillable($this->user->stripe_id);
if($user->subscribed($companySetup->plan->name) === false) {
$user->newSubscription($companySetup->plan->name, $stripe->stripe_price_id)->create($this->payment_method);
}
if($user->subscribed($companySetup->plan->name)) {
// UPDATE Payment Completed
DB::table('company_set_ups')->where('company_id', $companyData->id)->update([
'payment_completed' => 1
]);
// ADD ACTIONS
DB::table('companies')->where('id', $companyData->id)->update([
'actions' => $companySetup->plan->actions
]);
return response()->json([
'status' => 1
]);
} else {
return response()->json([
'status' => 0
]);
}
}
}
here is the controller method for the stripe.pay.update.database route
public function stripePayUpdateDatabase(Request $request) {
return event(new PaymentEventListener(auth()->user(), $request->paymentMethod));
}
Here is the form for the payment submission which is AJAX based
<form id="payment-form" data-secret="{{ $intent->client_secret }}">
<button id="submit_payment" class="stripe-button"><i class="icon-lock text-light"></i>@lang('main.business_setup_payment_card_pay_now') (${{ $setup->calculatePlanPrice() }})</button>
</form>
here is the JS for the form
var form = document.getElementById('payment-form');
var cardHolderName = document.getElementById('cardholder-name');
var clientSecret = form.dataset.secret;
form.addEventListener('submit', async function(event) {
event.preventDefault();
$("#card-errors").empty();
$("#submit_payment").html('<i class="icon-spin5 animate-spin"></i> @lang("main.business_setup_payment_card_pay_button_proccess")');
$("#submit_payment").attr('disabled', true);
const { setupIntent, error } = await stripe.confirmCardSetup(
clientSecret, {
payment_method: {
card,
billing_details: { name: cardHolderName.value }
}
}
);
if (error) {
$("#card-errors").empty();
$("#card-errors").html("<i class='icon-warning'></i> " + error.message);
$("#submit_payment").html('<i class="icon-lock text-light"></i>@lang("main.business_setup_payment_card_pay_now") ${{ $setup->plan->price * $setup->selected_payment_plan_cycle }}');
$("#submit_payment").attr('disabled', false);
$.ajax({
url: '{{ route("stripe.pay.update.database.errors") }}',
type: 'POST',
data: {
status: error.message
}
});
} else {
stripeTokenHandler(setupIntent);
$.ajax({
url: '{{ route("stripe.pay.update.database") }}',
type: 'POST',
success: function(response) {
if(response.status == 1) {
$("#card-successes").html('<i class="icon-check-1"></i> @lang("main.business_setup_payment_card_pay_success_message")');
}
}
});
}
});
// Submit the form with the token ID.
function stripeTokenHandler(setupIntent) {
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'paymentMethod');
hiddenInput.setAttribute('value', setupIntent.payment_method);
form.appendChild(hiddenInput);
// Submit the form
form.submit();
}
Before event creation it was just a simple route that is returning json response to a AJAX call and it was working perfectly (but insecure) and I decided to move this into an event and now it doesn't work. How can I make this work and also do you have any tips how to improve security for these payments?
Normal versus your code and wrong see my comment 2 posts before
You should have
use App\Listeners\PaymentEventListener; and your listener file in this directory.
Events must be in app\Events
Listeners in app\Listeners
Please or to participate in this conversation.