paypal批量支付api在php中的使用

Posted

技术标签:

【中文标题】paypal批量支付api在php中的使用【英文标题】:paypal mass payment api use in php 【发布时间】:2015-09-20 22:10:55 【问题描述】:

我正在尝试通过此代码使用 Paypal Mass Payment API,但出现此错误。

我将此代码保存到文件“MassPay.php”并上传到我的本地主机。我正在尝试测试代码,如果我可以让它工作,我将在核心 php 中使用它,每月向教师支付大量费用。

Array
(
    [TIMESTAMP] => 2015-07-03T06:55:19Z
    [CORRELATIONID] => 437cfd2eedc02
    [ACK] => Failure
    [VERSION] => 74.0
    [BUILD] => 17235934
    [L_ERRORCODE0] => 10004
    [L_SHORTMESSAGE0] => Transaction refused because of an invalid argument. See additional error messages for details.
    [L_LONGMESSAGE0] => The number of input records is less than or equal to zero
    [L_SEVERITYCODE0] => Error
    [ERRORS] => Array
        (
            [0] => Array
                (
                    [L_ERRORCODE] => 10004
                    [L_SHORTMESSAGE] => Transaction refused because of an invalid argument. See additional error messages for details.
                    [L_LONGMESSAGE] => The number of input records is less than or equal to zero
                    [L_SEVERITYCODE] => Error
                )

        )

    [REQUESTDATA] => Array
        (
            [USER] => dudhat-facilitator_api1.artoongames.com
            [PWD] => AXAT8HSZRR4XANFZ
            [VERSION] => 74.0
            [BUTTONSOURCE] => AngellEYE_PHPClass
            [SIGNATURE] => AFcWxV21C7fd0v3bYYYRCps-s-rl31Ab1QV5bWA6uEZ.6emUAkZaZEhjiA
            [METHOD] => MassPay
            [EMAILSUBJECT] => dudhat@artoongames.com
            [CURRENCYCODE] => USD
            [RECEIVERTYPE] => EmailAddress
        )

    [RAWREQUEST] => USER=dudhat-facilitator_api1.artoongames.com&PWD=AXAT8HSZRR4XANFZ&VERSION=74.0&BUTTONSOURCE=AngellEYE_PHPClass&SIGNATURE=AFcWxV21C7fd0v3bYYYRCps-s-rl31Ab1QV5bWA6uEZ.6emUAkZaZEhjiA&METHOD=MassPay&EMAILSUBJECT=dudhat%40artoongames.com&CURRENCYCODE=USD&RECEIVERTYPE=EmailAddress
    [RAWRESPONSE] => TIMESTAMP=2015%2d07%2d03T06%3a55%3a19Z&CORRELATIONID=437cfd2eedc02&ACK=Failure&VERSION=74%2e0&BUILD=17235934&L_ERRORCODE0=10004&L_SHORTMESSAGE0=Transaction%20refused%20because%20of%20an%20invalid%20argument%2e%20See%20additional%20error%20messages%20for%20details%2e&L_LONGMESSAGE0=The%20number%20of%20input%20records%20is%20less%20than%20or%20equal%20to%20zero&L_SEVERITYCODE0=Error
)

使用此代码“MassPay.php”

<?php

// Include required library files.
require_once('includes/config.php');
require_once('includes/paypal.class.php');

// Create PayPal object.
$PayPalConfig = array('Sandbox' => $sandbox, 'APIUsername' => $api_username, 'APIPassword' => $api_password, 'APISignature' => $api_signature);
$PayPal = new PayPal($PayPalConfig);

// Prepare request arrays
$MPFields = array(
    'emailsubject' => 'dudhat@artoongames.com', // The subject line of the email that PayPal sends when the transaction is completed.  Same for all recipients.  255 char max.
    'currencycode' => 'USD', // Three-letter currency code.
    'receivertype' => 'EmailAddress'       // Indicates how you identify the recipients of payments in this call to MassPay.  Must be EmailAddress or UserID
);

// Typically, you'll loop through some sort of records to build your MPItems array. 
// Here I simply include 3 items individually.  

$Item1 = array(
    'l_email' => 'dudhat@gmail.com', // Required.  Email address of recipient.  You must specify either L_EMAIL or L_RECEIVERID but you must not mix the two.
    'l_receiverid' => 'dudhat@gmail.com', // Required.  ReceiverID of recipient.  Must specify this or email address, but not both.
    'l_amt' => '10.00', // Required.  Payment amount.
    'l_uniqueid' => '522', // Transaction-specific ID number for tracking in an accounting system.
    'l_note' => 'test payment'         // Custom note for each recipient.
);

$Item2 = array(
    'l_email' => 'dilip@gmail.com', // Required.  Email address of recipient.  You must specify either L_EMAIL or L_RECEIVERID but you must not mix the two.
    'l_receiverid' => 'dilip@gmail.com', // Required.  ReceiverID of recipient.  Must specify this or email address, but not both.
    'l_amt' => '1.00', // Required.  Payment amount.
    'l_uniqueid' => '523', // Transaction-specific ID number for tracking in an accounting system.
    'l_note' => 'test payment'         // Custom note for each recipient.
);



