Osama's Weblog

The place where you get updated!
  • rss
  • Home
  • Who am I?
  • RSS Feeds

Paypal integration in Apex

Osama | December 23, 2009

Payment gateways are parimary requiremnt in most of the business application these days. Applications building up on force.com platform require payment gateways.

Integration of paypal seems to be a very common issue. One of my readers asked me to write the code to integrate paypal with apex.

Below I am writing the code which I wrote for a project. I hope this will help many of the apex developers. This particular method is for DoDirectPayment method.

public class PayPalApi
{
public static String result {set;get;}
public static String makeCallout(String amount, String shipToName, String shipToStreet1, String shipToStreet2, String shipToCity, String shipToState, String shipToPostalCode, String shipToCountry, String creditCardType, String creditCardNumber, String expMonth, String expYear, String payerFName, String payerLName, String payerCountry, String payerStreet1, String payerStreet2, String payerCity, String payerState, String payerPostalCode, String CVV2Number)
{ // Instantiate a new http object
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
// The webservice url - Pass in the endpoint to be used using the string url
String url = 'https://api-3t.sandbox.paypal.com/2.0/';
String soapXML;

soapXML =  '<soap:Envelope xmlns:soap=' + '\'' + 'http://schemas.xmlsoap.org/soap/envelope/'  + '\'' + ' xmlns:xsi=' + '\''+ 'http://www.w3.org/2001/XMLSchema-instance'  + '\'' + ' xmlns:xsd=' + '\''+ 'http://www.w3.org/2001/XMLSchema' + '\'' + '>';
soapXML += '<soap:Header><RequesterCredentials xmlns="urn:ebay:api:PayPalAPI"><Credentials xmlns="urn:ebay:apis:eBLBaseComponents">';
soapXML += '<Username>PAYPAL_USER_NAME</Username><ebl:Password xmlns:ebl="urn:ebay:apis:eBLBaseComponents">';
soapXML += 'PAYPAL_PASSWORD</ebl:Password><Signature>PAYPAL_SIGNATURE</Signature>';
soapXML += '</Credentials></RequesterCredentials></soap:Header><soap:Body><DoDirectPaymentReq xmlns="urn:ebay:api:PayPalAPI">';
soapXML += '<DoDirectPaymentRequest><Version xmlns="urn:ebay:apis:eBLBaseComponents">1.00</Version>';
soapXML += '<DoDirectPaymentRequestDetails xmlns="urn:ebay:apis:eBLBaseComponents">';
soapXML += '<PaymentAction>Sale</PaymentAction><PaymentDetails><OrderTotal currencyID="USD">' + amount + '</OrderTotal>';
soapXML += '<ShipToAddress><Name>' + shipToName + '</Name><Street1>' + shipToStreet1 + '</Street1><Street2>' +shipToStreet2 + '</Street2>';
soapXML += '<CityName>' + shipToCity + '</CityName><StateOrProvince>' + shipToState + '</StateOrProvince><PostalCode>' + shipToPostalCode + '</PostalCode>';
soapXML += '<Country>' + shipToCountry + '</Country></ShipToAddress>';
soapXML += '</PaymentDetails><CreditCard><CreditCardType>' + creditCardType + '</CreditCardType><CreditCardNumber>' + creditCardNumber + '</CreditCardNumber>';
soapXML += '<ExpMonth>' + expMonth + '</ExpMonth><ExpYear>' + expYear + '</ExpYear><CardOwner><PayerStatus>verified</PayerStatus><Payer>bgiles@ddd.com</Payer>';
soapXML += '<PayerName><FirstName>' + payerFName+ '</FirstName><LastName>' + payerLName + '</LastName></PayerName><PayerCountry>' + payerCountry + '</PayerCountry>';
soapXML += '<Address><Street1>' + payerStreet1 + '</Street1><Street2>' + payerStreet2 + '</Street2><CityName>' + payerCity + '</CityName>';
soapXML += '<StateOrProvince>' + payerState + '</StateOrProvince><Country>' + payerCountry + '</Country><PostalCode>' + payerPostalCode + '</PostalCode></Address>';
soapXML += '</CardOwner><CVV2>' + CVV2Number + '</CVV2></CreditCard></DoDirectPaymentRequestDetails>';
soapXML += '</DoDirectPaymentRequest></DoDirectPaymentReq></soap:Body></soap:Envelope>';

req.setBody(soapXML);
req.setEndpoint(url);
req.setMethod('POST');
req.setHeader('Content-length', '1753' );
req.setHeader('Content-Type', 'text/xml;charset=UTF-8'); req.setHeader('SOAPAction','');
req.setHeader('Host','api-aa.sandbox.paypal.com');

HttpResponse res = h.send(req);
String xml = res.getBody();

XmlStreamReader reader = res.getXmlStreamReader();
result = readXMLResponse(reader,'Ack');
return result;
}
public static String readXMLResponse(XmlStreamReader reader, String sxmltag)
{
string retValue; // Read through the XML
while(reader.hasNext())
{
if (reader.getEventType() == XmlTag.START_ELEMENT)
{
if (reader.getLocalName() == sxmltag) {
reader.next();
if (reader.getEventType() == XmlTag.characters)
{ retValue = reader.getText();
}
}
} reader.next();
}
return retValue;
}
}

Other paypal methods dont require this type of coding because they are mostly redirected to paypal website. If you need further help, feel free to ask questions.

