2009-01-26 22:49:43 +01:00
< ? php
2024-12-11 20:44:48 +01:00
/* Copyright ( C ) 2008 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2011 - 2012 Regis Houssin < regis . houssin @ inodbox . com >
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2009-01-26 22:49:43 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2009-01-26 22:49:43 +01:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2009-01-26 22:49:43 +01:00
*/
/**
2010-10-04 01:20:49 +02:00
* \file htdocs / paypal / lib / paypal . lib . php
2010-10-27 23:51:41 +02:00
* \ingroup paypal
2010-10-03 20:16:14 +02:00
* \brief Library for common paypal functions
2009-01-26 22:49:43 +01:00
*/
2011-09-06 00:52:17 +02:00
2020-12-07 11:01:08 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/payments.lib.php' ;
2011-09-06 00:52:17 +02:00
2011-05-17 12:36:53 +02:00
/**
* Define head array for tabs of paypal tools setup pages
2011-10-03 18:10:50 +02:00
*
2024-10-14 01:59:44 +02:00
* @ return array < array { 0 : string , 1 : string , 2 : string } > Array of tabs to show
2011-05-17 12:36:53 +02:00
*/
function paypaladmin_prepare_head ()
{
2011-05-26 12:16:10 +02:00
global $langs , $conf ;
2011-07-13 18:55:25 +02:00
2011-05-17 12:36:53 +02:00
$h = 0 ;
$head = array ();
$head [ $h ][ 0 ] = DOL_URL_ROOT . " /paypal/admin/paypal.php " ;
2015-08-31 12:13:23 +02:00
$head [ $h ][ 1 ] = $langs -> trans ( " PayPal " );
2011-05-17 12:36:53 +02:00
$head [ $h ][ 2 ] = 'paypalaccount' ;
$h ++ ;
2011-07-13 18:55:25 +02:00
2019-11-13 19:35:39 +01:00
$object = new stdClass ();
2011-12-07 17:28:11 +01:00
2020-10-27 19:46:07 +01:00
// Show more tabs from modules
// Entries must be declared in modules descriptor with line
// $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
// $this->tabs = array('entity:-tabname); to remove a tab
2019-01-27 11:55:16 +01:00
complete_head_from_modules ( $conf , $langs , $object , $head , $h , 'paypaladmin' );
2012-12-01 15:45:05 +01:00
2019-01-27 11:55:16 +01:00
complete_head_from_modules ( $conf , $langs , $object , $head , $h , 'paypaladmin' , 'remove' );
2011-05-17 12:36:53 +02:00
2020-10-27 19:46:07 +01:00
return $head ;
2011-05-17 12:36:53 +02:00
}
2010-11-21 14:48:42 +01:00
2011-09-06 00:52:17 +02:00
2010-11-21 14:48:42 +01:00
/**
* Send redirect to paypal to browser
2010-11-21 14:55:18 +01:00
*
2011-10-03 18:10:50 +02:00
* @ param float $paymentAmount Amount
* @ param string $currencyCodeType Currency code
* @ param string $paymentType Payment type
* @ param string $returnURL Url to use if payment is OK
* @ param string $cancelURL Url to use if payment is KO
2017-05-13 15:19:35 +02:00
* @ param string $tag Full tag
2018-03-04 19:39:46 +01:00
* @ return string No return ( a redirect is done ) if OK , or Error message if KO
2010-11-21 14:48:42 +01:00
*/
2019-01-27 15:20:16 +01:00
function print_paypal_redirect ( $paymentAmount , $currencyCodeType , $paymentType , $returnURL , $cancelURL , $tag )
2010-11-21 14:48:42 +01:00
{
2020-10-27 19:46:07 +01:00
//declaring of global variables
global $conf , $langs ;
global $API_Endpoint , $API_Url , $API_version , $USE_PROXY , $PROXY_HOST , $PROXY_PORT ;
global $PAYPAL_API_USER , $PAYPAL_API_PASSWORD , $PAYPAL_API_SIGNATURE ;
global $shipToName , $shipToStreet , $shipToCity , $shipToState , $shipToCountryCode , $shipToZip , $shipToStreet2 , $phoneNum ;
global $email , $desc ;
//'------------------------------------
//' Calls the SetExpressCheckout API call
//'
//'-------------------------------------------------
2023-11-27 11:56:32 +01:00
if ( ! getDolGlobalString ( 'PAYPAL_API_INTEGRAL_OR_PAYPALONLY' )) {
2021-02-26 15:27:05 +01:00
$conf -> global -> PAYPAL_API_INTEGRAL_OR_PAYPALONLY = 'integral' ;
}
2020-10-27 19:46:07 +01:00
$solutionType = 'Sole' ;
$landingPage = 'Billing' ;
// For payment with Paypal only
2023-10-08 23:19:49 +02:00
if ( getDolGlobalString ( 'PAYPAL_API_INTEGRAL_OR_PAYPALONLY' ) == 'paypalonly' ) {
2020-10-27 19:46:07 +01:00
$solutionType = 'Mark' ;
$landingPage = 'Login' ;
}
// For payment with Credit card or Paypal
2023-10-08 23:19:49 +02:00
if ( getDolGlobalString ( 'PAYPAL_API_INTEGRAL_OR_PAYPALONLY' ) == 'integral' ) {
2020-10-27 19:46:07 +01:00
$solutionType = 'Sole' ;
$landingPage = 'Billing' ;
}
// For payment with Credit card
2023-10-08 23:19:49 +02:00
if ( getDolGlobalString ( 'PAYPAL_API_INTEGRAL_OR_PAYPALONLY' ) == 'cconly' ) {
2020-10-27 19:46:07 +01:00
$solutionType = 'Sole' ;
$landingPage = 'Billing' ;
}
2010-11-21 14:48:42 +01:00
2021-01-21 11:50:15 +01:00
dol_syslog ( " print_paypal_redirect expresscheckout redirect with callSetExpressCheckout $paymentAmount , $currencyCodeType , $paymentType , $returnURL , $cancelURL , $tag , $solutionType , $landingPage , $shipToName , $shipToStreet , $shipToCity , $shipToState , $shipToCountryCode , $shipToZip , $shipToStreet2 , $phoneNum " );
2020-10-27 19:46:07 +01:00
$resArray = callSetExpressCheckout (
$paymentAmount ,
$currencyCodeType ,
$paymentType ,
$returnURL ,
$cancelURL ,
$tag ,
$solutionType ,
$landingPage ,
$shipToName ,
$shipToStreet ,
$shipToCity ,
$shipToState ,
$shipToCountryCode ,
$shipToZip ,
$shipToStreet2 ,
$phoneNum ,
$email ,
$desc
);
2021-01-21 11:50:15 +01:00
dol_syslog ( " print_paypal_redirect resArray= " . var_export ( $resArray , true ), LOG_DEBUG );
2020-10-27 19:46:07 +01:00
$ack = strtoupper ( $resArray [ " ACK " ]);
2021-02-26 15:27:05 +01:00
if ( $ack == " SUCCESS " || $ack == " SUCCESSWITHWARNING " ) {
2020-10-27 19:46:07 +01:00
$token = $resArray [ " TOKEN " ];
// Redirect to paypal.com here
$payPalURL = $API_Url . $token ;
2021-01-21 11:50:15 +01:00
dol_syslog ( " Redirect to " . $payPalURL , LOG_INFO );
2020-10-27 19:46:07 +01:00
header ( " Location: " . $payPalURL );
exit ;
} else {
//Display a user friendly Error on the page using any of the following error information returned by PayPal
$ErrorCode = urldecode ( $resArray [ " L_ERRORCODE0 " ]);
$ErrorShortMsg = urldecode ( $resArray [ " L_SHORTMESSAGE0 " ]);
$ErrorLongMsg = urldecode ( $resArray [ " L_LONGMESSAGE0 " ]);
$ErrorSeverityCode = urldecode ( $resArray [ " L_SEVERITYCODE0 " ]);
2021-02-26 15:27:05 +01:00
if ( $ErrorCode == 10729 ) {
2023-09-01 09:18:23 +02:00
$mesg = " PayPal can't accept payments for this thirdparty. An address is defined but is not complete (missing State).<br>Ask system administrator to fix address or to setup Paypal module to accept payments even on not complete addresses (remove option PAYPAL_REQUIRE_VALID_SHIPPING_ADDRESS).<br> \n " ;
2020-10-27 19:46:07 +01:00
} else {
$mesg = $langs -> trans ( 'SetExpressCheckoutAPICallFailed' ) . " <br> \n " ;
$mesg .= $langs -> trans ( 'DetailedErrorMessage' ) . " : " . $ErrorLongMsg . " <br> \n " ;
$mesg .= $langs -> trans ( 'ShortErrorMessage' ) . " : " . $ErrorShortMsg . " <br> \n " ;
$mesg .= $langs -> trans ( 'ErrorCode' ) . " : " . $ErrorCode . " <br> \n " ;
$mesg .= $langs -> trans ( 'ErrorSeverityCode' ) . " : " . $ErrorSeverityCode . " <br> \n " ;
}
return $mesg ;
}
2010-11-21 14:48:42 +01:00
}
2015-04-07 02:45:30 +02:00
/**
2015-04-08 00:14:44 +02:00
*-------------------------------------------------------------------------------------------------------------------------------------------
* Purpose : Prepares the parameters for the SetExpressCheckout API Call .
* Inputs :
* paymentAmount : Total value of the shopping cart
* currencyCodeType : Currency code value the PayPal API
* paymentType : paymentType has to be one of the following values : Sale or Order or Authorization
* returnURL : the page where buyers return to after they are done with the payment review on PayPal
* cancelURL : the page where buyers return to when they cancel the payment review on PayPal
* shipToName : the Ship to name entered on the merchant ' s site
* shipToStreet : the Ship to Street entered on the merchant ' s site
* shipToCity : the Ship to City entered on the merchant ' s site
* shipToState : the Ship to State entered on the merchant ' s site
* shipToCountryCode : the Code for Ship to Country entered on the merchant ' s site
* shipToZip : the Ship to ZipCode entered on the merchant ' s site
* shipToStreet2 : the Ship to Street2 entered on the merchant ' s site
* phoneNum : the phoneNum entered on the merchant ' s site
* email : the buyer email
* desc : Product description
2018-03-04 19:39:46 +01:00
* See https :// developer . paypal . com / docs / classic / api / merchant / SetExpressCheckout_API_Operation_NVP /
2015-04-08 00:14:44 +02:00
*
* @ param double $paymentAmount Payment amount
* @ param string $currencyCodeType Currency
* @ param string $paymentType Payment type
* @ param string $returnURL Return Url
* @ param string $cancelURL Cancel Url
2017-05-13 15:19:35 +02:00
* @ param string $tag Full tag
2018-03-04 19:39:46 +01:00
* @ param string $solutionType Type ( 'Mark' or 'Sole' )
* @ param string $landingPage Landing page ( 'Login' or 'Billing' )
2015-04-08 00:14:44 +02:00
* @ param string $shipToName Ship to name
* @ param string $shipToStreet Ship to street
* @ param string $shipToCity Ship to city
* @ param string $shipToState Ship to state
* @ param string $shipToCountryCode Ship to country code
* @ param string $shipToZip Ship to zip
* @ param string $shipToStreet2 Ship to street2
* @ param string $phoneNum Phone
* @ param string $email Email
* @ param string $desc Description
2024-11-04 12:32:13 +01:00
* @ return array < string , string > Array
2015-04-07 02:45:30 +02:00
*/
2019-01-27 15:20:16 +01:00
function callSetExpressCheckout ( $paymentAmount , $currencyCodeType , $paymentType , $returnURL , $cancelURL , $tag , $solutionType , $landingPage , $shipToName , $shipToStreet , $shipToCity , $shipToState , $shipToCountryCode , $shipToZip , $shipToStreet2 , $phoneNum , $email = '' , $desc = '' )
2010-11-21 14:48:42 +01:00
{
2020-10-27 19:46:07 +01:00
//------------------------------------------------------------------------------------------------------------------------------------
// Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation
//declaring of global variables
global $conf , $langs , $mysoc ;
global $API_Endpoint , $API_Url , $API_version , $USE_PROXY , $PROXY_HOST , $PROXY_PORT ;
global $PAYPAL_API_USER , $PAYPAL_API_PASSWORD , $PAYPAL_API_SIGNATURE ;
$nvpstr = '' ;
//$nvpstr = $nvpstr . "&VERSION=".$API_version; // Already added by hash_call
$nvpstr = $nvpstr . " &RETURNURL= " . urlencode ( $returnURL );
$nvpstr = $nvpstr . " &CANCELURL= " . urlencode ( $cancelURL );
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'PAYPAL_ALLOW_NOTES' )) {
2024-08-07 02:53:45 +02:00
$nvpstr .= " &ALLOWNOTE=0 " ;
2020-10-27 19:46:07 +01:00
}
2023-11-27 11:56:32 +01:00
if ( ! getDolGlobalString ( 'PAYPAL_REQUIRE_VALID_SHIPPING_ADDRESS' )) {
2024-08-07 02:53:45 +02:00
$nvpstr .= " &NOSHIPPING=1 " ; // An empty or not complete shipping address will be accepted
2020-10-27 19:46:07 +01:00
} else {
2024-08-07 02:53:45 +02:00
$nvpstr .= " &NOSHIPPING=0 " ; // A valid shipping address is required (full required fields mandatory)
2020-10-27 19:46:07 +01:00
}
2024-08-07 02:53:45 +02:00
$nvpstr .= " &SOLUTIONTYPE= " . urlencode ( $solutionType );
$nvpstr .= " &LANDINGPAGE= " . urlencode ( $landingPage );
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'PAYPAL_CUSTOMER_SERVICE_NUMBER' )) {
2024-08-07 02:53:45 +02:00
$nvpstr .= " &CUSTOMERSERVICENUMBER= " . urlencode ( getDolGlobalString ( 'PAYPAL_CUSTOMER_SERVICE_NUMBER' )); // Hotline phone number
2020-10-27 19:46:07 +01:00
}
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
$paypalprefix = 'PAYMENTREQUEST_0_' ;
//$paypalprefix = '';
2021-02-26 15:27:05 +01:00
if ( ! empty ( $paypalprefix ) && $paymentType == 'Sole' ) {
$paymentType = 'Sale' ;
}
2019-11-13 19:35:39 +01:00
2024-03-08 02:52:18 +01:00
$nvpstr = $nvpstr . " &AMT= " . urlencode (( string ) ( $paymentAmount )); // Total for all elements
2019-11-13 19:35:39 +01:00
2020-10-27 19:46:07 +01:00
$nvpstr = $nvpstr . " & " . $paypalprefix . " INVNUM= " . urlencode ( $tag );
2024-03-08 02:52:18 +01:00
$nvpstr = $nvpstr . " & " . $paypalprefix . " AMT= " . urlencode (( string ) ( $paymentAmount )); // AMT deprecated by paypal -> PAYMENTREQUEST_n_AMT
$nvpstr = $nvpstr . " & " . $paypalprefix . " ITEMAMT= " . urlencode (( string ) ( $paymentAmount )); // AMT deprecated by paypal -> PAYMENTREQUEST_n_AMT
2020-10-27 19:46:07 +01:00
$nvpstr = $nvpstr . " & " . $paypalprefix . " PAYMENTACTION= " . urlencode ( $paymentType ); // PAYMENTACTION deprecated by paypal -> PAYMENTREQUEST_n_PAYMENTACTION
$nvpstr = $nvpstr . " & " . $paypalprefix . " CURRENCYCODE= " . urlencode ( $currencyCodeType ); // CURRENCYCODE deprecated by paypal -> PAYMENTREQUEST_n_CURRENCYCODE
$nvpstr = $nvpstr . " & " . $paypalprefix . " L_PAYMENTREQUEST_0_QTY0=1 " ;
2024-03-08 02:52:18 +01:00
$nvpstr = $nvpstr . " & " . $paypalprefix . " L_PAYMENTREQUEST_0_AMT0= " . urlencode (( string ) ( $paymentAmount ));
2020-10-27 19:46:07 +01:00
$nvpstr = $nvpstr . " & " . $paypalprefix . " L_PAYMENTREQUEST_0_NAME0= " . urlencode ( $desc );
$nvpstr = $nvpstr . " & " . $paypalprefix . " L_PAYMENTREQUEST_0_NUMBER0=0 " ;
$nvpstr = $nvpstr . " & " . $paypalprefix . " SHIPTONAME= " . urlencode ( $shipToName ); // SHIPTONAME deprecated by paypal -> PAYMENTREQUEST_n_SHIPTONAME
$nvpstr = $nvpstr . " & " . $paypalprefix . " SHIPTOSTREET= " . urlencode ( $shipToStreet ); //
$nvpstr = $nvpstr . " & " . $paypalprefix . " SHIPTOSTREET2= " . urlencode ( $shipToStreet2 );
$nvpstr = $nvpstr . " & " . $paypalprefix . " SHIPTOCITY= " . urlencode ( $shipToCity );
$nvpstr = $nvpstr . " & " . $paypalprefix . " SHIPTOSTATE= " . urlencode ( $shipToState );
$nvpstr = $nvpstr . " & " . $paypalprefix . " SHIPTOCOUNTRYCODE= " . urlencode ( $shipToCountryCode );
$nvpstr = $nvpstr . " & " . $paypalprefix . " SHIPTOZIP= " . urlencode ( $shipToZip );
$nvpstr = $nvpstr . " & " . $paypalprefix . " PHONENUM= " . urlencode ( $phoneNum );
2021-02-26 15:27:05 +01:00
if ( ! empty ( $email )) {
$nvpstr = $nvpstr . " & " . $paypalprefix . " EMAIL= " . urlencode ( $email ); // EMAIL deprecated by paypal -> PAYMENTREQUEST_n_EMAIL
}
if ( ! empty ( $desc )) {
$nvpstr = $nvpstr . " & " . $paypalprefix . " DESC= " . urlencode ( $desc ); // DESC deprecated by paypal -> PAYMENTREQUEST_n_DESC
}
2020-10-27 19:46:07 +01:00
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'PAYPAL_LOGOIMG' ) && $mysoc -> logo ) {
2020-10-27 19:46:07 +01:00
global $dolibarr_main_url_root ;
// Define $urlwithroot
$urlwithouturlroot = preg_replace ( '/' . preg_quote ( DOL_URL_ROOT , '/' ) . '$/i' , '' , trim ( $dolibarr_main_url_root ));
$urlwithroot = $urlwithouturlroot . DOL_URL_ROOT ; // This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
$urllogo = $urlwithroot . " /viewimage.php?modulepart=mycompany&file= " . urlencode ( 'logos/' . $mysoc -> logo );
$nvpstr = $nvpstr . " &LOGOIMG= " . urlencode ( $urllogo );
}
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'PAYPAL_BRANDNAME' )) {
2024-01-05 04:18:53 +01:00
$nvpstr = $nvpstr . " &BRANDNAME= " . urlencode ( getDolGlobalString ( 'PAYPAL_BRANDNAME' )); // BRANDNAME
2020-10-27 19:46:07 +01:00
}
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'PAYPAL_NOTETOBUYER' )) {
2024-01-05 04:18:53 +01:00
$nvpstr = $nvpstr . " &NOTETOBUYER= " . urlencode ( getDolGlobalString ( 'PAYPAL_NOTETOBUYER' )); // PAYPAL_NOTETOBUYER
2020-10-27 19:46:07 +01:00
}
2010-11-21 14:48:42 +01:00
2017-08-31 02:34:07 +02:00
$_SESSION [ " FinalPaymentAmt " ] = $paymentAmount ;
2020-10-27 19:46:07 +01:00
$_SESSION [ " currencyCodeType " ] = $currencyCodeType ;
$_SESSION [ " PaymentType " ] = $paymentType ; // 'Mark', 'Sole'
2021-10-25 22:07:31 +02:00
$_SESSION [ 'ipaddress' ] = getUserRemoteIP (); // Payer ip
2020-10-27 19:46:07 +01:00
//'---------------------------------------------------------------------------------------------------------------
//' Make the API call to PayPal
2024-01-12 18:06:50 +01:00
//' If the API call succeeded, then redirect the buyer to PayPal to begin to authorize payment.
//' If an error occurred, show the resulting errors
2020-10-27 19:46:07 +01:00
//'---------------------------------------------------------------------------------------------------------------
$resArray = hash_call ( " SetExpressCheckout " , $nvpstr );
$ack = strtoupper ( $resArray [ " ACK " ]);
2021-02-26 15:27:05 +01:00
if ( $ack == " SUCCESS " || $ack == " SUCCESSWITHWARNING " ) {
2020-10-27 19:46:07 +01:00
$token = urldecode ( $resArray [ " TOKEN " ]);
$_SESSION [ 'TOKEN' ] = $token ;
}
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
return $resArray ;
2010-11-21 14:48:42 +01:00
}
2011-10-03 18:10:50 +02:00
/**
* Prepares the parameters for the GetExpressCheckoutDetails API Call .
*
2024-11-04 12:32:13 +01:00
* @ param string $token Token
* @ return array < string , string > The NVP Collection object of the GetExpressCheckoutDetails Call Response .
2010-11-21 14:48:42 +01:00
*/
2012-01-22 20:33:45 +01:00
function getDetails ( $token )
2010-11-21 14:48:42 +01:00
{
2020-10-27 19:46:07 +01:00
//'--------------------------------------------------------------
//' At this point, the buyer has completed authorizing the payment
//' at PayPal. The function will call PayPal to obtain the details
2024-01-12 18:06:50 +01:00
//' of the authorization, including any shipping information of the
2020-10-27 19:46:07 +01:00
//' buyer. Remember, the authorization is not a completed transaction
//' at this state - the buyer still needs an additional step to finalize
//' the transaction
//'--------------------------------------------------------------
//declaring of global variables
global $conf , $langs ;
global $API_Endpoint , $API_Url , $API_version , $USE_PROXY , $PROXY_HOST , $PROXY_PORT ;
global $PAYPAL_API_USER , $PAYPAL_API_PASSWORD , $PAYPAL_API_SIGNATURE ;
//'---------------------------------------------------------------------------
//' Build a second API request to PayPal, using the token as the
//' ID to get the details on the payment authorization
//'---------------------------------------------------------------------------
$nvpstr = " &TOKEN= " . $token ;
//'---------------------------------------------------------------------------
//' Make the API call and store the results in an array.
//' If the call was a success, show the authorization details, and provide
//' an action to complete the payment.
//' If failed, show the error
//'---------------------------------------------------------------------------
$resArray = hash_call ( " GetExpressCheckoutDetails " , $nvpstr );
$ack = strtoupper ( $resArray [ " ACK " ]);
2021-02-26 15:27:05 +01:00
if ( $ack == " SUCCESS " || $ack == " SUCCESSWITHWARNING " ) {
2020-10-27 19:46:07 +01:00
$_SESSION [ 'payer_id' ] = $resArray [ 'PAYERID' ];
}
return $resArray ;
2010-11-21 14:48:42 +01:00
}
2011-05-04 17:19:48 +02:00
2011-10-03 18:10:50 +02:00
/**
* Validate payment
*
2012-01-22 20:33:45 +01:00
* @ param string $token Token
* @ param string $paymentType Type
* @ param string $currencyCodeType Currency
* @ param string $payerID Payer ID
* @ param string $ipaddress IP Address
* @ param string $FinalPaymentAmt Amount
2017-05-13 15:19:35 +02:00
* @ param string $tag Full tag
2024-11-04 12:32:13 +01:00
* @ return array < string , string >
2010-11-21 14:48:42 +01:00
*/
2012-01-22 20:33:45 +01:00
function confirmPayment ( $token , $paymentType , $currencyCodeType , $payerID , $ipaddress , $FinalPaymentAmt , $tag )
2010-11-21 14:48:42 +01:00
{
2020-10-27 19:46:07 +01:00
/* Gather the information to make the final call to
2021-02-26 15:27:05 +01:00
finalize the PayPal payment . The variable nvpstr
holds the name value pairs
*/
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
//declaring of global variables
global $conf , $langs ;
global $API_Endpoint , $API_Url , $API_version , $USE_PROXY , $PROXY_HOST , $PROXY_PORT ;
global $PAYPAL_API_USER , $PAYPAL_API_PASSWORD , $PAYPAL_API_SIGNATURE ;
$nvpstr = '' ;
$nvpstr .= '&TOKEN=' . urlencode ( $token );
$nvpstr .= '&PAYERID=' . urlencode ( $payerID );
$nvpstr .= '&PAYMENTACTION=' . urlencode ( $paymentType );
$nvpstr .= '&AMT=' . urlencode ( $FinalPaymentAmt );
$nvpstr .= '&CURRENCYCODE=' . urlencode ( $currencyCodeType );
$nvpstr .= '&IPADDRESS=' . urlencode ( $ipaddress );
$nvpstr .= '&INVNUM=' . urlencode ( $tag );
/* Make the call to PayPal to finalize payment
2024-01-12 18:06:50 +01:00
If an error occurred , show the resulting errors
2021-02-26 15:27:05 +01:00
*/
2020-10-27 19:46:07 +01:00
$resArray = hash_call ( " DoExpressCheckoutPayment " , $nvpstr );
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
/* Display the API response back to the browser .
2021-02-26 15:27:05 +01:00
If the response from PayPal was a success , display the response parameters '
If the response was an error , display the errors received using APIError . php .
*/
2020-10-27 19:46:07 +01:00
$ack = strtoupper ( $resArray [ " ACK " ]);
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
return $resArray ;
2010-11-21 14:48:42 +01:00
}
2011-10-03 18:10:50 +02:00
/**
* This function makes a DoDirectPayment API call
*
* paymentType : paymentType has to be one of the following values : Sale or Order or Authorization
* paymentAmount : total value of the shopping cart
* currencyCode : currency code value the PayPal API
* firstName : first name as it appears on credit card
* lastName : last name as it appears on credit card
* street : buyer ' s street address line as it appears on credit card
* city : buyer ' s city
* state : buyer ' s state
* countryCode : buyer ' s country code
* zip : buyer ' s zip
* creditCardType : buyer ' s credit card type ( i . e . Visa , MasterCard ... )
* creditCardNumber : buyers credit card number without any spaces , dashes or any other characters
* expDate : credit card expiration date
* cvv2 : Card Verification Value
2024-11-04 12:32:13 +01:00
* @ return array < string , string > The NVP Collection object of the DoDirectPayment Call Response .
2010-11-21 14:48:42 +01:00
*/
2011-11-30 18:48:04 +01:00
/*
2011-10-03 18:10:50 +02:00
function DirectPayment ( $paymentType , $paymentAmount , $creditCardType , $creditCardNumber , $expDate , $cvv2 , $firstName , $lastName , $street , $city , $state , $zip , $countryCode , $currencyCode , $tag )
2010-11-21 14:48:42 +01:00
{
2021-02-26 15:27:05 +01:00
//declaring of global variables
global $conf , $langs ;
global $API_Endpoint , $API_Url , $API_version , $USE_PROXY , $PROXY_HOST , $PROXY_PORT ;
global $PAYPAL_API_USER , $PAYPAL_API_PASSWORD , $PAYPAL_API_SIGNATURE ;
//Construct the parameter string that describes DoDirectPayment
$nvpstr = '' ;
$nvpstr = $nvpstr . " &AMT= " . urlencode ( $paymentAmount ); // deprecated by paypal
$nvpstr = $nvpstr . " &CURRENCYCODE= " . urlencode ( $currencyCode );
$nvpstr = $nvpstr . " &PAYMENTACTION= " . urlencode ( $paymentType ); // deprecated by paypal
$nvpstr = $nvpstr . " &CREDITCARDTYPE= " . urlencode ( $creditCardType );
$nvpstr = $nvpstr . " &ACCT= " . urlencode ( $creditCardNumber );
$nvpstr = $nvpstr . " &EXPDATE= " . urlencode ( $expDate );
$nvpstr = $nvpstr . " &CVV2= " . urlencode ( $cvv2 );
$nvpstr = $nvpstr . " &FIRSTNAME= " . urlencode ( $firstName );
$nvpstr = $nvpstr . " &LASTNAME= " . urlencode ( $lastName );
$nvpstr = $nvpstr . " &STREET= " . urlencode ( $street );
$nvpstr = $nvpstr . " &CITY= " . urlencode ( $city );
$nvpstr = $nvpstr . " &STATE= " . urlencode ( $state );
$nvpstr = $nvpstr . " &COUNTRYCODE= " . urlencode ( $countryCode );
$nvpstr = $nvpstr . " &IPADDRESS= " . getUserRemotIP ();
$nvpstr = $nvpstr . " &INVNUM= " . urlencode ( $tag );
$resArray = hash_call ( " DoDirectPayment " , $nvpstr );
return $resArray ;
2010-11-21 14:48:42 +01:00
}
2011-11-30 18:48:04 +01:00
*/
2010-11-21 14:48:42 +01:00
/**
* hash_call : Function to perform the API call to PayPal using API signature
2011-10-03 18:10:50 +02:00
*
* @ param string $methodName is name of API method .
* @ param string $nvpStr is nvp string .
2024-11-04 12:32:13 +01:00
* @ return array < string , string > returns an associative array containing the response from the server .
2010-11-21 14:48:42 +01:00
*/
2019-01-27 15:20:16 +01:00
function hash_call ( $methodName , $nvpStr )
2010-11-21 14:48:42 +01:00
{
2020-10-27 19:46:07 +01:00
//declaring of global variables
global $conf , $langs ;
global $API_Endpoint , $API_Url , $API_version , $USE_PROXY , $PROXY_HOST , $PROXY_PORT , $PROXY_USER , $PROXY_PASS ;
global $PAYPAL_API_USER , $PAYPAL_API_PASSWORD , $PAYPAL_API_SIGNATURE ;
2011-07-13 18:55:25 +02:00
2020-10-27 19:46:07 +01:00
// TODO problem with triggers
$API_version = " 98.0 " ;
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'PAYPAL_API_SANDBOX' ) || GETPOST ( 'forcesandbox' , 'alpha' )) { // We can force sand box with param 'forcesandbox'
2020-10-27 19:46:07 +01:00
$API_Endpoint = " https://api-3t.sandbox.paypal.com/nvp " ;
$API_Url = " https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token= " ;
2020-05-21 15:05:19 +02:00
} else {
2020-10-27 19:46:07 +01:00
$API_Endpoint = " https://api-3t.paypal.com/nvp " ;
$API_Url = " https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token= " ;
2011-05-26 14:36:56 +02:00
}
2011-07-13 18:55:25 +02:00
2011-05-26 14:36:56 +02:00
// Clean parameters
2024-12-11 20:44:48 +01:00
$PAYPAL_API_USER = getDolGlobalString ( 'PAYPAL_API_USER' );
$PAYPAL_API_PASSWORD = getDolGlobalString ( 'PAYPAL_API_PASSWORD' );
$PAYPAL_API_SIGNATURE = getDolGlobalString ( 'PAYPAL_API_SIGNATURE' );
$PAYPAL_API_SANDBOX = getDolGlobalString ( 'PAYPAL_API_SANDBOX' );
2011-05-26 14:36:56 +02:00
// TODO END problem with triggers
2011-07-13 18:55:25 +02:00
2020-10-27 19:46:07 +01:00
dol_syslog ( " Paypal API endpoint " . $API_Endpoint );
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
//setting the curl parameters.
$ch = curl_init ();
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
/* print $API_Endpoint . " - " . $API_version . " - " . $PAYPAL_API_USER . " - " . $PAYPAL_API_PASSWORD . " - " . $PAYPAL_API_SIGNATURE . " <br> " ;
2021-02-26 15:27:05 +01:00
print $USE_PROXY . " - " . $gv_ApiErrorURL . " <br> " ;
print $nvpStr ;
exit ; */
2020-10-27 19:46:07 +01:00
curl_setopt ( $ch , CURLOPT_URL , $API_Endpoint );
curl_setopt ( $ch , CURLOPT_VERBOSE , 1 );
// TLSv1 by default or change to TLSv1.2 in module configuration
2023-11-27 11:56:32 +01:00
curl_setopt ( $ch , CURLOPT_SSLVERSION , ( ! getDolGlobalString ( 'PAYPAL_SSLVERSION' ) ? 1 : $conf -> global -> PAYPAL_SSLVERSION ));
2010-11-21 14:48:42 +01:00
2022-02-05 19:42:43 +01:00
$ssl_verifypeer = - 1 ;
// Turning on or off the ssl target certificate
if ( $ssl_verifypeer < 0 ) {
global $dolibarr_main_prod ;
$ssl_verifypeer = ( $dolibarr_main_prod ? true : false );
}
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'MAIN_CURL_DISABLE_VERIFYPEER' )) {
2022-02-05 19:42:43 +01:00
$ssl_verifypeer = 0 ;
}
2020-10-27 19:46:07 +01:00
//turning off the server and peer verification(TrustManager Concept).
2022-02-05 19:42:43 +01:00
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , ( $ssl_verifypeer ? true : false ));
2022-02-09 15:29:21 +01:00
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , ( $ssl_verifypeer ? true : false ));
2010-11-21 14:48:42 +01:00
2024-12-11 20:44:48 +01:00
curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT , getDolGlobalInt ( 'MAIN_USE_CONNECT_TIMEOUT' , 5 ));
curl_setopt ( $ch , CURLOPT_TIMEOUT , getDolGlobalInt ( 'MAIN_USE_RESPONSE_TIMEOUT' , 30 ));
2013-12-07 03:23:14 +01:00
2020-10-27 19:46:07 +01:00
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
curl_setopt ( $ch , CURLOPT_POST , 1 );
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
//if USE_PROXY constant set to true in Constants.php, then only proxy will be enabled.
2021-02-26 15:27:05 +01:00
if ( $USE_PROXY ) {
2020-10-27 19:46:07 +01:00
dol_syslog ( " Paypal API hash_call set proxy to " . $PROXY_HOST . " : " . $PROXY_PORT . " - " . $PROXY_USER . " : " . $PROXY_PASS );
//curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Curl 7.10
curl_setopt ( $ch , CURLOPT_PROXY , $PROXY_HOST . " : " . $PROXY_PORT );
2021-02-26 15:27:05 +01:00
if ( $PROXY_USER ) {
curl_setopt ( $ch , CURLOPT_PROXYUSERPWD , $PROXY_USER . " : " . $PROXY_PASS );
}
2020-10-27 19:46:07 +01:00
}
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
//NVPRequest for submitting to server
$nvpreq = " METHOD= " . urlencode ( $methodName ) . " &VERSION= " . urlencode ( $API_version ) . " &PWD= " . urlencode ( $PAYPAL_API_PASSWORD ) . " &USER= " . urlencode ( $PAYPAL_API_USER ) . " &SIGNATURE= " . urlencode ( $PAYPAL_API_SIGNATURE ) . $nvpStr ;
$nvpreq .= " &LOCALECODE= " . strtoupper ( $langs -> getDefaultLang ( 1 ));
//$nvpreq.="&BRANDNAME=".urlencode(); // Override merchant name
//$nvpreq.="&NOTIFYURL=".urlencode(); // For Instant Payment Notification url
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
dol_syslog ( " Paypal API hash_call nvpreq= " . $nvpreq );
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
//setting the nvpreq as POST FIELD to curl
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $nvpreq );
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
//getting response from server
$response = curl_exec ( $ch );
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
$nvpReqArray = deformatNVP ( $nvpreq );
$_SESSION [ 'nvpReqArray' ] = $nvpReqArray ;
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
//convrting NVPResponse to an Associative Array
dol_syslog ( " Paypal API hash_call Response nvpresp= " . $response );
$nvpResArray = deformatNVP ( $response );
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
if ( curl_errno ( $ch )) {
// moving to display page to display curl errors
$_SESSION [ 'curl_error_no' ] = curl_errno ( $ch );
$_SESSION [ 'curl_error_msg' ] = curl_error ( $ch );
2010-11-21 14:48:42 +01:00
2023-12-13 12:46:23 +01:00
//Execute the Error handling module to display errors.
2020-10-27 19:46:07 +01:00
} else {
//closing the curl
curl_close ( $ch );
}
2010-11-21 14:48:42 +01:00
2020-10-27 19:46:07 +01:00
return $nvpResArray ;
2010-11-21 14:48:42 +01:00
}
2011-11-30 18:48:04 +01:00
2011-10-03 18:10:50 +02:00
/**
2010-11-21 14:48:42 +01:00
* This function will take NVPString and convert it to an Associative Array and it will decode the response .
2024-01-12 18:06:50 +01:00
* It is useful to search for a particular key and displaying arrays .
2011-10-03 18:10:50 +02:00
*
* @ param string $nvpstr NVPString
2024-11-04 12:32:13 +01:00
* @ return array < string , string > nvpArray = Associative Array
2010-11-21 14:48:42 +01:00
*/
function deformatNVP ( $nvpstr )
{
2024-01-12 18:06:50 +01:00
$initial = 0 ;
2020-10-27 19:46:07 +01:00
$nvpArray = array ();
2021-02-26 15:27:05 +01:00
while ( strlen ( $nvpstr )) {
2024-01-12 18:06:50 +01:00
//position of Key
2020-10-27 19:46:07 +01:00
$keypos = strpos ( $nvpstr , '=' );
//position of value
$valuepos = strpos ( $nvpstr , '&' ) ? strpos ( $nvpstr , '&' ) : strlen ( $nvpstr );
/*getting the Key and Value values and storing in a Associative Array*/
2024-01-12 18:06:50 +01:00
$keyval = substr ( $nvpstr , $initial , $keypos );
2020-10-27 19:46:07 +01:00
$valval = substr ( $nvpstr , $keypos + 1 , $valuepos - $keypos - 1 );
2024-01-12 18:06:50 +01:00
//decoding the response
2020-10-27 19:46:07 +01:00
$nvpArray [ urldecode ( $keyval )] = urldecode ( $valval );
$nvpstr = substr ( $nvpstr , $valuepos + 1 , strlen ( $nvpstr ));
}
return $nvpArray ;
2010-11-21 14:48:42 +01:00
}
2012-07-02 19:30:37 +02:00
/**
2012-03-19 17:18:11 +01:00
* Get API errors
*
2024-11-04 12:32:13 +01:00
* @ return string [] Array of errors
2012-07-02 19:30:37 +02:00
*/
function getApiError ()
{
2019-11-13 19:35:39 +01:00
$errors = array ();
2012-07-02 19:30:37 +02:00
2019-11-13 19:35:39 +01:00
$resArray = $_SESSION [ 'reshash' ];
2012-07-02 19:30:37 +02:00
2021-02-26 15:27:05 +01:00
if ( isset ( $_SESSION [ 'curl_error_no' ])) {
2012-07-02 19:30:37 +02:00
$errors [] = $_SESSION [ 'curl_error_no' ] . '-' . $_SESSION [ 'curl_error_msg' ];
}
2021-02-26 15:27:05 +01:00
foreach ( $resArray as $key => $value ) {
2012-07-02 19:30:37 +02:00
$errors [] = $key . '-' . $value ;
}
return $errors ;
2012-03-12 17:13:54 +01:00
}