PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/RSS/PayPal_Mobile_Checkout_PHP/mobile-checkout.php

https://github.com/MIT-EPROM/EPROM
PHP | 161 lines | 25 code | 22 blank | 114 comment | 3 complexity | 4b48d305903d9002294612cead663d70 MD5 | raw file
  1. <?php
  2. /* PayPal Mobile Checkout Sample PHP - http://www.paypal.com/mobile/
  3. Those who don't have an existing PayPal account:
  4. Go to https://www.paypal.com/uk/mrb/pal=GV8A6PH9C6XVG
  5. Click Sign Up Today.
  6. Set up an account for Business Owners.
  7. Follow the instructions on the PayPal site.
  8. Those who already have a Personal or Premier account:
  9. Go to https://www.paypal.com/uk/mrb/pal=GV8A6PH9C6XVG
  10. Click the Upgrade your Account link.
  11. Click the Upgrade Now button.
  12. Choose to upgrade to a Business account and follow instructions to complete the upgrade.
  13. If you haven't already, add a bank account to become a Verified member. Follow the instructions on the PayPal site. This process may take 2-3 business days.
  14. --
  15. step 1 - configure your account for mobile checkout - paypal > profile > api access > edit / create > tick SetMobileCheckout and DoMobileCheckoutPayment
  16. step 2 - configure your api username, password and signature in constants.php
  17. step 3 - upload, test and integrate with your mobile site - when you want to go live comment out the sandbox value below
  18. mobile checkout is pretty simple, you submit some variables to paypal and they return a token
  19. you use this token when you direct your customer to paypal to confirm the payment
  20. once the customer has paid they're sent back to you with the same token attached
  21. mobile checkout developer guide - https://www.paypal.com/en_US/pdf/PP_MobileCheckout.pdf
  22. integration center - http://www.paypal.com/IntegrationCenter/ic_mobile-checkout.html
  23. paypal mobile forum - http://www.pdncommunity.com/pdn/board?board.id=mobile
  24. this script was originally based on ReviewOrder.php from the paypal php nvp sdk
  25. CallerService.php and constants.php are from the sdk though constants has been changed to switch sandbox on and off and use the mobile urls
  26. original unaltered copies can be downloaded from http://www.paypal.com/sdk/
  27. the latest version of this script can be found at http://www.andymoore.info/paypal-mobile-checkout-php/
  28. Andy Moore
  29. dotMobi Certified Mobile Web Developer
  30. http://www.andymoore.info/
  31. */
  32. // do you want to test on the sandbox? set to yes or comment this line to go live
  33. $sandbox = 'yes';
  34. // initialise the session
  35. session_start();
  36. // include the file with the api functions (which subsequently includes constants.php)
  37. include('CallerService.php');
  38. // if token is not set or is empty we perform SetMobileCheckout - otherwise we're processing DoMobileCheckoutPayment
  39. if(!isset($_REQUEST['token'])||$_REQUEST['token']==''){
  40. // build up the values we will submit for this order
  41. // in the most basic form this only needs AMT CURRENCYCODE DESC RETURNURL AND CANCELURL paramaters passing - everything else is optional
  42. $submit_string .= '&AMT=1.00'; // REQUIRED - Cost of the item before tax and shipping. Must not exceed $1,000 USD in any currency. No currency symbol, decimal seperator must be a point '.' optional thousands seperator ','
  43. $submit_string .= '&CURRENCYCODE=GBP'; // REQUIRED - three character currency code. Accepts the following values: AUD / CAD / EUR / GBP / JPY / USD
  44. $submit_string .= '&DESC=Product+Description'; // REQUIRED - the name of the item being offered <127 characters
  45. $submit_string .= '&RETURNURL='.urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); // REQUIRED - urlencoded value - where the customer is directed to post paypal - ?token=123456789 will be added - it's suggested to make it the final review page prior to ordering
  46. $submit_string .= '&CANCELURL='.urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); // REQUIRED - urlencoded value - where the customer is directed to if they click the cancel or return to merchant links at paypal
  47. // $submit_string = '&PHONENUM='; // OPTIONAL - Localized phone number used by the buyer to submit their payment request. If the phone number is activated for mobile checkout PayPal will use it to pre-propagate the login page - 9 to 13 numeric characters
  48. // $submit_string .= '&TAXAMT='; // OPTIONAL - tax on item purchased
  49. // $submit_string .= '&SHIPPINGAMT='; // OPTIONAL - the shipping cost for this transaction
  50. // $submit_string .= '&NUMBER='; // OPTIONAL - pass through value, returned verbatin on DoMobileCheckoutPayment - for values like stock keeping units <127 single byte characters
  51. // $submit_string .= '&CUSTOM='; // OPTIONAL - pass through value, returned verbatin on DoMobileCheckoutPayment - store session IDs and other values here <256 characters
  52. // $submit_string .= '&INVNUM='.time(); // OPTIONAL - Your own invoice number of ID used to identify the transaction. <127 single byte characters - must be unique
  53. // $submit_string .= '&ADDRESSDISPLAY=1'; // OPTIONAL - 0 = shipping address not required or 1 = an address is required, address displayed by default - must be 1 for physical goods - users can't edit the address though they can select from other addresses on file at paypal
  54. // $submit_string .= '&SHAREPHONENUM=0'; // OPTIONAL - indicates if the customer's phone number is to be returned to the merchant. The customer will be notified during the flow and given the opportunity to override this
  55. // $submit_string .= '&EMAIL='; // OPTIONAL - email address of the buyer as entered during checkout. If the phone number is not mobile activated this is used to pre-fill the login form <127 single byte characters
  56. // these values let you set the customer's shipping address, if an address is specified it is displayed during checkout, if not the customers default shipping address will be shown
  57. // these values are only applicable if ADDRESSDISPLAY=1
  58. // $submit_string .= '&SHIPTOCITY=';// REQUIRED - name of the city <120 single byte characters
  59. // $submit_string .= '&SHIPTOSTATE=';// OPTIONAL - name of the state or province <120 single byte characters
  60. // $submit_string .= '&SHIPTOCOUNTRY=';// REQUIRED - iso 3166 country code - examples US CA UK - two single byte characters
  61. // $submit_string .= '&SHIPTOZIP=';// OPTIONAL - us zip code or other country specific postal code <20 single byte characters
  62. // perform the api callback for SetMobileCheckout with those values
  63. $resArray = hash_call('SetMobileCheckout',$submit_string);
  64. // if we get an acknowledgement of SUCCESS there should also be a valid token
  65. if(strtoupper($resArray['ACK'])=='SUCCESS'){
  66. // redirect the customer to paypal to confirm the payment, pass the token value in the url - tokens expire after three hours (20 single byte characters)
  67. header('Location: '.PAYPAL_URL.urldecode($resArray['TOKEN']));
  68. }else{
  69. // SetMobileCheckout failed
  70. echo 'SetMobileCheckout failed: '.$resArray['L_SHORTMESSAGE0'].' '.$resArray['L_ERRORCODE0'].' '.$resArray['L_LONGMESSAGE0'];
  71. }
  72. // ends no token or empty token value / ends performing SetMobileCheckout
  73. }else{
  74. // starts processing token to call DoMobileCheckOutPayment to complete the transaction and collect the users details
  75. // if you don't do DoMobileCheckoutPayment you don't get the funds
  76. // submit the token value to paypal with the DoMobileCheckoutPayment callback
  77. $resArray = hash_call('DoMobileCheckoutPayment','&token='.$_REQUEST['token']);
  78. // run an if against the ACK value - it'll return either SUCCESS or FAILURE
  79. if(strtoupper($resArray['ACK'])=='SUCCESS'){
  80. echo 'DoMobileCheckoutPayment success: '.$resArray['CURRENCYCODE'].' '.$resArray['AMT'].' from '.$resArray['EMAIL'].' ('.$resArray['FIRSTNAME'].' '.$resArray['LASTNAME'].')';
  81. // values to read are: (if you have instant payment notification running paypal will post you these values to your regular ipn handler)
  82. // CUSTOM - pass through value returned from SetMobileCheckout
  83. // INVNUM - pass through value return from SetMobileCheckout
  84. // TRANSACTIONID - unique transaction ID for this order, 19 single byte characters
  85. // PARENTTRANSACTIONID - (should always be empty) parent or related TRANSACTIONID - processed for the following transaction types: Reversal, Capture of an authorised transaction, Reauthorisation of a transaction, Capture of an order, Authorisation of an order (in both those cases the PARENTTRANSACTION is the original order id) Capture of an order authorisation, void of an order. - 16 single byte characters in xxxx-xxxx-xxxx-xxxxm format
  86. // RECEIPTID - receipt indentification 16 single byte numbers in xxxx-xxxx-xxxx-xxxx format
  87. // TRANSACTIONTYPE - the type of transaction, with PPMC only send-money will be passed
  88. // PAYMENTTYPE - indicates if the payment is instant or delayed, values are none and instant
  89. // ORDERTIME - the time and date of the order
  90. // AMT - the full order ammount before transaction fees are deducted - <$1000 USD in any currency
  91. // CURRENCYCODE - the currency passed in SetMobileCheckout - this is oddly listed in the spec four times.......
  92. // FEEAMT - the ammount PayPal charged to process this transaction
  93. // SETTLEAMT - how much from the transaction will be deposited into your PayPal account
  94. // TAXAMT - the tax charged on the transaction as specified in SetMobileCheckout
  95. // EXCHANGERATE - exchange rate if any currency conversion occured
  96. // PAYMENTSTATUS - status of the order with PayPal will be either Completed or Pending - if pending see PENDINGREASON below - can also be Reversed tho not listed in the docs
  97. // PENDINGREASON - the reason the payment is pending: none, address, intl, multi-currency, verify, unilateral, upgrade, other - see the docs for a full breakdown of each
  98. // REASONCODE - only applicable if the transaction has been reversed (PAYMENTSTATUS is Reversed)
  99. // EMAIL - the buyers email address
  100. // PayerInfo values:
  101. // PAYERID - unique customer account number for that customer
  102. // PAYERSTATUS - status of the payer's email address
  103. // COUNTRYCODE - iso 3166 country code
  104. // BUSINESS - payer's business name
  105. // PHONENUM - phone number shared by the customer with the merchant, see notes above about how the customer can deny this
  106. // PayerName values:
  107. // SALUTATION - the payer's salutation
  108. // FIRSTNAME - first name
  109. // MIDDLENAME - middle name
  110. // LASTNAME - surname
  111. // SUFFIX - suffix
  112. // AddressType values - only if DISPLAYADDRESS=1
  113. // NAME - the persons name associated with that shipping address
  114. // SHIPTOSTREET - street address line 1
  115. // SHIPTOSTREET2 - street address line 2
  116. // SHIPTOCITY - name of city
  117. // SHIPTOSTATE - name of state or province
  118. // SHIPTOCOUNTRY - ISO 3166 country code
  119. // SHIPTOZIP - us zip code or other country specific postal code
  120. // SHIPTOPHONENUM - the phone number associated with this address
  121. // ADDRESSOWNER - ebay company which maintains this address, either eBay or PayPal
  122. // ADDRESSSTATUS - status of the address on file with PayPal - either None, Confirmed or Unconfirmed
  123. // ends SUCCESS
  124. }else{
  125. // DoMobileCheckoutPayment failed
  126. echo 'DoMobileCheckoutPayment failed '.$resArray['L_SHORTMESSAGE0'].' '.$resArray['L_ERRORCODE0'].' '.$resArray['L_LONGMESSAGE0'];
  127. }
  128. // ends processing the DoMobileCheckoutPayment callback
  129. }
  130. ?>