【问题标题】:Paypal Mass Payment NVP API -Paypal Mass Payment NVP API -
【发布时间】:2014-11-11 15:56:46
【问题描述】:

我正在尝试将此代码与 Paypal Mass Payment API 一起使用,但我没有得到任何答复。不是错误消息或成功消息,只是空白屏幕。

我将此代码保存到文件“paypal.php”并上传到我的服务器。我正在尝试测试代码,如果我可以让它工作,我将在 Wordpress 中使用它,每月向 LMS 的教师支付大量费用。

<?php
// code modified from source: https://cms.paypal.com/cms_content/US/en_US/files/developer/nvp_MassPay_php.txt
// documentation: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_api_masspay
// sample code: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/library_code

// eMail subject to receivers
$vEmailSubject = 'Pagamento Paypal';

/** MassPay NVP example.
 *
 *  Pay one or more recipients. 
*/

// For testing environment: use 'sandbox' option. Otherwise, use 'live'.
// Go to www.x.com (PayPal Integration center) for more information.
$environment = 'sandbox'; // or 'beta-sandbox' or 'live'.

/**
 * Send HTTP POST Request
 *
 * @param string The API method name
 * @param string The POST Message fields in &name=value pair format
 * @return array Parsed HTTP Response body
 */
function PPHttpPost($methodName_, f_)
{
 global $environment;

 // Set up your API credentials, PayPal end point, and API version.
 // How to obtain API credentials:
 // https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_NVPAPIBasics#id084E30I30RO
 $API_UserName = urlencode('****');
 $API_Password = urlencode('***');
 $API_Signature = urlencode('***');
 $API_Endpoint = "https://api-3t.paypal.com/nvp";
 if("sandbox" === $environment || "beta-sandbox" === $environment)
 {
  $API_Endpoint = "https://api-3t.$environment.paypal.com/nvp";
 }
 $version = urlencode('51.0');

 // Set the curl parameters.
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
 curl_setopt($ch, CURLOPT_VERBOSE, 1);

 // Turn off the server and peer verification (TrustManager Concept).
 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, and API signature in the request.
 $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";

 // Set the request as a POST FIELD for curl.
 curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);

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

 if( !$httpResponse)
 {
  echo $methodName_ . ' failed: ' . curl_error($ch) . '(' . curl_errno($ch) .')');
 }

 // Extract the response details.
 $httpResponseAr = explode("&", $httpResponse);

 $httpParsedResponseAr = array();
 foreach ($httpResponseAr as $i => $value)
 {
  $tmpAr = explode("=", $value);
  if(sizeof($tmpAr) > 1)
  {
   $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
  }
 }

 if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr))
 {
  exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
 }

 return $httpParsedResponseAr;
}

echo $httpParsedResponseAr;
// Set request-specific fields.
$emailSubject = urlencode($vEmailSubject);
$receiverType = urlencode('EmailAddress');
$currency = urlencode('BRL'); // or other currency ('GBP', 'EUR', 'JPY', 'CAD', 'AUD')

// Receivers
// Use '0' for a single receiver. In order to add new ones: (0, 1, 2, 3...)
// Here you can modify to obtain array data from database.
$receivers = array(
  0 => array(
    'receiverEmail' => "ricardoglrj@yahoo.com.br", 
    'amount' => "20.00",
    'uniqueID' => "id_001", // 13 chars max
    'note' => " pagamento de comissão Fashiontuts"), // I recommend use of space at beginning of string.
  1 => array(
    'receiverEmail' => "ricardo@brgweb.com.br",
    'amount' => "162.38",
    'uniqueID' => "id_002", // 13 chars max, available in 'My Account/Overview/Transaction details' when the transaction is made 
    'note' => " pagamento de comissão fashiontuts"  // space again at beginning.
  )
);
$receiversLenght = count($receivers);

// Add request-specific fields to the request string.
$nvpStr="&EMAILSUBJECT=$emailSubject&RECEIVERTYPE=$receiverType&CURRENCYCODE=$currency";

$receiversArray = array();

for($i = 0; $i < $receiversLenght; $i++)
{
 $receiversArray[$i] = $receivers[$i];
}

foreach($receiversArray as $i => $receiverData)
{
 $receiverEmail = urlencode($receiverData['receiverEmail']);
 $amount = urlencode($receiverData['amount']);
 $uniqueID = urlencode($receiverData['uniqueID']);
 $note = urlencode($receiverData['note']);
 $nvpStr .= "&L_EMAIL$i=$receiverEmail&L_Amt$i=$amount&L_UNIQUEID$i=$uniqueID&L_NOTE$i=$note";
}

// Execute the API operation; see the PPHttpPost function above.
$httpParsedResponseAr = PPHttpPost('MassPay', $nvpStr);

if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
{
 echo 'MassPay Completed Successfully: ' . $httpParsedResponseAr;
}
else
{
 echo 'MassPay failed: ' . $httpParsedResponseAr;
}

?>

原始代码中正确提供了变量 $API_UserName、$API_Password、$API_Signature。

