Thursday, 30 August 2012

Certified Force.com Developer

I remember a time when I blogged about coding and solving technical problems with intelligent solutions, but now I seem to write more and more about qualifications and user-meetups.

Maybe I am becoming a consultant after all?

Well asides, because it's time for a new badge, and about time too. I have just taken and passed my Salesforce.com Certified Force.com developers exam. I have huge thanks to pass on to Christopher Lewis, a colleague with the qualification who helped me prepare, through conversations, debates and a wicked revision guide, and also to the boss himself who took the exam minutes before me (and passed) which boosted my confidence nicely.

So here's to DREAMFORCE 2012, and my Developer Session "Integrating PayPal Mass Payments with Force.com" - presented Friday September 21st 11:00 San Francisco time (7:00pm UK time) in the Developer Theatre.


Monday, 25 June 2012

Now Cross Browser Compatible!

I recently attended The Learning Tree's "CSS for Web Page Development" course, at their Euston House education center, London.

I have just received my certificate for completing the 3 day qualification, having passed the final exam with a super respectable 87.5%.


Thursday, 17 May 2012

Force by Forcewest: Salesforce Developer meetup

Thursday May 31st @ 6pm in The Elephant, Bristol
It's that time again, time for me to sell my soul and advertise another group meet-up in the South West of England.

This time, the group is Force by Forcewest, it's an-off shoot of the original Salesforce user group, Forcewest, and it's really intended just for the technically minded. No old admins in battered ties, or accountants talking about licence costs, just developers, coders and hackers (ethical, of course) talking about Apex, Workflow and Visualforce.

So if you live anywhere near Bristol and you know how important semi-colons are, then write "May 31st - FxFw" on a post-it note and stick it to your monitor. We'll be meeting in The Elephant pub, in St. Nicks market, please come on down and join us for a couple of beers, and to meet like minded people.

 Sign up for more info on the usual channels:

http://www.facebook.com/ForceByForceWest
http://twitter.com/fxfwbarman

and actually sign up (for free!)

http://fxfw1.eventbrite.com/

but more importantly just head to this pub:


Developer certification is certainly not a must (I'm not even a certified developer!) but you might find the more badges, and the more merch you can drop into the conversation, the more likely you are to be bought a pint by me :D


Thursday, 10 May 2012

Drupal cron stopped working after site migration

Have you recently moved your Drupal website to a new server, and all of a sudden Cron has stopped working, and all your jobs are timing out, or locking up?

That has recently happened to me.

First of all, I will point out that I am using Drupal 7, and we are managing a number of jobs using the Ultimate Cron module, which as you may know, is dependant on the Background Process (and Progress) module(s). We migrated our site from a test server to a live server, with identical setup, and all of a sudden, our cron jobs packed in. I couldn't run them automatically, manually, externally, with drush or even from the root shell.

For a day I struggled. With every CRON setting I could imagine.

Then, whilst manually trawling through the system and variable tables in the database I discovered the following setting:

background_process_derived_default_host | a:1:{s:7:"default";a:1:{s:8:"base_url";s:20:"http://test.desynit.com";}}

http://test.desynit.com is the name server of test site (obviously). This is not the path of the live server.

So I leapt into the admin config for the BACKGROUND PROCESS module, and lo and behold, there it was..

I clicked "Determine default service host" and this was instantly updated to the new correct base_url for the live server, and away you go!

Full cron ahead.

I hope this helps someone out.

Tuesday, 10 April 2012

Forcewest - UK Salesforce user group


Where will you be on April 17th 2012? If you'll be anywhere near the south west of England, can I recommend you make your evening plans to be attending Forcewest?

Forcewest is a networking group for Salesforce users and developers organised by my company, Desynit. Attendance is free, but places are numbered, so ideally you need to register your name at Eventbrite. It then kicks off at 6pm at Totos bar, which is on the opposite site of the river from King Street and The Apple; and less than 5 minutes walk from Temple Meads station.

There's a presentation from Simon Parker who is presenting his Surf Life Saving Salesforce.com implementation case study, followed by an open Q&A. The night then normally evolves into an eruption of conversation and debate about almost all technologies.

I'll also point out that there is a fair tab behind the bar, courtesy of Desynit, so if you're in good time you can enjoy a free pint to help break the ice with your fellow Salesforce.com users.

Thursday, 2 February 2012

Salesforce Paypal IPN integration

In my previous post Salesforce Paypal Integration I went through sending payment details to the Paypal Mass Payments API. By the end of that post, your application should be receiving "Success" messages from Paypal, and seeing payments move between two accounts. This is really only half the story though.

The "Success" message is Paypal confirming it has received the SOAP request, and it was correctly structured. You don't actually know what has happened to each payment row in your request. What, for example, happens to payments issued to e-mail addresses not registered to a Paypal account? Or if a transaction is cancelled or reversed? These are things you need to know, and that is where the Paypal IPN system comes into play.

The IPN messages are documented quite well here on the PayPal website, and indeed if you were developing in ASP or Java, that's probably all you'd need to get going with them, but this is Salesforce.com, and so things are a little different.

IPN messages are the second half of our payment process diagram:

They originate from PayPal, and are delivered to a URL in your application.

Your first action is to send the whole message back to PayPal, which will verify it originated there, and that it is intact.

You can then examine the message, extract the referenced payments, and execute update actions on their status/pay dates and so forth.

IPN messages for a transaction can appear any time up to 30 days after it was requested, and IPN messages can update a single transaction many times, depending on how complicated it proves to be.


So how do we deal with IPN messages in Salesforce? In short, we are going to do the following:
  • Establish a publically visible end point, using a visualforce page, and Salesforce sites
  • Implement a custom controller to execute the behaviour described above
We'll execute these points in reverse order though, because if we try to create the page before the controller, it will complain that it can't find the referenced files and functions.

So go to Your name -> Setup -> Develop -> Apex classes and select to make a new class. Then copy the file PaypalIPNController.cls from the GitHub repository:

 https://github.com/srlawr/PaypalSFDC/blob/master/PaypalIPNController.cls



Have a read through it so you know what's going on, and you'll probably notice that because this controller has to send the message back to PayPal we will need to add the verification URL to our Remote Site Settings so your application can communicate with the outside world.

Go to Your name -> Setup -> Security Controls -> Remote Site Settings and add a new Paypal URL for:

PayPal Sandbox IPN: https://www.sandbox.paypal.com/cgi-bin/webscr

With the Apex controller in place, we need to create the page to front it. There isn't actually anything on this page, because it's never going to be served out to a browser, it is simply acting as as URL for web-service calls. We do need to point it at this controller though,

Navigate your way to  Your name -> Setup  -> Develop -> Pages and create a new page. Call it Paypal_IPN, for clarity. This Visualforce page only has one real line to it, which you can see in paypal_ipn.page on GitHub:

https://github.com/srlawr/PaypalSFDC/blob/master/paypal_ipn.page


This is all the code you really need. When the page is loaded, the controller is constructed, and you can see the Apex page "Action" attribute to call the function "digestMessage", this is because you cannot make DML calls in a controller's constructor, or any method calls direct from it.

The final action on the Salesforce side of things is to expose this page via Salesforce sites. Click Your name -> Setup -> Develop -> Sites and click to create a new Site. I simply set the label, name and default web address all to "Paypal" and then in the Active Site Home Page selected the new page we just created.

The sites detail page should list a number of attributes of this new object, one of which is "Secure Web Address" - copy the value of this to your clipboard, because we're going to need it in a minute.


Next then, back in your Paypal sandbox, we need to tell it what URL to send the messages too, so log into your Payments Pro account and click on "Profile" -> "Instant Payment Notification preferences". On this page, you will need to enter the Secure Web Address of the Salesforce.com page, and Enable IPN messages. From the confirmation page, head over to the "IPN History Page", this page could become your best friend during development/testing.



Ready to roll!

It feels like we've done enough work now. Lets see if it's all come together.

If you return to your Payments List page, and make sure you have a handful of payment records with a status of "Unpaid", you should find yourself looking at something like this:


Just like in the last stage, if you click the "Pay out" button, the page should reload, with a success message and, having updated all the PayPal Status values to "Sent".



What has happened now is PayPal has received the request and is whipping through each of your transfers. Typically, (typically!!) this takes a few seconds in the first instance, and after each row has been considered, your first IPN message will be sent over by PayPal.

Twiddle your thumbs for just a few seconds (or if you wish to use this time constructively, check your PayPal sandbox, and make sure your transaction has been recorded on their side) and then reload the Payment List page in Salesforce.

You should see the status column has been updated, to something similar to this:


In this case, three of my transactions (those associated with my PayPal Sandbox email addresses) have completed entirely, and instantly, but one transaction is currently sitting unclaimed, because that e-mail address isn't associated with a PayPal account. You can read up more on IPN status codes over on the PayPal website.

In Paypal you can view your IPN messages, like the one below, from your IPN history page, which you'll find linked from the configuration page you put your IPN endpoint in above.



That really is it though, for a basic demonstration of Integrating the PayPal Mass Payments API with Force.com.

It should be absolutely noted that this is a purely educational blog post, and no-one should ever attempt to implement payment gateways or infrastructures without the appropriate knowledge and testing.

If you would like to talk to Desynit about applying this knowledge to your business or organisation, please get in touch via the details on our website. Otherwise, any questions or discussions are always more than welcome in the comments section below.

Tuesday, 10 January 2012

Salesforce - Paypal Masspay Integration

It took me less than two hours to get processing test transactions with the PayPal Sandbox and Salesforce.com. This blog will cover how I setup the MassPay API, to allow my Salesforce application to send money to customer accounts, on demand.

Very quickly, first of all, MassPay is the Paypal API for transacting money from one Paypal account to many others, it is called MassPay because it can handle up to 250 accounts and transaction amounts in one go. If you want to integrate a "customers paying for stuff" shopping experience, maybe give this blog a read as well, http://techsahre.blogspot.com/2011/01/simple-paypal-integration-with.html

The basic process of integrating with the PayPal API involves dispatching an XML request to their web-service endpoint, interpreting the response, and then listening out yourself for incoming IPN messages from PayPal, which deliver status updates and information on your transactions as they are processed. This UML model roughly outlines the experience.


In this blog post, I will discuss the first half of the process. If you'd like to skip off and discover more about the IPN update messages, there is a second blog post by myself called Salesforce Paypal IPN Integration.

So first of all let's head over to the Paypal sandbox at https://developer.paypal.com/ and first register ourselves a PayPal developer account. Once you're in, create yourself two preconfigured accounts, make sure one is described a PayPal Payments Pro account - which will become the source of your money (so give it a chunky balance). The other can be a Buyer or Seller account, and this will be the test account that will receive the money. You can create as many of these as you like, depending on how thoroughly you like to test!

Now, we need to activate API access to the Pro account. So from the PayPal accounts list, select the Pro account and click "Enter Sandbox Test Site" and log in. Follow these steps:
  • Click "Profile" (the last item on the second menu under "My Account")
  • Click "Request API Credentials" underneath "Account Information"
  • In Option 1 click "Setup PayPal API credentials and permissions"
  • In Option 2 click "View API Signature"
  • Copy out your API Username, API Password and Signature.

We're now done on the PayPal side of things. So log in to your Salesforce.com account. As this blog is just about integration, I will talk you through a fairly simple demo, and you can then deploy the knowledge it gives you as you see fit.

For this demo, we are going to work with one simple custom object, called Paypal_obj. The MetaData for this is on the GitHub repository for this project, in the folder demo/pages


 https://github.com/srlawr/PaypalSFDC/tree/master/demo/objects


But for ease of click-development, here it is in UML for you to work through.


I set the label to "Paypal Payment" and the Record name to "Payment number" - as an Auto-numbering field, format:




PP-{0000}


and starting at "1".


 Once you have this custom object set up, you can go ahead and create a few records. Make sure your Paypal_status field is defaulting correctly to "Unpaid" and in the Paypal_email field, put (mostly) the e-mail addresses associated with the test buyer and seller accounts you created in PayPal above. Any e-mail addresses that arn't in that list, will be your test cases for failure, because they won't work!

With a bunch of unpaid PayPal payments in our app though, we'd best create the payment processing class to deal with them...

Click on Your Name > Setup > Develop > Apex Classes and select "New" (or, if you use an IDE, create a new class file). Now, head over the the GitHub repository for this integration and get hold of a copy of the PaypalProcessor.cls. 

https://github.com/srlawr/PaypalSFDC


Copy all this this code into the file, and give it a good read. It is explained through the comments. Most importantly, copy your API user details and signature (collected above) into the relevant fields in the generateRequest function. (Note: For production, please move these out to at least a Custom Setting Object!).

Before we can interact with the PayPal servers, you need to grant your organisation access to the Paypal web services. Click Your name -> Setup -> Security Controls -> Remote Site Settings, now add the following site to the list:


Paypal_SOAP:    https://api-3t.sandbox.paypal.com/2.0/

At this point, we basically have a functioning back end integration with PayPal. But.. it'd probably be nice to have a little UI for executing payments, not least for testing. So before we start firing SOAP requests all over the place, lets set up a page and controller to give us something to look at.

First, lets add the custom controller class to our application. Create a new Apex class and copy the contents of the approvedPaypalController.cls into it from GitHub

https://github.com/srlawr/PaypalSFDC/tree/master/demo/classes


Once that is saved, click through Your name -> Setup -> Develop -> Pages and set up a new page called Payment_List. Copy the contents of the Payment_List.page file on GitHub into the body of the page:

https://github.com/srlawr/PaypalSFDC/tree/master/demo/pages


 I personally then like to create a new custom Visualforce Tab to access this page, which is a lot easier than having to navigate to the URL over and over again.

With this page, controller, and tab, and a handful of test records, you should find yourself with a setup not a million miles away from this:




Now, I would imagine you are very excited right now about that custom action button "Pay out"... and so long as you have everything appropriately set up (in Sandbox) you should be able to click it. Go ahead!

The result should be a green success message at the top of the page, and all of your payment objects will have been updated to status "Sent".. much like below:



If you now log into your PayPal Payments pro account and have a look (click on "All account activity" or "Payments sent"), you should see a transaction appear for the total amount of your payments, and also the PayPal Fees amount the request would have cost.

 If you only paid out to e-mail addresses in your list of sandbox accounts, I would expect the transaction Payment Status to be "Completed" - but if (like in the screenshots above) you had other accounts involved in the mix, it will be "Processed" - because technically, PayPal will still be waiting for those accounts to collect their money - which in Sandbox they can actually never do.

So that's it! We're kind of there. The PayPal Mass Payments API has been invoked, and we have sent potentially thousands of pounds (or dollars) out into the Internet.

Of course, when we send money out on to the internet, most people want to know what has happened to it, and that is the second half of the process, which I will cover in a subsequent post, called Salesforce Paypal IPN Integration.

(by the way, if you have clicked "Pay Out" reverting the processing is as easy as editing the records and setting their status back to "Unpaid" - a testing loop you're probably going to become very familiar with.)