$MPItems = array($Item1,$Item2);  // etc

$PayPalRequestData = array('MPFields' => $MPFields, 'MPItems' => $MPFields);

// Pass data into class for processing with PayPal and load the response array into $PayPalResult
$PayPalResult = $PayPal->MassPay($PayPalRequestData);

// Write the contents of the response array to the screen for demo purposes.
echo '<pre />';
print_r($PayPalResult);
?>

【问题讨论】:

【参考方案1】:

您在 $MPItems 中组装了一些付款项目,但没有将 $MPItems 添加到 PayPal 请求数据中。因此 PayPal 拒绝了批量付款,因为其中不包含任何付款项目。

【讨论】:

【参考方案2】:

您可以使用以下类进行批量支付交易

使用模式(live/sandbox)的实际您的贝宝凭据定义您的常量。

class PaypalnvpComponent 

    private static $API_Username = PAYPAL_API_USERNAME;
    private static $API_Password = PAYPAL_API_PASSWORD;
    private static $API_Signature = PAYPAL_API_SIGNATURE;
    private static $API_Environment = PAYPAL_API_MODE;
    private static $API_Version = '116.0';

    public static function Call($methodName,$params)
        if(self::$API_Environment == 'LIVE')
            $API_Endpoint = "https://api-3t.paypal.com/nvp";            
        else
            $API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp";
        

        $nvpstr = "";
        foreach($params as $k=>$v)
            $nvpstr .="&".$k."=".urlencode($v);
        

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);

        //Turn of server and pakagemanager
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);

        //set the API operation,version,API signature in requrest

        $nvpreq ="";
        $nvpreq .= "METHOD=".urlencode($methodName);
        $nvpreq .= "&VERSION=".urlencode(self::$API_Version);
        $nvpreq .= "&PWD=".urlencode(self::$API_Password);
        $nvpreq .= "&USER=".urlencode(self::$API_Username);
        $nvpreq .= "&SIGNATURE=".urlencode(self::$API_Signature);
        $nvpreq .= $nvpstr;

        //set the request as POST field for curl
        curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);

        //get the response from server
        $httpResponse = curl_exec($ch);

        if(!$httpResponse)
            return "$methodName failed:".curl_error($ch).'('.curl_errno($ch).')';
        

        //Extract the response details

        $httpResponseArray = explode('&', $httpResponse);

        $httpParsedResponseArray = array();

        foreach ($httpResponseArray as $i=>$value)
            $tmpArray = explode('=', $value);
            if(sizeof($tmpArray) > 1)
                $httpParsedResponseArray[$tmpArray[0]] = urldecode($tmpArray[1]);
            
        
        if((0 == sizeof($httpParsedResponseArray)) || !array_key_exists('ACK',$httpParsedResponseArray))
            return "Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.";

        
        return $httpParsedResponseArray;        
    


    #----------------------------
    # @$hok
    # mass payment with multiple users in one go
    # paypal transaction status will be : Completed, Failed, Returned, Reversed, Unclaimed, Pending, Blocked
    #----------------------------
    public static function MassPay($params)
        $methodName = "MassPay";        
        return self::Call($methodName, $params);
    

    public static function GetTransactionDetail($params)
        $methodName = "GetTransactionDetails";        
        return self::Call($methodName, $params);        
    

    public static function TransactionSearch($params)
        $methodName = "TransactionSearch";        
        return self::Call($methodName, $params);        
    


# below is the example how to use above class
$params = array();
$params["RECEIVERTYPE"] = "EmailAddress";
$params["EMAILSUBJECT"] = "Your withdraw request was processed";

$params["L_AMT0"] = 1;
$params["L_EMAIL0"] = "xyz@xyz.abc"; 
$params["L_UNIQUEID0"] = rand(11111,9999999);

$params["L_AMT1"] = 2;
$params["L_EMAIL1"] = "hamed-buyer@lifeofu.com"; 
$params["L_UNIQUEID1"] = rand(11111,9999999);

$params["L_AMT2"] = 3;
$params["L_EMAIL2"] = "testuser1@test1.com"; 
$params["L_UNIQUEID2"] = rand(11111,9999999);

$params["CURRENCYCODE"] = "USD";       

$result = PaypalnvpComponent::MassPay($params);


//transaction detial  
$params = array();
$params["TRANSACTIONID"] = TRANSACTIONID_HERE; 
$result = PaypalnvpComponent::GetTransactionDetail($params);

【讨论】:

以上是关于paypal批量支付api在php中的使用的主要内容,如果未能解决你的问题,请参考以下文章

Paypal 批量支付在节点 js api 中不起作用

我不断收到关于 paypal php 自适应支付 API 支付的错误“您的支付无法完成......”

REST api 中的 Paypal 自适应支付

PayPal 自适应支付 API - 延迟链支付(PHP、JavaScript)

Paypal 从 php 中的支付密钥获取交易详情

PayPal 支付 REST API - 可用性?