Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomasosho's avatar

I want my form to be submitted after successful payment JS

I want my form to submit after a successful payment. it shouldn't load up automatically.

<form action="####" name="myform" method="POST">
                    {{csrf_field()}}
                    <script src="https://js.paystack.co/v1/inline.js"></script>
                    @cannot ('update', $user->profile)
                        <button type="button" onclick="payWithPaystack()"> {{$prv->amount}}kobo to view </button> 
                    @endcannot
                </form>
 
                <script>
                  function payWithPaystack(){
                    var handler = PaystackPop.setup({
                      key: '####',
                      email: '{{ auth()->check()?auth()->user()->email: null}}',
                      amount: '{{$prv->amount}}',
                      currency: "NGN",
                      ref: ''+Math.floor((Math.random() * 1000000000) + 1), // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
                      metadata: {
                        custom_fields: [
                            {
                                display_name: "Mobile Number",
                                variable_name: "mobile_number",
                                value: "+2348012345678"
                            }
                        ]
                      },
                      callback: function(response){
                          alert('success. transaction ref is ' + response.reference);
                          document.myform.submit();
                      },
                      onClose: function(){
                          alert('window closed');
                      }
                    });

                    document.myform.submit();
                    
                    handler.openIframe();

                  }
                </script>
0 likes
17 replies
fylzero's avatar

@tomasosho Looks right. You might need... maybe put id="myForm" on the form tag.

function payWithPaystack(event){
    event.preventDefault();



document.getElementById('myForm').submit();
1 like
bugsysha's avatar

Not sure I understand but have you tried replacing

document.myform.submit();
handler.openIframe();

with just

handler.openIframe();

Cause you are submitting it on success and also on every button click.

tomasosho's avatar

@fylzero It's still responds the same way. Once clicked it submits. It doesn't wait to fail of be successful.

<form action="/membersubscribe" id="myform" name="myform" method="POST">
                    {{csrf_field()}}
                  <script src="https://js.paystack.co/v1/inline.js"></script>
                  <button type="button" onclick="payWithPaystack()"> Pay </button> 
                </form>
 
                <script>
                  function payWithPaystack(){
                    var handler = PaystackPop.setup({
                      key: 'pk_test_0e8187eb43726e09614de21d5a2fe6d48266dbaa',
                      email: '{{ auth()->check()?auth()->user()->email: null}}',
                      plan: 'PLN_b4zn1ixrsn9j87a',
                      currency: "NGN",
                      ref: ''+Math.floor((Math.random() * 1000000000) + 1), // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
                      metadata: {
                        custom_fields: [
                            {
                                display_name: "Mobile Number",
                                variable_name: "mobile_number",
                                value: "+2348012345678"
                            }
                        ]
                      },
                      callback: function(response){
                          alert('success. transaction ref is ' + response.reference);
                      },
                      onClose: function(){
                          alert('window closed');
                      }
                    });
                    
                    function payWithPaystack(event){
                    event.preventDefault();
                    }

                    document.myform.submit();

                    handler.openIframe();
                  }
                </script>
tomasosho's avatar

@bugsysha with just the handler.openIframe(); it just loads the alert, it doesn't submit. I want it to submit once it is successful.

tomasosho's avatar
tomasosho
OP
Best Answer
Level 4

I solved it

callback: function(response){
                        document.myform.submit();
                      }
                    });
1 like
bugsysha's avatar

So if you have same code how does it behave differently? I do not understand.

tomasosho's avatar

You'd have to place the submit in the call back so there'll be a confirmation before it redirects.

bugsysha's avatar

Look at your initial post. You already had that in the callback. What I said is to remove the one from the end of the payWithPaystack function.

So you should have

<script>
                  function payWithPaystack(){
                    var handler = PaystackPop.setup({
                      key: '####',
                      email: '{{ auth()->check()?auth()->user()->email: null}}',
                      amount: '{{$prv->amount}}',
                      currency: "NGN",
                      ref: ''+Math.floor((Math.random() * 1000000000) + 1), // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
                      metadata: {
                        custom_fields: [
                            {
                                display_name: "Mobile Number",
                                variable_name: "mobile_number",
                                value: "+2348012345678"
                            }
                        ]
                      },
                      callback: function(response){
                          alert('success. transaction ref is ' + response.reference);
                          document.myform.submit();
                      },
                      onClose: function(){
                          alert('window closed');
                      }
                    });

                    // document.myform.submit(); this should be removed.
                    
                    handler.openIframe();

                  }
                </script>
tomasosho's avatar

@bugsysha your reply which was me removing the document.myform.submit(); Was originally my initial code and which only submits on the payment platform and gives a flash message. NOTE it doesn't send a POST request to my controller and I needed it to. What I did was I replaced the callback value with a document.myform.submit();. You really did not understand what I was talking about. I'm glad I got you laughing.🙂

1 like
tomasosho's avatar

It's cool. Did you get what I was saying? I'm still building up my js skill.

bugsysha's avatar

@tomasosho to be honest I did not. I'm repeating one thing and you are repeating other. For some reason we do not understand each other fully. Sorry for that.

muolokwu's avatar

You should create a duplicate form, then pass the values of the original form to the duplicate form when the payment gateway is initiated. Then apply the submit method on the form duplicate inside the callback response.

Please or to participate in this conversation.