Plugin Options, Defaults & Usages


Below are the available options & default values used at mojoReq(js). Set, change, update these options values anytime in your webpage using any type of event that suits your application.

Always consider to obfuscate your Instamojo api_key & auth_token.

;(function($){
    $.fn.mojoReq({
        sandbox      : true,
        sandbox_url  : 'https://test.instamojo.com/api/1.1/payment-requests/',
        process_url  : 'URL-TO-PROCESS.PHP',
        data: {
          req_url    : 'https://www.instamojo.com/api/1.1/payment-requests/',
          api_key    : null,
          auth_token : null,
          currency   : 'INR',
          redirect_url:window.location.href,
          buyer_name : null,
          email      : null,
          phone      : null,
          amount     : null,
          purpose    : null,
          send_sms   : false,
          send_email : false          
        },         
        error: 'No response from Instamojo servers. Please try again later.',
        checkout_type   : null,
        checkout_custom : function(){},
        floating_attr   : {
          rel : 'im-checkout',
          'data-behaviour' : 'remote',
          'data-style'     : 'light',
          'data-text'      : 'Checkout With Instamojo'    
        },
        floating_container : null,
        floating_embed_url : 'https://d2xwmjc4uy2hr5.cloudfront.net/im-embed/im-embed.min.js'  
    });
})(jQuery);

Methods & Application


mojoReq(js) is best used when attached to a DOM object event, for example a button click, a link trigger etc. This will give you room to further enhance your webpage checkout experience before & after mojoReq(js) initiates (via Instamojo default, light & floating checkout).

Add a progress bar or alert messages when checkout button is clicked, or initiate an alert error message if data is not retrieved when processed.

;(function($){

    $('.some-button').on('click',function( event ){

        // prevents default click behaviours
        event.preventDefault();

        // add custom functions before checkout begins

        // initiate mojoReq(js)
        $.fn.mojoReq({

            // ... your options...

        });

        setTimeout(function(){

           // add custom function when data received
           // and initiate before re-directed to Instamojo checkout page

        }, 5000 );

    });

})(jQuery);

Or use the checkout_type = 'custom' option & add your own custom functions at checkout_custom option parameter, which has access to all returned JSON objects from Instamojo which includes the unique URL secured payment link.

;(function($){

    $('.some-button').on('click',function( event ){

        // prevents default click behaviours
        event.preventDefault();

        $.fn.mojoReq({

            // ... your options ...

            checkout_type: 'custom',
            checkout_custom: function( data ){

                // get Instamojo returned success status
                var success = data.success;

                // if returned success is ok
                if( success == true ){

                   // handle checkout here
                   // do checking and create custom checkout experience

                } else {

                   // do something when status is not ok
                   // for error handling

                }
            }
        });
    });
})(jQuery);

You can also attach mojoReq(js) to a DOM object & trigger the checkout functions like so:-

;(function($){

    $('.some-element').mojoReq({

        // ... your options...

    });

    // later fire mojoPay(js)
    $('.some-element').trigger('click');

})(jQuery);

Define/access plugin options directly using $.fn.mojoReq.defaults.[options] like so:-

;(function($){

    // ie. set up an api key
    $.fn.mojoReq.defaults.data.api_key = 'YOUR-API-KEY';

    // ie. set a floating checkout
    $.fn.mojoReq.defaults.checkout_type = 'floating';
       
})(jQuery);

Example assign different values to Instamojo checkout on a separate DOM object event:-

;(function($){
 
    // define re-usable options
    $.fn.mojoReq({
      data : {
        api_key    : 'YOUR-API-KEY',
        auth_token : 'YOUR-AUTH-TOKEN'
      }
        // ... your re-usable options...
    });

    // ie: send amount 400.00 for purpose 'Download this plugin' 
    // when '.some-button-1' is clicked
    $( '.some-button-1' ).on( 'click' , function( event ){

        // prevents default click behaviors
        event.preventDefault();

        $.fn.mojoReq.defaults.data.amount = '400.00';
        $.fn.mojoReq.defaults.data.purpose = 'Download this plugin.';

    });

    // ie: send amount 5000.00 for purpose 'Buy this template'
    // when '.some-button-2' is clicked
    $( '.some-button-2' ).on( 'click' , function( event ){

        // prevents default click behaviours
        event.preventDefault();

        $.fn.mojoReq.defaults.data.amount = '5000.00';
        $.fn.mojoReq.defaults.data.purpose = 'Buy This Template.';

    });
       
})(jQuery);

