This PHP library is a basic implementation for the paypal mobile checkout NVP interface. For simplicities sake I am going to create a single controller for the checkout that will check to see if we have a token or not, and then process it accordingly.
We will require the Cana_Paypal libraries for this. Go ahead and grab them over on github:
https://github.com/arzynik/cana-paypal
First lets set up our object. You will need to create an api user/pass/sig on paypals site, or with the sandbox.
$paypal = new Cana_Paypal_Mobile;
$paypal->setApiVersion('3.0') ->setApiUrl('https://mobile.paypal.com/wc?t=') ->setApiUser('api_user') ->setApiPass('api_pass') ->setApiSignature('api_sig');
We next need to call the SetMobileCheckout method on the api. In this example we will switch between Set and Do checkout based on if there is a token. You can see how this all fits together by downloading the library and example at the bottom of the page.
if (empty($_GET['token'])) { // SetMobileCheckout } else { // DoMobileCheckout }
SetMobileCheckout is what we do first, then send the user off to paypal.
$response = $paypal->setAmt('5.00') ->setCurrencycode('USD') ->setDesc('Test Item') ->setReturnurl('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']) ->setCancelurl('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']) ->setCheckout($optionalParams ? optionalParams : null);
if ($paypal->getResponseStatus()) { // forward user off to paypal header('Location: '.$paypal->getApiUrl().urldecode($paypal->getToken())); } else { // error print_r($response); exit; }
When they come back, they will have a token. We take that token and authenticate it back to paypal to finish the transaction.
$response = $paypal->setToken($_GET['token']) ->doCheckoutPayment();
if ($paypal->getResponseStatus()) { // success! print_r($response); exit; } else { // error print_r($response); exit; }