【问题讨论】:

    标签: php paypal


    【解决方案1】:

    代码似乎有些问题。我已经纠正了一些错误,您可以使用以下代码:

    <?php
    // code modified from source: https://cms.paypal.com/cms_content/US/en_US/files/developer/nvp_MassPay_php.txt
    // documentation: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_api_masspay
    // sample code: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/library_code
    
    // eMail subject to receivers
    $vEmailSubject = 'Pagamento Paypal';
    
    /** MassPay NVP example.
     *
     *  Pay one or more recipients. 
    */
    
    // For testing environment: use 'sandbox' option. Otherwise, use 'live'.
    // Go to www.x.com (PayPal Integration center) for more information.
    $environment = 'sandbox'; // or 'beta-sandbox' or 'live'.
    
    /**
     * Send HTTP POST Request
     *
     * @param string The API method name
     * @param string The POST Message fields in &name=value pair format
     * @return array Parsed HTTP Response body
     */
    function PPHttpPost($methodName_, $nvpStr_)
    {
     global $environment;
    
     // Set up your API credentials, PayPal end point, and API version.
     // How to obtain API credentials:
     // https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_NVPAPIBasics#id084E30I30RO
     $API_UserName = urlencode('XXXXXXXXXXX');
     $API_Password = urlencode('XXXXXXXXXXX');
     $API_Signature = urlencode('XXXXXXXXXXX');
     $API_Endpoint = "https://api-3t.paypal.com/nvp";
     if("sandbox" === $environment || "beta-sandbox" === $environment)
     {
      $API_Endpoint = "https://api-3t.$environment.paypal.com/nvp";
     }
     $version = urlencode('51.0');
    
     // Set the curl parameters.
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
     curl_setopt($ch, CURLOPT_VERBOSE, 1);
    
     // Turn off the server and peer verification (TrustManager Concept).
     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, and API signature in the request.
    
     $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";
    
     // Set the request as a POST FIELD for curl.
     curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq."&".$nvpStr_);
    
     // Get response from the server.
     $httpResponse = curl_exec($ch);
    
     if( !$httpResponse)
     {
      echo $methodName_ . ' failed: ' . curl_error($ch) . '(' . curl_errno($ch) .')';
     }
    
     // Extract the response details.
     $httpResponseAr = explode("&", $httpResponse);
    
     $httpParsedResponseAr = array();
     foreach ($httpResponseAr as $i => $value)
     {
      $tmpAr = explode("=", $value);
      if(sizeof($tmpAr) > 1)
      {
       $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
      }
     }
    
     if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr))
     {
      exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
     }
    print_r($httpParsedResponseAr);
    
     return $httpParsedResponseAr;
    }
    
    // Set request-specific fields.
    $emailSubject = urlencode($vEmailSubject);
    $receiverType = urlencode('EmailAddress');
    $currency = urlencode('BRL'); // or other currency ('GBP', 'EUR', 'JPY', 'CAD', 'AUD')
    
    // Receivers
    // Use '0' for a single receiver. In order to add new ones: (0, 1, 2, 3...)
    // Here you can modify to obtain array data from database.
    $receivers = array(
      0 => array(
        'receiverEmail' => "ricardoglrj@yahoo.com.br", 
        'amount' => "0.01",
        'uniqueID' => "id_001", // 13 chars max
        'note' => " pagamento de comissão Fashiontuts"), // I recommend use of space at beginning of string.
      1 => array(
        'receiverEmail' => "ricardo@brgweb.com.br",
        'amount' => "0.02",
        'uniqueID' => "id_002", // 13 chars max, available in 'My Account/Overview/Transaction details' when the transaction is made 
        'note' => " pagamento de comissão fashiontuts"  // space again at beginning.
      )
    );
    $receiversLenght = count($receivers);
    
    // Add request-specific fields to the request string.
    $nvpStr="&EMAILSUBJECT=$emailSubject&RECEIVERTYPE=$receiverType&CURRENCYCODE=$currency";
    
    $receiversArray = array();
    
    for($i = 0; $i < $receiversLenght; $i++)
    {
     $receiversArray[$i] = $receivers[$i];
    }
    
    foreach($receiversArray as $i => $receiverData)
    {
     $receiverEmail = urlencode($receiverData['receiverEmail']);
     $amount = urlencode($receiverData['amount']);
     $uniqueID = urlencode($receiverData['uniqueID']);
     $note = urlencode($receiverData['note']);
     $nvpStr .= "&L_EMAIL$i=$receiverEmail&L_Amt$i=$amount&L_UNIQUEID$i=$uniqueID&L_NOTE$i=$note";
    }
    
    // Execute the API operation; see the PPHttpPost function above.
    $httpParsedResponseAr = PPHttpPost('MassPay', $nvpStr);
    
    if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
    {
     echo 'MassPay Completed Successfully: ' . $httpParsedResponseAr;
    }
    else
    {
     echo '\nMassPay failed: ';
     print_r($httpParsedResponseAr);
    }
    
    ?>
    

    【讨论】:

    • Hello @Eshan 它在单个 php 文件中工作,但是当我尝试将其包含在另一个文件中(在 Wordpress 插件中)时,我收到以下 10002 安全错误。凭据很好,因为当我直接访问原始文件时它们可以工作...
    • 好的,找到了!这是全球 $enviroment ......刚刚摆脱它,现在它正在工作。谢谢
    • 你好@Ehsan 我也在尝试使用masspay api。并找到了这段代码。你能告诉我如何使用这个代码函数 PPHttpPost($methodName_, $nvpStr_)
    猜你喜欢
    • 2014-11-24
    • 2019-01-15
    • 2017-07-08
    • 2016-03-05
    • 2013-07-17
    • 2010-10-17
    • 2013-04-17
    • 2012-12-20
    • 2011-06-27
    相关资源
    最近更新 更多