Skip to main content

Merchant Help

 

Affirm Merchant Help

Status Visibility Example Code

 

//-------------------------
// START AFFIRM MFP EXAMPLE

    var toInteger = function(a) {
    var _a;
    if (typeof a === "number" && a != Math.floor(a)){
        return a * 100;
    }
    if (typeof a === "number" && a === Math.floor(a)) {
        return a;
    }
    if (typeof a === "string" && a.includes('.')) {
        var _a = a.replace(/[$\.\,]/gi,'');
        return parseInt(_a);
    }
    if (typeof a === "string" && a.includes('$')) {
        var _a = a.replace(/[$\,]/gi,'') + '00';
        return parseInt(_a);
    }
    else {
        return parseInt(a);
    }
}

    var affirm_mfp_config = {
        // Price threshold
        "min": 30000,
        
        // Put your MFP Promo ID below:
        "promo_id": "",
        // Put your MFP Financing Program name below:
        "program_name": "",

        "default_promo_id": "",
        "default_program_name": ""
    };
    
    // Your existing price element's ID (optional)
    var affirm_price_elid = "the-price";
    
    // A delay to let things load (optional)
    var affirm_trigger = "refresh-button";
    
    if(affirm_trigger){
        document.getElementById(affirm_trigger);
        affirm_trigger.addEventListener('click',function(){affirmRefresh(affirm_current_price)});
    }
        var affirm_program_name = "";
      var affirm_current_price = function() {
        var _current_price;
        var _affirm_promos = document.getElementsByClassName('affirm-as-low-as');

       var _price_el = document.getElementById(affirm_price_elid);
       if (!_price_el) {
          console.log("No price provided");
          return false
        }
        if (_price_el.tagName === "INPUT"){
          _current_price = toInteger(_price_el.value);
        }
        else {
          _current_price = toInteger(_price_el.innerText);
        }
      return _current_price
      }

    var affirm_mfp_eligible = function(a) {
        return affirm_current_price >= affirm_mfp_config.min
    }
  
  function affirmRefresh(a) {
    var _affirm_promos = document.getElementsByClassName('affirm-as-low-as');
    for (var i = _affirm_promos.length - 1; i >= 0; i--) {
      _affirm_promos[i].setAttribute('data-amount',affirm_current_price());
    }
    affirmMfp(a);
    affirm.ui.refresh();
  }
  
    function affirmMfp(a) {
        var _affirm_promos = document.getElementsByClassName('affirm-as-low-as');
        if (affirm_mfp_eligible(a)) {
            console.log('mfp');
            for (var i = _affirm_promos.length - 1; i >= 0; i--) {
                _affirm_promos[i].setAttribute('data-promo-id',affirm_mfp_config.promo_id);
            }
            affirm_program_name = affirm_mfp_config.program_name;
        }
        else {
            for (var i = _affirm_promos.length - 1; i >= 0; i--) {
            console.log('default');
            _affirm_promos[i].setAttribute('data-promo-id',affirm_mfp_config.default_promo_id);
            }
            affirm_program_name = affirm_mfp_config.default_program_name
        }
    }

    // affirm_checkout_object = {
    // ...
    //    "financing_program_name" : affirm_program_name;
    // ...
    // }
  
 // END AFFIRM MFP EXAMPLE
 //-------------------------