$.fn.offsetRelative = function(top){ var $this = $(this); var $parent = $this.offsetParent(); var offset = $this.position(); // add scroll offset.top += $this.scrollTop()+$parent.scrollTop(); offset.left += $this.scrollLeft()+$parent.scrollLeft(); if(!top) { // Didn't pass a 'top' element return offset; } else if($parent.get(0).tagName == "BODY") { // Reached top of document return offset; } else if($($(top), $parent).length) { // Parent element contains the 'top' element we want the offset to be relative to return offset; } else if($parent[0] == $(this).closest(top)[0]) { // Reached the 'top' element we want the offset to be relative to return offset; } else { // Get parent's relative offset var parent_offset = $parent.offsetRelative(top); offset.top += parent_offset.top; offset.left += parent_offset.left; return offset; } };
Saturday, May 9, 2015
jQuery get an elements position or offset according to some parent element
Friday, January 16, 2015
eWay Shared Rapid Payment Using PHP
First, create an api from eway api setup page for online hosted rapid payment
Then download source code.
Browse http://localhost/ewayHosted/index.php and fill up Api Key & Password field and following by other fields and click 'Pay...' button.
If all details are correct you would get data like below from eway:
stdClass Object
(
[SharedPaymentUrl] => https://secure-au.sandbox.ewaypayments.com/sharedpage/sharedpayment?AccessCode=F9802jxD7ElfeXKsdljElP1OKxSJyKZHJuaRB8qf97fzPPiDGFS-JmlfVug7kAvkctMALC0cSrVtKdomIp0DPlJv0CdRAl4apAHgJNzFMo5TXfQ804a8-5BP80gWFSmOLsLgeXOGFrjiuBvfYPjjs8F1zBw==
[AccessCode] => F9802jxD7ElfeXKsdljElP1OKxSJyKZHJuaRB8qf97fzPPiDGFS-JmlfVug7kAvkctMALC0cSrVtKdomIp0DPlJv0CdRAl4apAHgJNzFMo5TXfQ804a8-5BP80gWFSmOLsLgeXOGFrjiuBvfYPjjs8F1zBw==
[Customer] => stdClass Object
(
[CardNumber] =>
[CardStartMonth] =>
[CardStartYear] =>
[CardIssueNumber] =>
[CardName] =>
[CardExpiryMonth] =>
[CardExpiryYear] =>
[IsActive] =>
[TokenCustomerID] =>
[Reference] => KU060238
[Title] => Mr.
[FirstName] => Pritom
[LastName] => Kumar
[CompanyName] => WEB ACTIVE
[JobDescription] => Developer
[Street1] => 15 Smith St
[Street2] =>
[City] => Phillip
[State] => ACT
[PostalCode] => 2602
[Country] => au
[Email] =>
[Phone] => 1800 10 10 65
[Mobile] => 1800 10 10 65
[Comments] => Some comments here
[Fax] => 02 9852 2244
[Url] => http://www.yoursite.com
)
[Payment] => stdClass Object
(
[TotalAmount] => 100
[InvoiceNumber] => INVOICE1001
[InvoiceDescription] => Individual Invoice Description
[InvoiceReference] => 513456
[CurrencyCode] => AUD
)
[FormActionURL] => https://secure-au.sandbox.ewaypayments.com/AccessCode/F9802jxD7ElfeXKsdljElP1OKxSJyKZHJuaRB8qf97fzPPiDGFS-JmlfVug7kAvkctMALC0cSrVtKdomIp0DPlJv0CdRAl4apAHgJNzFMo5TXfQ804a8-5BP80gWFSmOLsLgeXOGFrjiuBvfYPjjs8F1zBw==
[CompleteCheckoutURL] =>
[Errors] =>
)
Then collect 'AccessCode' from data returned and redirect to 'SharedPaymentUrl' where your original payment would process.
Need to fill up all fields and click on 'PAY NOW' button above.
Now click on 'FINALIZE TRANSACTION' button to redirect back to your original site from where you went.
And finally your transaction in eWay portal:
Php & MySql oAuth2 Server & Client Example
Download source code and install
Add a new client secret pair to server side:
Start authorize from client side:
Authorize application from server side, add your login functionality by own to make the page restricted.
Now, you are getting your data from server using the access token. After expire access token, program automatically can collect new access token using refresh token.
Saturday, December 13, 2014
jQuery rounding number to nearest, flooring or ceiling as expected decimal point
function roundNumber(value, exp) { return decimalAdjust('round', value, exp); } function floorNumber(value, exp) { return decimalAdjust('floor', value, exp); } function ceilNumber(value, exp) { return decimalAdjust('ceil', value, exp); } function decimalAdjust(type, value, exp) { // If the exp is undefined or zero... if (typeof exp === 'undefined' || +exp === 0) { return Math[type](value); } value = +value; exp = +exp; // If the value is not a number or the exp is not an integer... if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) { return NaN; } // Shift value = value.toString().split('e'); value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp))); // Shift back value = value.toString().split('e'); return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)); }
jQuery Rounding Number To Nearest Interval Such: 0.05, 0.5, 0.1, 5, 10, 100
JSFIDDLE LINK ||| TEXT LINK
Html Content:<input type='text' name='number' style='width: 150px;' value='10.21'/> <input type='text' name='round' style='width: 50px;' value='0.25'/> <input type='text' name='dp' style='width: 50px;' value='2'/> <input type='button' name='check' value='Check'/> <div></div>
jQuery Content:
var div = $("div"); log("---------------------------------------------------------"); $("input[type='button']").click(function() { var number = parseFloat($("input[name='number']").val()); var round = parseFloat($("input[name='round']").val()); var dp = parseFloat($("input[name='dp']").val()); if (!isNaN(number) && !isNaN(round) && !isNaN(dp)) { var nn = roundInterval(number, round, dp, "up"); log("<br/>Rounding: " + numberToString(number, dp, true) + " using: " + round + "/" + "<span class='up-down'>Up</span>" + " as: " + numberToString(nn, dp, true)); var nn = roundInterval(number, round, dp, "down"); log("<br/>Rounding: " + numberToString(number, dp, true) + " using: " + round + "/" + "<span class='up-down'>Down</span>" + " as: " + numberToString(nn, dp, true)); log("---------------------------------------------------------"); } }); $("input[type='button']").trigger("click"); function roundInterval(number, interval, round, roundType) { if (round > 9) { round = 9; } else if (round < 0) { round = 0; } number = number > 999999999999999 ? 999999999999999 : number; var isMinus = false; if (number < 0) { isMinus = true; number = number * -1; } number = parseFloat(numberToString(number, round)); interval = parseFloat(numberToString(interval, round)); var multiplier = roundType == 'up' ? Math.ceil(number / interval) : Math.floor(number / interval); log("Multiplier: " + multiplier); log("As: " + number + "/" + interval + " :: " + (number/interval)); number = multiplier * interval; number = multiplier * interval; if (isMinus) { number = number * -1; } return parseFloat(number.toFixed(round)); } function numberToString(number, dp, asDp) { var format = '#'; if (asDp === undefined || asDp == null) { asDp = false; } if (dp !== undefined && dp != null && !isNaN(dp)) { if (dp > 9) { dp = 9; } } else { dp = 0; } if (dp > 0 && asDp) { format += "."; format += "0000000000".substr(0, dp); } else if (dp > 0) { format += "."; format += "##########".substr(0, dp); } number = number.toString(); var minus = number.substr(0, 1) == '-' ? '-' : ''; var ln = ""; if (number.lastIndexOf("e+") > -1) { ln = number.substr(0, number.lastIndexOf("e+")); for (var i = ln.length - 2; i < parseInt(number.substr(number.lastIndexOf("e+") + 1)); i++) { ln += "0"; } ln = ln.replace(/[^0-9]/g, ''); number = ln; } var tail = format.lastIndexOf('.'), nail = number.lastIndexOf('.'); if (nail < 0 && tail >= 0) { number = number + "." + format.substr(tail + 1, 1); nail = number.lastIndexOf('.'); } tail = tail > -1 && nail > -1 ? format.substr(tail) : ''; var numtail = number.substr(number.indexOf(".") ) ; if(tail.length > 0 && dp !== undefined && dp > 0) { tail = tail.substr(0, dp + 1); var tails = tail.split(''), ntail = "", canPop = true; for (var i = 1; i < tails.length; i++) { if ((tails[i] == '#' || tails[i].match(/([0-9])/g)) && numtail.length > i) { ntail += numtail.substr(i, 1); } else if(tails[i] == '#') { ntail += '0'; } else { ntail += tails[i]; } } var ttail = ntail.split(''), ptail = tail.substr(1).split(''); for(var i = ttail.length - 1; i > -1; i--){ if (ptail[i] == '#' && canPop && (ttail[i] == '0' || ttail[i] == '#')) { ntail = ntail.substr(0, ntail.length - 1); } else { canPop = false; } } if (ntail.length > 0) { tail = "." + ntail; } else { tail = ""; } } number = number.replace(/\..*|[^0-9]/g,'').split(''); format = format.replace(/\..*/g,'').split(''); for(var i = format.length - 1; i > -1; i--){ if(format[i] == '#') { format[i]=number.pop() } } number = minus + number.join('') + format.join('') + tail; return number; } function log(m) { div.prepend(m + "<br/>"); }
Friday, November 14, 2014
Spring MVC custom body tag library example
Create two java file with following contents:
package com.pkm.maven.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; public class BodyTagOne extends BodyTagSupport { private int mTimes = 0; private BodyContent mBodyContent; public void setTimes(int pTimes) { mTimes = pTimes; } @Override public void setBodyContent(BodyContent pBodyContent) { mBodyContent = pBodyContent; } @Override public int doStartTag() throws JspException { if (mTimes >= 1) { return EVAL_BODY_TAG; } else { return SKIP_BODY; } } @Override public int doAfterBody() throws JspException { if (mTimes > 1) { mTimes--; return EVAL_BODY_TAG; } else { return SKIP_BODY; } } @Override public int doEndTag() throws JspException { try { if (mBodyContent != null) { mBodyContent.writeOut(mBodyContent.getEnclosingWriter()); } } catch (Exception pIOEx) { throw new JspException("Error: " + pIOEx.getMessage()); } return EVAL_PAGE; } } package com.pkm.maven.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; public class BodyTagTwo extends BodyTagSupport { @Override public int doAfterBody() throws JspException { try { BodyContent bc = getBodyContent(); String body = bc.getString(); JspWriter out = bc.getEnclosingWriter(); if (body != null) { body = body.substring(0, 1).toUpperCase() + body.substring(1); out.print(body); } } catch (Exception ioe) { throw new JspException("Error: " + ioe.getMessage()); } return SKIP_BODY; } }
Create the tag library descriptor (TLD) under WEB-INF folder suppose named: "UiTabLib.tld":
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <tlib-version>1.0</tlib-version> <short-name>Ui Tab Library</short-name> <uri>UiTabLib</uri> <tag> <name>loopText</name> <tag-class>com.pkm.maven.tag.BodyTagOne</tag-class> <body-content>JSP</body-content> <info>This Tag Displayes given text multiple times</info> <attribute> <name>times</name> <required>true</required> <description>Provide number of times the text will be repeated</description> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>capitalize</name> <tag-class>com.pkm.maven.tag.BodyTagTwo</tag-class> <body-content>JSP</body-content> <info>This Tag Displays given string to be capitalized</info> </tag> </taglib>
Reference & use the tag library:
<p> Body Tag Example: <br/> <% Integer counter = 1; %> <UiTabLib:loopText times="10"> Counter: <%= (counter++) %><br/> </UiTabLib:loopText> <%= counter %> </p> <p> String capitalize (text to be capitalized): <UiTabLib:capitalize>text to be capitalized</UiTabLib:capitalize> </p>
Output would be like this:
Body Tag Example:
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
Counter: 6
Counter: 7
Counter: 8
Counter: 9
Counter: 10
11
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
Counter: 6
Counter: 7
Counter: 8
Counter: 9
Counter: 10
11
String capitalize (text to be capitalized): Text to be capitalized
Spring MVC simple custom tag library example
Create a java file with following contents:
package com.pkm.maven.tag; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; public class NewDateTag extends SimpleTagSupport { private String prefix; public void setPrefix(String prefix) { this.prefix = prefix; } @Override public void doTag() throws JspException, IOException { final JspWriter writer = getJspContext().getOut(); String pattern = "dd/MM/yyyy H:m:s"; SimpleDateFormat format = new SimpleDateFormat(pattern); if (prefix != null) { writer.print(prefix + " "); } writer.print(format.format(new Date())); } }
Create the tag library descriptor (TLD) under WEB-INF folder suppose named: "UiTabLib.tld":
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <tlib-version>1.0</tlib-version> <short-name>Ui Tab Library</short-name> <uri>UiTabLib</uri> <tag> <name>newDate</name> <tag-class>com.pkm.maven.tag.NewDateTag</tag-class> <body-content>empty</body-content> <attribute> <name>prefix</name> <type>java.lang.String</type> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
Reference & use the tag library:
<%@taglib prefix="UiTabLib" uri="UiTabLib" %> <p>New Date Tag Lib Output: <UiTabLib:newDate/></p> <p>New Date Tag Lib Output: <UiTabLib:newDate prefix="Prefix"/></p>
Output would be like this:
New Date Tag Lib Output: 14/11/2014 18:25:12
New Date Tag Lib Output: Prefix 14/11/2014 18:25:12
Subscribe to:
Posts (Atom)