如何使用php汇款到paypal

Posted

技术标签:

【中文标题】如何使用php汇款到paypal【英文标题】:How to send money to paypal using php 【发布时间】:2011-05-10 15:57:12 【问题描述】:

我已将客户的 paypal 电子邮件地址存储在数据库中,我想使用该电子邮件地址向他们汇款。我正在使用 php

请任何人建议如何做到这一点。

【问题讨论】:

您向客户汇款而不是收款似乎很奇怪。我想在你的名单上。 让我们从您尝试过的所有内容的列表开始?你至少谷歌了吗?如果你能在你被卡住的地方给出一个特定的问题,那就更好了。谢谢 @pinaki:谷歌不是一个答案,也不是对 SO 的帮助(大多数搜索无论如何都会回到这个问题)。但我同意你的观点,这个问题太笼统了,他应该带着一个具体的问题回来。 @Bobby:如果我在谷歌上搜索“php paypal 示例”,我会获得一些示例代码的 phpclasses 链接(第 1 页上的链接 3)。我没有检查代码的有效性,但我指的是 OP 所做的任何此类研究。这至少可以让我们缩小问题范围。 另外,你搜索过***.com/search?q=paypal+php吗? 【参考方案1】:

我正在寻找同样的问题,这对我有用。 在“沙盒”模式下测试并使用 NVP(而不是 SOAP)。 您的服务器必须支持 CURL,以便验证它使用:

<?php
echo 'curl extension/module loaded/installed: ';
echo ( !extension_loaded('curl')) ? 'no' : 'yes';
echo "<br />\n";
phpinfo(INFO_MODULES); // just to be sure
?>

如果未加载或安装,请询问您的主机管理员或get it here,否则请继续:

<?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 = 'PayPal payment';

/** 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('my_api_username');
 $API_Password = urlencode('my_api_password');
 $API_Signature = urlencode('my_api_signature');
 $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)
 
  exit("$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;


// Set request-specific fields.
$emailSubject = urlencode($vEmailSubject);
$receiverType = urlencode('EmailAddress');
$currency = urlencode('USD'); // 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' => "user1@paypal.com", 
    'amount' => "20.00",
    'uniqueID' => "id_001", // 13 chars max
    'note' => " payment of commissions"), // I recommend use of space at beginning of string.
  1 => array(
    'receiverEmail' => "user2@paypal.com",
    'amount' => "162.38",
    'uniqueID' => "A47-92w", // 13 chars max, available in 'My Account/Overview/Transaction details' when the transaction is made 
    'note' => " payoff of what I owed you"  // 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"]))

 exit('MassPay Completed Successfully: ' . print_r($httpParsedResponseAr, true));

else

 exit('MassPay failed: ' . print_r($httpParsedResponseAr, true));


?>

祝你好运!

【讨论】:

也许我忘了提到我是从 PayPal 的示例代码中做的,或者可能是第一个注释行有这样的话:'code modified from source'。我不想给 Deepak 或其他人只是官方文档的链接,我给了他一个有用的代码。顺便说一下,我修改了官方的示例代码,因为原来的支付过程做了3次,也就是说:你要发送10块钱,代码花了30块,没有警告。 我已经成功完成了masspay,但它没有发送电子邮件.. 应该这样吗?难道我做错了什么? - 贝宝开发不能更令人沮丧 我只在沙盒模式下进行了测试,一切都很顺利,直到我向新的测试账户付款。我在发件人的测试电子邮件中收到了收据,但收件人没有...直到我验证了它。你是在沙盒还是实时模式下发送的? 感谢您提供的示例代码,PayPal 几乎无法找到它。至少,我很难找到它...... 谢谢,这应该被接受为答案。【参考方案2】:

您要查找的是来自 PayPal 官方代码示例的 DoMassPay,这个名字不容易猜到:P

【讨论】:

【参考方案3】:

他们在自己的网站上添加了示例代码:

https://cms.paypal.com/cms_content/US/en_US/files/developer/nvp_MassPay_php.txt

【讨论】:

以上是关于如何使用php汇款到paypal的主要内容,如果未能解决你的问题,请参考以下文章

如何汇款到任何贝宝账户

使用 Paypal php rest SDK 汇款

如何使用 paypal API 从汇款交易中自动收取佣金?

通过 OmniPay PHP 进行 PayPal 汇款

使用 PHP 自动执行 PayPal 付款

使用 Paypal Express Checkout 进行汇款