P.S: This code is completely working. You just have to replace with your credentials.

  • Share/Bookmark
Comments
No Comments »
Categories
APEX
Tags
APEX, DoDirectPayment, paypal, VisualForce, web service
Comments rss Comments rss
Trackback Trackback

Searching for Regular Expression in Apex

Osama | December 19, 2009

We can traverse a string in in order to search for a particular sort of expression. For example if we want to search a date expression from a string, we can use do that. For this to be done, we have PATTERN and MATCHER classes in apex.

I had to parse an email message to search for a regular expression. Using PATTERN class, we can set any kind of expression we have to search for. e.g

Pattern cpattern = Pattern.compile('[S][O]-(\\d{0,100})-(\\d{0,100})-{\\d{0,100}')

The above statement sets a particular regular expression. In order to search this kind of expression in a string, we can use the following statement:

Matcher cmatcher = cpattern.matcher(myString);

We can set a Boolean variable which can return true or false if he value is found in the string or not:

myVar = cmatcher.find();

and if we want to get the part of the string that has been found in the given string:

myName2 = cmatcher.group();

The overall code looks like:

public class linkTestController {
public String myName {set;get;}
public String myName2 {set;get;}
public Boolean myVar {set;get;}
public Integer myVar2 {set;get;}

public linktestController()
{
myName = '1313 1231231 SO-20091217-000253   asdad   123  adasd';
Pattern cpattern = Pattern.compile('[S][O]-(\\d{0,100})-(\\d{0,100})*');
Matcher cmatcher = cpattern.matcher(myName );
myVar = cmatcher.find();
myName2 = cmatcher.group();
}

}
Now, if I display the value of myName2, it will return ‘SO-20091217-000253’ (without quotes) only.

Please feel free to ask any questions.

Technorati Tags: visualforce,apex,salesforce,REGEX,pattern,matcher,regular expression
  • Share/Bookmark
Comments
No Comments »
Categories
APEX, VisualForce
Tags
APEX, matcher, pattern, REGEX, regular expression, salesforce, VisualForce
Comments rss Comments rss
Trackback Trackback

Google Goggles – A new way to search

Osama | December 12, 2009

Gooogle
Tired of searching with keywords? Don’t come up with a keyword for your desired item? Here is the solution. Google has came up with an exciting product, Google Wave. Its a visual search indeed. Just submit the snapshot of something and you are flooded with the search results with the detail of the image.

For example, you are standing in front of Statue of Liberty. Take the snapshot and submit it in the Google Goggle application and you come up with the search results containing Statue of Liberty.

Moreover, if you find out what business are nearby, just point your phone to a store front. It works with the GPS in your cell phone.

Currently, Google Goggle supports photographic searches for Landmarks, Books, Contact Info, Places, Logos and Artworks.

According to Google,

This is just the beginning – it’s not quite perfect yet.Works well for some things, but not for all.

So there is more to come.

Here’s how it works: “When you capture an image, Google breaks it down into object-based signatures. It then compares those signatures against every item it can find in its image database. Within seconds, it returns the results to you, ordered by rank. Some results are returned before you even snap a photo, too, thanks to seamless integration of GPS and compass functionality.”

If you are worried about the privacy, then you don’t have to. The application provides you two options, whether to save the images or discard the images. The application saves the images with the intention to improvise the service.

The Google Goggles app is now available as a free download in the Android Market.

The examples for this application can be found here .

[youtube=Google Goggles]

  • Share/Bookmark
Comments
No Comments »
Categories
General
Tags
android, goggles, google, images, search
Comments rss Comments rss
Trackback Trackback

Populating html components with apex controller variables

Osama | December 11, 2009

I was working today and there was a requirement for a third party integration on a visual force page. That requires a html form  which takes input and redirects to the required page by pressing submit button.

there were html input tags.. <input type="text" name="txtBox" />

I was jus thinking how to populate this textbox with the apex variable I have at the backend, I was about to try the javaScript method to populate this textbox with apex controller variable. But before that what I did was, I just called the variable name in the ‘value’ attribute of the input component this way:

<input type="text" name="txtBox" value="{!myVar}" />

this seems pretty simpler. it saved my lot of time by not getting into javaScript. I didnt know before that we can call apex controller variable for html components.

Feel free to ask any questions.

  • Share/Bookmark
Comments
No Comments »
Categories
APEX, VisualForce
Tags
APEX, Apex variable, API, controller, html, input, variable, Visual Force, VisualForce
Comments rss Comments rss
Trackback Trackback

My status

Categories

  • .NET
  • APEX
  • consulting
  • General
  • Oracle CRM on Demand
  • salesforce
  • Uncategorized
  • VisualForce

MM Did You Know?

Charlie Chaplin once won third prize in a Charlie Chaplin look-alike contest.
Plugin by mmilan

Its all about cloud

Salesforce

.NET .NET 4.0 beta actionFunction actionSupport administrator android APEX Apex variable API button certification cloud computing consulting controller custom DoDirectPayment force.com GET goggles google html images input inputField JavaScript jquery master-detail matcher Parallel Programming parameters pattern paypal query string REGEX regular expression salesforce search url variable Visual Force VisualForce Visual Studio vmware web service XML PARSING DOM SAX STAX

WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.

My Tweets

Error: Please make sure the Twitter account is public.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox