We recently had a requirement where we had to find the “verification” status of any given paypal account. This might be very useful to reduce the risk of online frauds. I was searching for a Ruby on rails plugin for PayPal’s adaptive accounts API. Neither did i have the time nor the patience to go through the search results. Looking at the GetVerifiedStatus Adaptive Accounts API it looked damn straight forward. So i went ahead to implement it with my own code.
Its pretty simple. I used the httpclient gem, which is pretty cool, to make the http request. You can install it by doing
gem install httpclient
I also use the xmlsimple gem to process the response from the API. Makes things a lot easier.
gem install xml-simple
Once you have the gems installed, these are the parameters you got to watch out for
The end point for Sandbox is
https://svcs.sandbox.paypal.com/AdaptiveAccounts/GetVerifiedStatus
And the production end point is
https://svcs.paypal.com/AdaptiveAccounts/GetVerifiedStatus
The entire code. Here you go!
def verify_paypal
#include the gems needed
require 'httpclient'
require 'xmlsimple'
#set the header of the request
header = {"X-PAYPAL-SECURITY-USERID" => "tok261_biz_api.abc.com",
"X-PAYPAL-SECURITY-PASSWORD" => "1244612379",
"X-PAYPAL-SECURITY-SIGNATURE" => "lkfg9groingghb4uw5",
"X-PAYPAL-REQUEST-DATA-FORMAT" => "NV",
"X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML",
"X-PAYPAL-APPLICATION-ID" => "APP-80W284485P519543T"
}
#data to be sent in the request
data = {"emailAddress" => params[:paypal],
"firstName"=> current_user.first_name,
"lastName" => current_user.last_name,
"matchCriteria"=> "NAME",
"requestEnvelope.errorLanguage" => "en_US"}
#initialize the request
clnt = HTTPClient.new
#API end point(sandbox)
uri = "https://svcs.sandbox.paypal.com/AdaptiveAccounts/GetVerifiedStatus"
#make the post
res = clnt.post(uri, data, header)
#if the request is successfull parse the XML
if res.status == 200
@xml = XmlSimple.xml_in(res.content)
#check if the status node exists in the xml
if @xml['accountStatus']!=nil
account_status = @xml['accountStatus'][0]
#its pretty obvious from here init?
if account_status.to_s() == "VERIFIED"
render :text => "Account verified"
else
render :text => "Oopsy! Yet to be verified"
end
else
render :text => "Gee! sorry! something went seriously wrong"
end
end
end
Breaking it down. First the request header. PayPal expects the following parameters in the request header
#set the headers of the request
#X-PAYPAL-SECURITY-USERID = <Your API username>
#X-PAYPAL-SECURITY-PASSWORD = <Your API password>
#X-PAYPAL-SECURITY-SIGNATURE = <Your API signature>
#X-PAYPAL-REQUEST-DATA-FORMAT = NV
#Please note that for sandbox you can use APP-80W284485P519543T
# as the X-PAYPAL-APPLICATION-ID
header = {"X-PAYPAL-SECURITY-USERID" => "tok261_biz_api.abc.com",
"X-PAYPAL-SECURITY-PASSWORD" => "1244612379",
"X-PAYPAL-SECURITY-SIGNATURE" => "lkfg9groingghb4uw5",
"X-PAYPAL-REQUEST-DATA-FORMAT" => "NV",
"X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML",
"X-PAYPAL-APPLICATION-ID" => "APP-80W284485P519543T"
}
In the above snippet if you notice, im sending the data as name value pairs in the request.
"X-PAYPAL-REQUEST-DATA-FORMAT" => "NV"
and consuming the response data in XML format.
"X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML"
Please note that paypal sends and accepts data as NV,XML,JSON
Now comes the data to be sent along the request
data = {"emailAddress" => "vindibly@dibly.net",
"firstName"=> "vin",
"lastName" => "dibly",
"matchCriteria"=> "NAME",
"requestEnvelope.errorLanguage" => "en_US"}
All the parameters are required for GetVerifiedStatus call. firstName, lastName, matchCriteria. The parameter matchCriteria takes only the value “NAME” as of now and this is case sensitive. The API checks if the account is the name of “vin dibly” in case of our example. If that varies, status is returned as unknown. So we got to make sure the user provides or your system has the right first and last name of the user.
The rest of it
clnt = HTTPClient.new
sandbox url
uri = "https://svcs.sandbox.paypal.com/AdaptiveAccounts/GetVerifiedStatus"
Post the data with the header information to the API
res = clnt.post(uri, data, header)
check if the request is succesfull
if res.status == 200
#now parse the response content(which happens to be a XML in our case)
@xml = XmlSimple.xml_in(res.content)
make sure the node isnt nil and check the status value
if @xml['accountStatus']!=nil
#yep this is where the status sits
account_status = @xml['accountStatus'][0]
#you know what to do if its a verified account
if account_status.to_s() == "VERIFIED"
#do your logic
render :text => "Account verified"
else
render :text => "Oopsy! Yet to be verified"
end
else
render :text => "Gee! sorry! something went seriously wrong"
end
end