PayPal
Edit the file paypal.inc in the folder includes.
paypal.inc
- global $paypal_context, $paypal_url, $paypal_api_url, $paypal_api_version;
- $paypal_context = 'sandbox'; // 'sandbox' or 'live'
- if ($paypal_context == 'sandbox') {
- $paypal_api_url = 'https://api-3t.sandbox.paypal.com/nvp';
- $paypal_url = 'https://www.sandbox.paypal.com';
- }
- else {
- $paypal_api_url = 'https://api-3t.paypal.com/nvp';
- $paypal_url = 'https://www.paypal.com';
- }
- $paypal_api_version='124';
- global $paypal_username, $paypal_password, $paypal_signature;
- if ($paypal_context == 'sandbox') {
- $paypal_username = false;
- $paypal_password = false;
- $paypal_signature = false;
- }
- else {
- $paypal_username = false;
- $paypal_password = false;
- $paypal_signature = false;
- }
- global $paypal_log;
- $paypal_log=true;
Initialize $paypal_username
, $paypal_password
and $paypal_signature
in the case where $paypal_context
is 'sandbox'
with the values defined in the configuration of your developer's account.
IMPORTANT: Make sure that $paypal_context
is set to 'sandbox'
.
Test
Validate a complete payment by the site with the donation form.
Go to the home page of the site.
If the home page doesn't display the button in the banner, make sure that the action home
builds the block banner
with the parameter donate
at true
.
You can also directly enter the URL /en/donation in the address bar of the navigator.
In case of error, check if the action donation
is properly associated to a URL in the file aliases.inc of the folder config.
Entrer an amount of 25 EUR and press on . The navigator is redirected to the payment page of Paypal.
Start by cancelling the payment. The site displays the page which confirms the cancellation of a payment.
Payment cancelled
Your payment of 25.00 EUR has been cancelled. If you think this is an error, please contact us.
Retry the operation this time validating the payment using your buyer's test account. The site displays the page which confirms a payment.
Payment accepted
Your payment of 25.00 EUR has been accepted. Thank you for your confidence and support.
If a payment is rejected, the site displays yet another page.
Payment rejected
Your payment of 25.00 EUR has been rejected. If you think this is an error, please contact us.
If you have set the parameter $paypal_log
to true
, display the log of the operations in the file of the day in the folder log/paypal.log.
2017-09-26 11:23:43 127.0.0.1 METHOD=SetExpressCheckout;ACK=SUCCESS;TOKEN=PP-8LW79843V0049193J
To switch the site in production mode with real payments, initialize $paypal_username
, $paypal_password
and $paypal_signature
in the case where $paypal_context
is not 'sandbox'
with the values defined in the configuration of your merchant's account.
Set the parameter $paypal_context
to 'live'
.
Code
paypalcheckout.php
- require_once 'paypal.php';
- require_once 'userisidentified.php';
- require_once 'userprofile.php';
- require_once 'validatecurrency.php';
Loads the code of the functions paypal_setexpresscheckout
, paypal_amt
and paypal_localecode
which interface Paypal as well as the configuration file paypal.inc.
Loads the code of the functions user_is_identified
, user_profile
and validate_currency
.
- function paypalcheckout($lang, $amount, $currency, $tax=0, $context=false) {
The function paypalcheckout
has 5 parameters: the language, the total amount of the payment all taxes included, the currency of the payment, the total tax amount and an optional array which can be used to carry through the whole process details on the payment such as a product number, billing informations, etc.
- global $base_url, $paypal_url, $sitename, $supported_languages;
Accesses the global variable $base_url
which contains the URL of the entry point of the site, $paypal_url
which defines the URL which will be used to call Paypal, $sitename
which will be passed as a parameter to Paypal and $supported_languages
which is used to choose a default language for the Paypal payment page.
- if (!user_is_identified()) {
- return run('error/unauthorized', $lang);
- }
Checks if the user is identified.
- if (!(is_numeric($amount) and $amount > 0)) {
- return run('error/badrequest', $lang);
- }
- $amt=paypal_amt($amount);
Checks the parameter $amount
and formats it for the Paypal interface.
- if (!validate_currency($currency)) {
- return run('error/badrequest', $lang);
- }
- $currencycode=$currency;
Checks the parameter $currency
and formats it for the Paypal interface.
- if (!(is_numeric($tax) and $tax >= 0)) {
- return run('error/badrequest', $lang);
- }
- $taxamt=paypal_amt($tax);
Checks the parameter $tax
and formats it for the Paypal interface.
- $itemamt=paypal_amt($amount-$tax);
Calculates and formats the total price free of tax.
- $name=translate('donate:name', $lang);
- $locale = $lang;
- if (!$locale) {
- $locale=user_profile('locale');
- }
- if (!$locale) {
- $locale=$supported_languages[0];
- }
- $localecode=paypal_localecode($locale);
- $email=user_profile('mail');
- $brandname=$sitename;
- $hdrimg=$base_url . '/logos/sitelogo.png';
- $returnurl=$base_url . url('paypalreturn', $lang);
- $cancelurl=$base_url . url('paypalcancel', $lang);
- $params = array(
- 'LOCALECODE' => $localecode,
- 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
- 'PAYMENTREQUEST_0_CURRENCYCODE' => $currencycode,
- 'PAYMENTREQUEST_0_AMT' => $amt,
- 'PAYMENTREQUEST_0_ITEMAMT' => $itemamt,
- 'PAYMENTREQUEST_0_TAXAMT' => $taxamt,
- 'L_PAYMENTREQUEST_0_NAME0' => $name,
- 'L_PAYMENTREQUEST_0_AMT0' => $itemamt,
- 'L_PAYMENTREQUEST_0_TAXAMT0' => $taxamt,
- 'L_PAYMENTREQUEST_0_QTY0' => '1',
- 'NOSHIPPING' => '1',
- 'EMAIL' => $email,
- 'BRANDNAME' => $sitename,
- 'HDRIMG' => $hdrimg,
- 'RETURNURL' => $returnurl,
- 'CANCELURL' => $cancelurl,
- );
Prepares the parameters required by the Paypal interface for a request for an immediate payment.
- $r = paypal_setexpresscheckout($params);
- if (!$r) {
- return run('error/internalerror', $lang);
- }
Calls the function paypal_setexpresscheckout
which sends a request to the Paypal site.
Displays the page signaling an internal error if the operation has failed.
- $token = $r['TOKEN'];
Extracts the value of the token given to the operation by Paypal.
- $_SESSION['paypal'] = compact('token', 'amt', 'itemamt', 'taxamt', 'currencycode', 'context');
Saves in the session the token provided by Paypal and the parameters of the payment.
- reload($paypal_url . '/webscr&cmd=_express-checkout&token=' . $token);
- }
Loads the payment page of the Paypal site in the navigator.
paypalreturn.php
- require_once 'paypal.php';
- function paypalreturn($lang, $arglist=false) {
- if (!isset($_SESSION['paypal'])) {
- return run('error/badrequest', $lang);
- }
- $token=$_SESSION['paypal']['token'];
- $amt=$_SESSION['paypal']['amt'];
- $itemamt=$_SESSION['paypal']['itemamt'];
- $taxamt=$_SESSION['paypal']['taxamt'];
- $currencycode=$_SESSION['paypal']['currencycode'];
- $context=$_SESSION['paypal']['context'];
- unset($_SESSION['paypal']);
- if (!isset($arglist['token']) or $arglist['token'] != $token) {
- return run('error/badrequest', $lang);
- }
- $params = array(
- 'TOKEN' => $token,
- );
- $r = paypal_getexpresscheckoutdetails($params);
- if (!$r) {
- return run('error/internalerror', $lang);
- }
- if ($r['TOKEN'] != $token or $r['PAYMENTREQUEST_0_AMT'] != $amt or $r['PAYMENTREQUEST_0_ITEMAMT'] != $itemamt or $r['PAYMENTREQUEST_0_TAXAMT'] != $taxamt or $r['PAYMENTREQUEST_0_CURRENCYCODE'] != $currencycode) {
- return run('error/internalerror', $lang);
- }
- $payerid = $r['PAYERID'];
- $email = $r['EMAIL'];
- $params = array(
- 'TOKEN' => $token,
- 'PAYERID' => $payerid,
- 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
- 'PAYMENTREQUEST_0_CURRENCYCODE' => $currencycode,
- 'PAYMENTREQUEST_0_AMT' => $amt,
- 'PAYMENTREQUEST_0_ITEMAMT' => $itemamt,
- 'PAYMENTREQUEST_0_TAXAMT' => $taxamt,
- );
- $r = paypal_doexpresscheckoutpayment($params);
- if (!$r) {
- return run('error/internalerror', $lang);
- }
- if ($r['TOKEN'] != $token or $r['PAYMENTINFO_0_AMT'] != $amt or $r['PAYMENTINFO_0_TAXAMT'] != $taxamt or $r['PAYMENTINFO_0_CURRENCYCODE'] != $currencycode) {
- return run('error/internalerror', $lang);
- }
- $transactionid=$r['PAYMENTINFO_0_TRANSACTIONID'];
- $paymentstatus=strtoupper($r['PAYMENTINFO_0_PAYMENTSTATUS']);
- $completed=false;
- switch ($paymentstatus) {
- case 'COMPLETED':
- $feeamt=$r['PAYMENTINFO_0_FEEAMT'];
- $completed=true;
- break;
- case 'PENDING':
- $pendingreason=strtoupper($r['PAYMENTINFO_0_PENDINGREASON']);
- break;
- default:
- break;
- }
- if (!$completed) {
- require_once 'actions/paymentrejected.php';
- $output = paymentrejected($lang, $amt, $currencycode, $context);
- }
- else {
- require_once 'actions/paymentaccepted.php';
- $output = paymentaccepted($lang, $amt, $currencycode, $context);
- }
- return $output;
- }
paypalcancel.php
- function paypalcancel($lang, $arglist=false) {
- if (!isset($_SESSION['paypal'])) {
- return run('error/badrequest', $lang);
- }
- $token=$_SESSION['paypal']['token'];
- $amt=$_SESSION['paypal']['amt'];
- $currencycode=$_SESSION['paypal']['currencycode'];
- $context=$_SESSION['paypal']['context'];
- unset($_SESSION['paypal']);
- if (!isset($arglist['token']) or $arglist['token'] != $token) {
- return run('error/badrequest', $lang);
- }
- require_once 'actions/paymentcancelled.php';
- return paymentcancelled($lang, $amt, $currencycode, $context);
- }
See the documentation on the functions which interface the Paypal service defined in paypal by the library.
Comments