Plugin Directory

Changeset 3212066


Ignore:
Timestamp:
12/23/2024 09:37:00 AM (15 months ago)
Author:
ampacash
Message:

Fixed VEPRAP not displaying the message after the payment correctly.

Location:
ampacash-payment-method
Files:
17 added
3 edited

Legend:

Unmodified
Added
Removed
  • ampacash-payment-method/trunk/ampacash-veprap-payment-wp.php

    r3197136 r3212066  
    33 * Plugin Name: AmpaCash Veprap Payment
    44 * Description: AmpaCash Veprap Payment Method for user to pay for merchandise.
    5  * Version: 2.9
     5 * Version: 3.0
    66 * Author: AmpaCash
    77 * License: GPLv2 or later
  • ampacash-payment-method/trunk/js/script.js

    r3192084 r3212066  
    1 setTimeout(function() {
    2     // Ensure the variables are properly defined
    3     var paymentMethodVar = typeof ampaCashVars !== 'undefined' ? ampaCashVars.paymentMethodVar : 'ampacash_veprap';
    4     var isUserLoggedInVar = typeof ampaCashVars !== 'undefined' ? ampaCashVars.isUserLoggedInVar : '';
    5     var merchantIdVar = typeof ampaCashVars !== 'undefined' ? ampaCashVars.merchantIdVar : '';
    6    
    7         let isVeprapInitialized = false;
    8    
    9         function initializeVeprap() {
    10             if (!isVeprapInitialized) {
    11                 console.log("Initializing veprap.js...");
    12                 jQuery.getScript("wp-content/plugins/ampacash-payment-method/js/veprap.js", function () {
    13                     isVeprapInitialized = true;
    14                     console.log("veprap.js successfully loaded.");
    15                 });
    16             }
    17         }
    18    
    19         function removeVeprap() {
    20             if (isVeprapInitialized) {
    21                 console.log("Removing veprap.js...");
    22                 // Clean up any event listeners or variables here
    23                 jQuery("#def").off("onsuccess");
    24                 jQuery("#ampa-cash-errors").html("");
    25                 localStorage.removeItem("paymentStatus");
    26    
    27                 isVeprapInitialized = false;
    28             }
    29         }
    30        
    31     // Function to show/hide the order button
    32     function toggleOrderButton(display) {
    33         console.log("place-order button visibility:", display);
    34         document.getElementById("place_order").style.display = display;
    35         document.getElementById("def").style.display = !display;
    36     }
    37    
    38     // Function to validate billing details
    39     function validateBillingDetails() {
    40         console.log("validating...");
    41         var isValid = true;
    42    
    43         // Clear previous errors
    44         jQuery("#ampa-cash-errors").html("");
    45    
    46         // Check all required fields with class "validate-required" and without style="display: none;"
    47         jQuery(".validate-required:visible").each(function () {
    48             var fieldId = jQuery(this).find("input, select, textarea").attr("id");
    49             var fieldValue = jQuery(this).find("input, select, textarea").val();
    50            
    51             // Custom validation for ZIP code format
    52             if (fieldId === "billing_postcode") {
    53                 if (!isValidZIP(fieldValue)) {
    54                     isValid = false;
    55                     var errorMessage = "<div class='wc-block-components-notice-banner is-error' role='alert'><div class='wc-block-components-notice-banner__content'>Billing ZIP Code is not a valid postcode / ZIP.</div></div>";
    56                     jQuery("#ampa-cash-errors").append(errorMessage);
    57                 }
    58             }
    59    
    60             if (!fieldValue) {
    61                 console.log("invalid fields found...");
    62                 isValid = false;
    63    
    64                 // Display the error message
    65                 var errorMessage = "<div class='wc-block-components-notice-banner is-error' role='alert'><div class='wc-block-components-notice-banner__content'><strong>" + fieldId + "</strong> is a required field.</div></div>";
    66                 jQuery("#ampa-cash-errors").append(errorMessage);
    67             }
    68         });
    69    
    70         return isValid;
    71     }
    72    
    73     // Function to validate ZIP code format
    74     function isValidZIP(zip) {
    75         var zipRegex = /^[a-zA-Z0-9]{5,6}$/;
    76         return zipRegex.test(zip);
    77     }
    78    
    79     // Function to check if the user is logged in
    80     function isUserLoggedIn() {
    81         return isUserLoggedInVar;
    82     }
    83    
    84     jQuery(function ($) {
    85         paymentMethodVar = $("input[name='payment_method']:checked").val(); // Get the initially selected payment method
    86         console.log("Get the initially selected payment method", paymentMethodVar);
    87        
    88         // Check local storage for payment success status
    89         if (localStorage.getItem("paymentStatus") === "success") {
    90             document.getElementById("success_def").innerHTML = "Payment Received.";
    91             // toggleOrderButton("block");
    92         } else {
    93             // Hide the order button by default if the payment method is veprap
    94             if (paymentMethodVar === "ampacash_veprap") {
    95                 initializeVeprap();
    96                 toggleOrderButton("none");
    97                 console.log("VEPRAP is selected by default", paymentMethodVar);
    98             }
    99             else {
    100                 removeVeprap();
    101                 setTimeout(() => {toggleOrderButton("block")}, 1000);
    102                 console.log("Different payment method is selected by default", paymentMethodVar);
    103             }
    104         }
    105    
    106         // Handle payment method change event
    107         $(document.body).on("change", "input[name='payment_method']", function () {
    108             paymentMethodVar = $(this).val(); // Update the variable when payment method changes
    109                    
    110             console.log("paymentMethodVar: ", paymentMethodVar);
    111             console.log("paymentMethodVar === 'ampacash_veprap'?: ", paymentMethodVar === "ampacash_veprap");
    112            
    113             if (paymentMethodVar === "ampacash_veprap") {
    114                 initializeVeprap();
    115             } else {
    116                 removeVeprap();
    117             }
    118            
    119             toggleOrderButton(paymentMethodVar === "ampacash_veprap" && $("#success_def").html().trim() === "" ? "none" : "block");
    120         });
    121    
    122         // Handle AmpaCash button hover event (onmouseover)
    123         $(document.body).on("mouseover", "#def a", function () {
    124             if (validateBillingDetails() && isUserLoggedIn()) {
    125                 // Get the merchant ID
    126                 var merchantId = merchantIdVar;
    127                 $("#def").attr("merchantid", merchantId);
    128             } else {
    129                 $("#def").removeAttr("merchantid");
    130                 if (!isUserLoggedIn()) {
    131                     var errorMessage = "<div class='wc-block-components-notice-banner is-error' role='alert'><div class='wc-block-components-notice-banner__content'>You must be logged in to use this payment method.</div></div>";
    132                     jQuery("#ampa-cash-errors").append(errorMessage);
    133                 }
    134             }
    135         });
    136    
    137         // Reset payment status on page load
    138         $(window).on('load', function() {
    139             localStorage.removeItem("paymentStatus");
    140             document.getElementById("success_def").innerHTML = "";
    141            
    142             if ($("input[name='payment_method']:checked").val() !== "ampacash_veprap") {
    143                 toggleOrderButton("block");
    144                 console.log("Resetting Order button to block");
    145             } else {
    146                 toggleOrderButton("none");
    147                 console.log("Resetting Order button to none");
    148             }
    149         });
    150     });
    151    
    152    
    153     console.log("Script loaded");
    154     document.querySelector('#def').addEventListener("onsuccess", (event) => {
     1setTimeout(function () {
     2  // Ensure the variables are properly defined
     3  var paymentMethodVar =
     4    typeof ampaCashVars !== "undefined"
     5      ? ampaCashVars.paymentMethodVar
     6      : "ampacash_veprap";
     7  var isUserLoggedInVar =
     8    typeof ampaCashVars !== "undefined" ? ampaCashVars.isUserLoggedInVar : "";
     9  var merchantIdVar =
     10    typeof ampaCashVars !== "undefined" ? ampaCashVars.merchantIdVar : "";
     11
     12  let isVeprapInitialized = false;
     13
     14  function initializeVeprap() {
     15    if (!isVeprapInitialized) {
     16      console.log("Initializing veprap.js...");
     17      jQuery.getScript(
     18        "wp-content/plugins/ampacash-payment-method/js/veprap.js",
     19        function () {
     20          isVeprapInitialized = true;
     21          console.log("veprap.js successfully loaded.");
     22        }
     23      );
     24    }
     25  }
     26
     27  function removeVeprap() {
     28    if (isVeprapInitialized) {
     29      console.log("Removing veprap.js...");
     30      // Clean up any event listeners or variables here
     31      jQuery("#def").off("onsuccess");
     32      jQuery("#ampa-cash-errors").html("");
     33      localStorage.removeItem("paymentStatus");
     34
     35      isVeprapInitialized = false;
     36    }
     37  }
     38
     39  // Function to show/hide the order button
     40  function toggleOrderButton(display) {
     41    console.log("place-order button visibility:", display);
     42    document.getElementById("place_order").style.display = display;
     43    document.getElementById("def").style.display = !display;
     44  }
     45
     46  // Function to validate billing details
     47  function validateBillingDetails() {
     48    console.log("validating...");
     49    var isValid = true;
     50
     51    // Clear previous errors
     52    jQuery("#ampa-cash-errors").html("");
     53
     54    // Check all required fields with class "validate-required" and without style="display: none;"
     55    jQuery(".validate-required:visible").each(function () {
     56      var fieldId = jQuery(this).find("input, select, textarea").attr("id");
     57      var fieldValue = jQuery(this).find("input, select, textarea").val();
     58
     59      // Custom validation for ZIP code format
     60      if (fieldId === "billing_postcode") {
     61        if (!isValidZIP(fieldValue)) {
     62          isValid = false;
     63          var errorMessage =
     64            "<div class='wc-block-components-notice-banner is-error' role='alert'><div class='wc-block-components-notice-banner__content'>Billing ZIP Code is not a valid postcode / ZIP.</div></div>";
     65          jQuery("#ampa-cash-errors").append(errorMessage);
     66        }
     67      }
     68
     69      if (!fieldValue) {
     70        console.log("invalid fields found...");
     71        isValid = false;
     72
     73        // Display the error message
     74        var errorMessage =
     75          "<div class='wc-block-components-notice-banner is-error' role='alert'><div class='wc-block-components-notice-banner__content'><strong>" +
     76          fieldId +
     77          "</strong> is a required field.</div></div>";
     78        jQuery("#ampa-cash-errors").append(errorMessage);
     79      }
     80    });
     81
     82    return isValid;
     83  }
     84
     85  // Function to validate ZIP code format
     86  function isValidZIP(zip) {
     87    var zipRegex = /^[a-zA-Z0-9]{5,6}$/;
     88    return zipRegex.test(zip);
     89  }
     90
     91  // Function to check if the user is logged in
     92  function isUserLoggedIn() {
     93    return isUserLoggedInVar;
     94  }
     95
     96  jQuery(function ($) {
     97    paymentMethodVar = $("input[name='payment_method']:checked").val(); // Get the initially selected payment method
     98    console.log("Get the initially selected payment method", paymentMethodVar);
     99
     100    // Check local storage for payment success status
     101    if (localStorage.getItem("paymentStatus") === "success") {
     102      document.getElementById("success_def").innerHTML = "Payment Received.";
     103      // toggleOrderButton("block");
     104    } else {
     105      // Hide the order button by default if the payment method is veprap
     106      if (paymentMethodVar === "ampacash_veprap") {
     107        initializeVeprap();
     108        toggleOrderButton("none");
     109        console.log("VEPRAP is selected by default", paymentMethodVar);
     110      } else {
     111        removeVeprap();
     112        setTimeout(() => {
     113          toggleOrderButton("block");
     114        }, 1000);
     115        console.log(
     116          "Different payment method is selected by default",
     117          paymentMethodVar
     118        );
     119      }
     120    }
     121
     122    // Handle payment method change event
     123    $(document.body).on("change", "input[name='payment_method']", function () {
     124      paymentMethodVar = $(this).val(); // Update the variable when payment method changes
     125
     126      console.log("paymentMethodVar: ", paymentMethodVar);
     127      console.log(
     128        "paymentMethodVar === 'ampacash_veprap'?: ",
     129        paymentMethodVar === "ampacash_veprap"
     130      );
     131
     132      if (paymentMethodVar === "ampacash_veprap") {
     133        initializeVeprap();
     134      } else {
     135        removeVeprap();
     136      }
     137
     138      toggleOrderButton(
     139        paymentMethodVar === "ampacash_veprap" &&
     140          $("#success_def").html().trim() === ""
     141          ? "none"
     142          : "block"
     143      );
     144    });
     145
     146    // Handle AmpaCash button hover event (onmouseover)
     147    $(document.body).on("mouseover", "#def a", function () {
     148      if (validateBillingDetails() && isUserLoggedIn()) {
     149        // Get the merchant ID
     150        var merchantId = merchantIdVar;
     151        $("#def").attr("merchantid", merchantId);
     152      } else {
     153        $("#def").removeAttr("merchantid");
     154        if (!isUserLoggedIn()) {
     155          var errorMessage =
     156            "<div class='wc-block-components-notice-banner is-error' role='alert'><div class='wc-block-components-notice-banner__content'>You must be logged in to use this payment method.</div></div>";
     157          jQuery("#ampa-cash-errors").append(errorMessage);
     158        }
     159      }
     160    });
     161
     162    // Reset payment status on page load
     163    $(window).on("load", function () {
     164      localStorage.removeItem("paymentStatus");
     165      document.getElementById("success_def").innerHTML = "";
     166
     167      if (
     168        $("input[name='payment_method']:checked").val() !== "ampacash_veprap"
     169      ) {
     170        toggleOrderButton("block");
     171        console.log("Resetting Order button to block");
     172      } else {
     173        toggleOrderButton("none");
     174        console.log("Resetting Order button to none");
     175      }
     176    });
     177  });
     178
     179  console.log("Script loaded");
     180  setTimeout(function () {
     181    var defElement = document.querySelector("#def");
     182    if (defElement) {
     183      defElement.addEventListener("onsuccess", function (event) {
    155184        console.log("event lister not initialized...");
    156185        if (event.detail.data.data["success"]) {
    157             console.log(event.detail.data.data["message"]);
    158             if (document.getElementById("success_def").innerHTML !== "Payment Received.") {
    159                 document.getElementById("success_def").innerHTML = "Payment Received.";
    160                 // toggleOrderButton("block");
    161             }
    162             document.getElementById("ampa-cash-errors").style.display = "none";
    163             document.getElementById("error_def").innerHTML = "";
    164    
    165             // Set payment status to success in local storage
    166             localStorage.setItem("paymentStatus", "success");
    167             // Automatically submit the order form
    168             setTimeout(function() {
    169                 jQuery('form.checkout').submit();
    170             }, 100);
    171            
     186          console.log(event.detail.data.data["message"]);
     187          if (
     188            document.getElementById("success_def").innerHTML !==
     189            "Payment Received."
     190          ) {
     191            document.getElementById("success_def").innerHTML =
     192              "Payment Received.";
     193            // toggleOrderButton("block");
     194          }
     195          document.getElementById("ampa-cash-errors").style.display = "none";
     196          document.getElementById("error_def").innerHTML = "";
     197
     198          // Set payment status to success in local storage
     199          localStorage.setItem("paymentStatus", "success");
     200          // Automatically submit the order form
     201          setTimeout(function () {
     202            jQuery("form.checkout").submit();
     203          }, 100);
    172204        } else {
    173             console.log("error has been logged: ", event.detail.data.data["message"]);
    174             if (document.getElementById("success_def").innerHTML !== "Payment Received.") {
    175                 document.getElementById("success_def").innerHTML = "";
    176                 document.getElementById("ampa-cash-errors").style.display = "block";
    177                 document.getElementById("error_def").innerHTML = event.detail.data.data["message"];
    178             }
    179         }
    180     });
    181     }, 1000);
    182    
     205          console.log(
     206            "error has been logged: ",
     207            event.detail.data.data["message"]
     208          );
     209          if (
     210            document.getElementById("success_def").innerHTML !==
     211            "Payment Received."
     212          ) {
     213            document.getElementById("success_def").innerHTML = "";
     214            document.getElementById("ampa-cash-errors").style.display = "block";
     215            document.getElementById("error_def").innerHTML =
     216              event.detail.data.data["message"];
     217          }
     218        }
     219      });
     220      console.log("Event listener attached.");
     221    } else {
     222      console.log("Failed to attach the event listener: Element not found.");
     223    }
     224  }, 1000);
     225}, 1000);
  • ampacash-payment-method/trunk/readme.txt

    r3197136 r3212066  
    44Requires at least: 4.7
    55Tested up to: 6.5
    6 Stable tag: 2.9
     6Stable tag: 3.0
    77Requires PHP: 7.0
    88License: GPLv2 or later
     
    101101== Changelog ==
    102102
     103= 3.0 =
     104* Fixed VEPRAP not displaying the message after the payment correctly.
     105
    103106= 2.9 =
    104107* Removed colon from the payment method title.
Note: See TracChangeset for help on using the changeset viewer.