Or handle checkout options with dynamic values for example from a checkout form.

<!-- example form -->
<form>
    <label>Buyer Name</label>
    <input name="buyer_name" type="text" />
    <label>Email</label>
    <input name="email" type="text" />
    <label>Phone</label>
    <input name="phone" type="text" />

    <!-- checkout button -->
    <button class="send-instamojo" type="button"></button>
</form>

<script>
;(function($){
 
    // define secret keys
    $.fn.mojoReq({
        data : {
          api_key    : 'YOUR-API-KEY',
          auth_token : 'YOUR-AUTH-TOKEN'
        }
    });

    $( '.some-button-1' ).on( 'click' , function( event ){

        // prevents default click behaviours
        event.preventDefault();

        $.fn.mojoReq({

            // ... your checkout options ...

            // grab input values from form
            data: {
                buyer_name : $('[name=buyer_name]').val(),
                email      : $('[name=email]').val(),
                phone      : $('[name=phone]').val()
            }

        });
    });
})(jQuery);
</script>

Note: Never trust data passed online, coming from a secured to non secured web environment (vice versa). Instamojo API supplies a unique algorithm (generated from your data sent) & provides you a unique link to verify your checkout & payment authenticity. Plugin is a client side application/programming, additional check should be considered as much as possible to minimize risks.

Here are the complete options, default values assigned & the available custom setup when using mojoPay(js).

Option Value Type Defaults & Description
sandbox boolean Default: true

Enable/ disable Instamojo checkout test environment.
If false, new data.api_key & data.auth_token
is needed obtained from live Instamojo account.
sandbox_url string/url Default: https://test.instamojo.com/api/1.1/payment-requests/

No changes required. Url points to instamojo test
payment API/environment.
process_url string/url Url points to hosted Instamojo payments
verification bridge.
data Object Contains object data name & value pairs to pass
to Instamojo payment verifications.
data.req_url string Default: https://www.instamojo.com/api/1.1/payment-requests/

Url pointed to Instamojo checkout API
data.api_key string Default: null

Required. Do not share.
Unique alphanumeric strings provided by Instamojo
Account > Settings.
Needs new API Key for sandbox & live environment.
data.auth_token string Default: null

Required. Do not share.
Unique alphanumeric token provided by Instamojo
Account > Settings.
Needs new API Key for sandbox & live environment.
data.currency string/currency code format Default: INR

Currency code to checkout at Instamojo. 
Supports Indian Rupee INR only.
data.redirect_url string/url Default: <page-url-initiated-Instamojo-checkout>

The url used by Instamojo API to redirect user after
payments.
data.buyer_name string Default: null

The data to pass & pre-filled at Instamojo
payment form. 
data.phone integer Default: null

The phone number to pass & pre-filled at Instamojo payment form.
If available, Instamojo will match existing users during payment.
Required if data.send_sms notification if enabled.  
data.email string Default: null

The email address to pass & pre-filled at Instamojo
payment form.
If available, Instamojo will match to existing users
during payments.
Required if data.send_email notification if enabled.  
data.amount integer Default: null

Required.
The total amount to make payment. 
Format in 2 decimal places.
data.purpose string Default: null

Data passed to display the purpose for payment.
Truncated  <30 characters. 
data.send_sms boolean Default: false

Enable/disable sending sms notification.
data.phone value is required
data.send_email boolean Default: false

Enable/disable sending email notification.
data.email value is required
error string Default: No response from Instamojo servers. Please try again later.

Text to output if failed Instamojo verifications.
checkout_type pre-defined default: light
Success verification, automatically redirects to
Instamojo payment page.

floating
Success verification, enables floating payment
experience without redirects.

custom
Enables checkout_custom function to handle custom 
payment experience before continuing to Instamojo
payment page.
checkout_custom function Default: function( data ){}

Custom function to handle data received from
Instamojo verifications.
floating_attr pre-definded rel : 'im-checkout'

'data-behaviour' : 'remote',

.'data-style' : 'light',

'data-text' : 'Checkout With Instamojo'

Customize the floating checkout display when 
checkout_type: 'floating' enabled.
Details at Instamojo integration page.
floating_container object Default: null

The HTML container Object reference to append
floating checkout initiator.
floating_embed_url string/url Default: https://d2xwmjc4uy2hr5.cloudfront.net/im-embed/im-embed.min.js

Required when checkout_type: 'floating' enabled.
The url to Instamojo embed script.

No comments:

Post a Comment