Paypal integration instruction

September 3, 2008

For sandbox
-create developer central a/c
-creaet multiple Paypal Test a/c for buyer and merchants

make a form in your site which will hidden
this is the default format for paypal
1. <FORM action=”https://www.paypal.com/cgi-bin/webscr” method=”post”>
2. <INPUT TYPE=”hidden” name=”variableName” value=”allowedValue”>

Buy Now buttons – <INPUT TYPE=”hidden” name=”cmd” value=”_xclick”>
Donate buttons – <INPUT TYPE=”hidden” name=”cmd” value=”_donations”>
Subscribe buttons – <INPUT TYPE=”hidden” name=”cmd” value=”_xclick-
subscriptions”>
Shopping cart buttons – <INPUT TYPE=”hidden” name=”cmd” value=”_cart”>

The following are passthrough variables:
custom
item_number or item_number_x
invoice

HTML Code for FORM Prepopulation
<form action=”https://www.paypal.com/cgi-bin/webscr” method=”post”>
for sandbox

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

<input type=”hidden” name=”cmd” value=”_xclick”>
<input type=”hidden” name=”business” value=”seller@designerfotos.com”>
<input type=”hidden” name=”item_name”
value=”Memorex 256MB Memory Stick”>
<input type=”hidden” name=”item_number” value=”MEM32507725″>
<input type=”hidden” name=”amount” value=”3″>
<input type=”hidden” name=”tax” value=”1″>
<input type=”hidden” name=”quantity” value=”1″>
<input type=”hidden” name=”no_note” value=”1″>
<input type=”hidden” name=”currency_code” value=”USD”>
<!– Enable override of payer’s stored PayPal address. –>
<input type=”hidden” name=”address_override” value=”1″>
<!– Set prepopulation variables to override stored address. –>
<input type=”hidden” name=”first_name” value=”John”>
<input type=”hidden” name=”last_name” value=”Doe”>
<input type=”hidden” name=”address1″ value=”345 Lark Ave”>
<input type=”hidden” name=”city” value=”San Jose”>
<input type=”hidden” name=”state” value=”CA”>
<input type=”hidden” name=”zip” value=”95121″>
<input type=”hidden” name=”country” value=”US”>
<input type=”image” name=”submit” border=”0″
src=”https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif”
alt=”PayPal – The safer, easier way to pay online”>
</form>

<INPUT TYPE=”hidden” name=”charset” value=”utf-8″>
<INPUT TYPE=”hidden” NAME=”return” value=”URLspecificToThisTransaction”> (default paypal own return )
<INPUT TYPE=”hidden” NAME=”currency_code” value=”CurrencyCode”> ( default USD)
<INPUT TYPE=”hidden” name=”address_override” value=”1″>

===IPN ( instant payment notification)=notify_url======

====Allowable Values for the cmd HTML Variable==
Value of cmd            Description
_xclick          The button that the person clicked was a Buy Now button.
_donations       The button that the person clicked was a Donate button.
_xclick-subscriptions  The button that the person clicked was a Subscribe button.
_oe-gift-certificate   The button that the person clicked was a Buy Gift Certificate button.
_cart             For shopping cart purchases; these additional variables specify the
kind of shopping cart button that the person clicked:
add – Add to Cart buttons for the PayPal Shopping Cart
display – View Cart buttons for the PayPal Shopping Cart
upload – The Cart Upload command for third party carts
_s-xclick          The button that the person clicked was protected from tampering by
using encryption.

============= ==IPN back=============

// read the post from PayPal system and add ‘cmd’
$req = ‘cmd=_notify-validate’;

foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= “&$key=$value”;
}

// post back to PayPal system to validate
$header .= “POST /cgi-bin/webscr HTTP/1.0\r\n”;
$header .= “Content-Type: application/x-www-form-urlencoded\r\n”;
$header .= “Content-Length: ” . strlen($req) . “\r\n\r\n”;
$fp = fsockopen (’ssl://www.paypal.com’, 80, $errno, $errstr, 30);

// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, “VERIFIED”) == 0) {
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
}
else if (strcmp ($res, “INVALID”) == 0) {
// log for manual investigation
}
}
fclose ($fp);
}


Web Service

September 3, 2008

What is a Web Service?
A “web service” is a network accessible interface to application functionality built using XML and usually HTTP.
Standard Setup

* • Clients make requests and servers reply
* • Communication done over the Internet

Four Basic Steps

* 1. Web Services client script builds up the request
* 2. Client sends the request to server using HTTP
* 3. Server replies and returns an XML document with results
* 4. Client parses XML
Three forms of Web Services

* • SOAP
* • XML-RPC
* • REST

SOAP

* • Formerly known as Simple Object Access Protocol.
* • Uses XML, but you never need to touch it.
* • Which is good, because SOAP is messy to look at.
* • You just call functions and manipulate arrays. (Unless you want to.)
* • A few PHP implementations: PEAR::SOAP, PHP-SOAP, NuSOAP

Make a Request
Load in the SOAP Client
<?php require ‘SOAP/Client.php’; ?>
Generate Client Proxy (for Amazon.Com)
<?php
// We have human readable explanation of the API.
$wsdl_url = ‘http://soap.amazon.com/schemas3/AmazonWebServices.wsdl’;
$WSDL = new SOAP_WSDL($wsdl_url);
$client = $WSDL->getProxy();
?>
Web Service Description Language (WSDL)
Machine readable description (XML) of a web service. Used here to define server’s methods and parameters.
SOAP Server
<?php
require_once ‘SOAP/Server.php’;

class SOAP_Server_rot13 {
function rotate($input) {
return str_rot13($input);
}
}

$server = new SOAP_Server;
$soapclass = new SOAP_Server_rot13();
// Associate PHP class with SOAP message
$server->addObjectMap($soapclass ,’urn:SOAP_Server_rot13′);
$server->service($HTTP_RAW_POST_DATA);
?>

XML-RPC

* • XML Remote Procedure Call
* • Similar to SOAP, but less complex
* • Which is its biggest advantage
* • And its biggest disadvantage
* • But it is often “good enough”
* • But, SOAP has better buzzword compliance

REST

* • REpresentational State Transfer
* • Make URI request using existing HTTP methods: GET / POST / PUT / DELETE.
* • Data returned as XML, and you do need to touch it.
* • Which is good, because it’s not complicated.
* • Many ways to parse XML: SAX / DOM / XSLT / SimpleXML