REST API v2 JavaScript using PayPal Validation w on Cancel

JackieDaytona
Contributor
Contributor

What I am looking to do is add to my JavaScript/JQuery code (which is working just fine) to use the PayPAl Validation routine to cancel the pop-up window if any form fields are empty.

 

here is my code so far.

 

@ links to members, content

        paypal.

                   Buttons({

                                  style: {
                                            layout: 'horizontal',
                                            color: 'gold',
                                            shape: 'pill',
                                            label: 'checkout',
                                            size: 'responsive',
                                            tagline: 'true',
                                  },

                   // HERE IS WHERE I WANT TO INTEGRATE THE PAYPAL VALIDATION FUNCTION AND WHERE I AM HAVING ISSUES

                    onClick: function(data, actions) {

                        $(window.document) // get back to top window and then to document to use $(this)

                        var isEmpty = 'false';

                        $('input[type="text"]').each(function() {

                           if($(this).val() = "" ) {

                               empty == 'true'; 

                               onCancel: function(data) {
                                    $('#transmsg').html('<b>' + 'YOUR TRANSACTION WAS CANCELLED' + '</b>');

                                    $('#transmsg').html('<b>' + 'YOU HAVE EMPTY FORM FIELDS' + '</b>');
                                    $('#transmsg').fadeIn('slow').delay(3000).fadeOut('slow', function() {
                                    $('#paypalmsg').show();
                                    $('#chkoutmsg').show();
                                    $('#transmsg').empty();
                              });

                           }

                       });

                   },


                     // Set up the transaction
        createOrder: function(data, actions) {
       $('#paypalmsg').hide();
       $('#transmsg').show();
       $('#transmsg').html('<b>'+'WAITING ON AUTHORIZATION...'+'</b>');
       $('#chkoutmsg').hide()
       return actions.order.create({
           purchase_units: [{
          description: 'GnG Order',
           amount: {
                  value: cartTotal
           }
      }],
        application_context: {
           shipping_preference: 'NO_SHIPPING'
        }
      });
   },

    onApprove: function(data, actions) {
         return actions.order.get().then(function(orderDetails) {
         // Show a success message to the buyer
         $('#transmsg').html('<b>' + 'AUTHORIZED...' + '</b>');
          $('#transmsg').append('<br>'+'Transaction completed by: ' + orderDetails.payer.name.given_name +' '+ orderDetails.payer.name.surname + '<br>'             + "Order Id: " + orderDetails.id + '<br>' + 'Status: ' + orderDetails.status+'!' + '<br>'+ 'Thank You For Your Order'+ '<br>');
           if (orderDetails.status === "APPROVED") {
           window.setTimeout(function() {}, 500)
           $('#transmsg').append('<b>' + 'Sending Order...Please Wait' + '</b>'+'<br>');

            //do a bit of checking to see if this has been checked or not
              var getId = '#'+ $('span:contains("Market")').attr('id');
              var getmrktDiv = '#'+ $(getId).offsetParent().attr('id');
              var mrktchkId = '#'+ $(getmrktDiv).closest(getmrktDiv).find(".chkbox").attr('id');
              var mrktpriceId = '#'+ $(getmrktDiv).closest(getmrktDiv).find(".price").attr('id');
              var chkboxId = "#chk6";
              var hidpriceId = "#pricef";
              var marketLocation = $(mrktchkId);
              if (marketLocation.length > 0 ) {
                  var checked = $(mrktchkId).prop('checked');
                  if (!checked) {
                      var storedVal = $(mrktpriceId).val();
                      if ($(mrktpriceId+':not([data-val])')) {
                           $(mrktpriceId).attr("data-val", storedVal);
                        }
                         $(mrktpriceId).val("");
                  }
            }

                 $('#transid').val(orderDetails.id);
                 $('#orderstat').val(orderDetails.status);
                 $('#orderform').submit();
          }           
     });

        if (details.error === 'INSTRUMENT_DECLINED') {
            $('#transmsg').html('<b>' + 'TRANSACTION WAS DECLINED'+'</b>');
             $('#transmsg').fadeIn('slow').delay(3000).fadeOut('slow', function() {
             $('#paypalmsg').show();
             $('#chkoutmsg').show();
              $('#transmsg').empty();
        });

          return actions.restart();
    };
  },

    onCancel: function(data) {
       $('#transmsg').html('<b>' + 'YOUR TRANSACTION WAS CANCELLED' + '</b>');
       $('#transmsg').fadeIn('slow').delay(3000).fadeOut('slow', function() {
       $('#paypalmsg').show();
       $('#chkoutmsg').show();
       $('#transmsg').empty();
   });
}

}).render('#paypal-button-container');

 

 

thank you and 

Any help would be appreciated

 

Login to Me Too
4 REPLIES 4

MTS-Aaron
PayPal Employee
PayPal Employee

Hi @JackieDaytona 

 

I was able to find this documentation on this. Perhaps trying to include the mentioned "disable" method and then listen for events to happen with whatever fields you're looking for before enabling the buttons. On top of that there is the example below there for Asynchronous validation where you can do a reject or a resolve depending on if the validation passed the check (if it is more than just a checkbox). 

 

I hope this helps!

-Aaron

 

 

Login to Me Too

JackieDaytona
Contributor
Contributor

You guys over at PayPal and pointing people to your crappy API documentation is Hilarious and costs you TONS of business.

 

TRY REAL WORLD EXAMPLES...(LIKE STACK OVER FLOW DOES), YOUR API DOES NOT HANDLE REAL TIME VALIDATION WELL AT ALL (YES I KNOW WHY).  YOU SHOULD HAVE HANDLERS OVER IN YOUR TRANSACTION CANCELLED FUNCTION THAT HOOK INTO YOUR VALIDATION, IT WOULD MAKE LIFE A LOT EASIER AND CLEANER.

 

For anyone interested, I am using a SINGLE FORM CART/CHECKOUT PAGE, via Express Checkout as an ORDER.  No Clicks required (except on the PayPal) to pay for the order and submit the form for PayPal as well as en email to the merchant AFTER the transaction is approved and completed. 

 

I ended up using JQuery Validate to check the Form BEFORE I display the Paypal Buttons (that way I know it is VALID, without calling your button).  Inside the PayPAl API Iframe Window, I call my submit for the form to mailed to the merchant after it comes back approved.

 

I have the whole thing in JQuery and JavaScript using REST API V2 for anyone that is interested.  Just ask and I will be glad to send you the code or post it.

 

D

 

 

Login to Me Too

joshh385
New Community Member

I've been in a similar situation and glad to hear I am not the only one who finds this setup and related docs absurd.Would you mind sharing your solution? 

The way my form is setup ideally I would run my validation process at the time the PayPal button is clicked as they are already rendered on the form, but would like to prevent the popup from displaying if the form isn't valid.

Login to Me Too

JackieDaytona
Contributor
Contributor
Sure not a problem, give me a few min to dig through my code and ill post here .. JD
Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.