2010-11-16 47 views
6

Tôi đã lưu trữ địa chỉ email paypal của khách hàng trong cơ sở dữ liệu và tôi muốn gửi tiền cho họ bằng địa chỉ email đó. Tôi đang sử dụng PHP.Cách gửi tiền tới paypal bằng cách sử dụng php

Bất kỳ ai cũng vui lòng đề xuất cách thực hiện điều đó.

+13

có vẻ lạ rằng bạn đang gửi tiền cho khách hàng thay vì thu tiền. tôi muốn có mặt trong danh sách của bạn. – stillstanding

+0

Hãy bắt đầu với một danh sách những gì bạn đã thử? Bạn có google atleast? Nó là tốt hơn nếu bạn có thể đưa ra một vấn đề cụ thể nơi bạn đang mắc kẹt. Cảm ơn – pinaki

+0

@pinaki: Google không phải là câu trả lời cũng không hữu ích đối với SO (hầu hết các tìm kiếm đều dẫn đầu về câu hỏi này). Nhưng tôi đồng ý với bạn rằng câu hỏi quá rộng và anh ấy nên quay trở lại với một vấn đề cụ thể. – Bobby

Trả lời

11

Tôi đang tìm kiếm cùng một vấn đề, dưới đây là những gì đang hoạt động đối với tôi. Thử nghiệm ở chế độ 'sandbox' và sử dụng NVP (thay vì SOAP). Máy chủ của bạn phải hỗ trợ CURL, để xác minh nó sử dụng:

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

Nếu không được tải hoặc cài đặt yêu cầu hostmaster hoặc get it here bạn, nếu không đi trước:

<?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' => "[email protected]", 
    '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' => "[email protected]", 
    '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)); 
} 

?> 

Chúc may mắn!

+1

Tại sao, trên Trái đất, sử dụng một số ví dụ không rõ ràng về nguồn gốc không xác định từ bảng thông báo ẩn danh, thay vì mã ** từ tài liệu chính thức ** ?! –

+6

Có lẽ tôi quên đề cập đến việc tôi đã làm điều đó từ mã mẫu của PayPal, hoặc có thể là dòng nhận xét đầu tiên có các từ: 'mã được sửa đổi từ nguồn'. Tôi không muốn đưa cho Deepak hoặc những người khác chỉ là một liên kết đến tài liệu chính thức, tôi đã cho anh ta một mã số hữu ích. Nhân tiện, tôi đã sửa đổi mã mẫu chính thức vì quá trình thanh toán ban đầu được thực hiện ba lần, điều đó có nghĩa là: bạn muốn gửi 10 đô la và mã đó chi tiêu 30, mà không có cảnh báo. – quantme

+4

quantme 1 Col. Shrapnel 0 –

1

gì bạn đang tìm kiếm là DoMassPay từ mẫu mã chính thức PayPal, không dễ dàng để đoán rằng tên: P

Các vấn đề liên quan