如何从发票 ID 获取 PayPal 交易 ID

发布于 2024-12-22 19:05:46 字数 913 浏览 2 评论 0原文

我与一个电子商务网站合作,该网站使用用 PHP 编写的 PayPal 结账组件。出于会计目的,我想使用 PayPal PHP SOAP API 检索一些附加信息。

我发现了如何使用交易 ID 和 GetTransactionDetails 对象访问交易:

// snip - include PayPal libraries and set up APIProfile object -

$trans_details =& PayPal::getType('GetTransactionDetailsRequestType');
$tran_id = $_GET['transactionID'];
$trans_details->setTransactionId($tran_id, 'iso-8859-1');
$caller =& PayPal::getCallerServices($profile);
$response = $caller->GetTransactionDetails($trans_details);
$paymentTransDetails = $response->getPaymentTransactionDetails();

// snip - work with transaction details -

但是,我需要增强这一点,以便我可以首先使用本地可用的发票 ID 找到 12 个字符的字符串交易 ID MySQL 数据库(PayPal 网站上的交易中也引用了该数据库)。

我想我必须使用 交易搜索 ,但我不知道如何使用 PHP SOAP API 来做到这一点。如何检索发票 ID 的交易 ID?

I work with an e-commerce website that uses a PayPal checkout component written in PHP. For accounting purposes I want to retrieve some additional information using the PayPal PHP SOAP API.

I found out how to access the transaction using the transaction id and the GetTransactionDetails object:

// snip - include PayPal libraries and set up APIProfile object -

$trans_details =& PayPal::getType('GetTransactionDetailsRequestType');
$tran_id = $_GET['transactionID'];
$trans_details->setTransactionId($tran_id, 'iso-8859-1');
$caller =& PayPal::getCallerServices($profile);
$response = $caller->GetTransactionDetails($trans_details);
$paymentTransDetails = $response->getPaymentTransactionDetails();

// snip - work with transaction details -

However, I need to enhance this so that I can find out the 12-character string transaction id first by using the invoice id which I have available in a local MySQL database (which is also referenced in the transaction on the PayPal website).

I guess that I have to use Transaction Search for that but I don't know how to do this with the PHP SOAP API. How can I retrieve the transaction id for an invoice id?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上得阳光 2024-12-29 19:05:46

我深入研究了 API 文档并设法找到了答案。

// snip - include PayPal libraries and set up APIProfile object (variable: profile) - 

$trans_search =& PayPal::getType('TransactionSearchRequestType');

// 01/12/201 as an example date, we always need a start date for the API
$start_date_str = '01/12/2011'; 
$start_time = strtotime($start_date_str);
$iso_start = date('Y-m-d\T00:00:00\Z', $start_time);
$trans_search->setStartDate($iso_start, 'iso-8859-1');

$invoice_ID = '10942456';  // here we insert the invoice ID we know
$trans_search->setInvoiceID($invoice_ID);

$caller =& PayPal::getCallerServices($profile);

$response = $caller->TransactionSearch($trans_search); // execute search

$ptsr = $response->getPaymentTransactions();
$nrecs = sizeof($ptsr);
$ack = $response->getAck();

if( ($ack != ACK_SUCCESS) 
    && ($ack != ACK_SUCCESS_WITH_WARNING) ) 
    exit; // jump out on error

if($nrecs == 1){ // check whether we found only one transaction (as expected)
    $paymentTransaction = $ptsr[0];
    // we found our transaction ID
    $transID = $paymentTransaction->getTransactionID();  
}else{
    // invoice ID not unique?! :-(
    exit('Found multiple transactions: '. print_r($ptsr, true)); // jump out        
}

// snip - work with transaction ID - 

I dived into the API documentation and managed to find it out.

// snip - include PayPal libraries and set up APIProfile object (variable: profile) - 

$trans_search =& PayPal::getType('TransactionSearchRequestType');

// 01/12/201 as an example date, we always need a start date for the API
$start_date_str = '01/12/2011'; 
$start_time = strtotime($start_date_str);
$iso_start = date('Y-m-d\T00:00:00\Z', $start_time);
$trans_search->setStartDate($iso_start, 'iso-8859-1');

$invoice_ID = '10942456';  // here we insert the invoice ID we know
$trans_search->setInvoiceID($invoice_ID);

$caller =& PayPal::getCallerServices($profile);

$response = $caller->TransactionSearch($trans_search); // execute search

$ptsr = $response->getPaymentTransactions();
$nrecs = sizeof($ptsr);
$ack = $response->getAck();

if( ($ack != ACK_SUCCESS) 
    && ($ack != ACK_SUCCESS_WITH_WARNING) ) 
    exit; // jump out on error

if($nrecs == 1){ // check whether we found only one transaction (as expected)
    $paymentTransaction = $ptsr[0];
    // we found our transaction ID
    $transID = $paymentTransaction->getTransactionID();  
}else{
    // invoice ID not unique?! :-(
    exit('Found multiple transactions: '. print_r($ptsr, true)); // jump out        
}

// snip - work with transaction ID - 
合久必婚 2024-12-29 19:05:46

使用 TransactionSearch API 按发票编号查找交易非常简单。您所需要做的就是将 API 调用中的 INVNUM 参数发送到 PayPal。

例如(基于 PayPal 的 TransactionSearch PHP 示例代码):

<?php

/** TransactionSearch NVP example; last modified 08MAY23.
 *
 *  Search your account history for transactions that meet the criteria you specify. 
*/

$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.
    $API_UserName = urlencode('xxxxxxxxxxxx');
    $API_Password = urlencode('yyyyyyyyy');
    $API_Signature = urlencode('zzzzzzzzzzzzzzzzzzzzzzz');
    $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('84.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.
//$transactionID = urlencode('example_transaction_id');
$invoice = urlencode('1234');

// Add request-specific fields to the request string.
//$nvpStr = "&TRANSACTIONID=$transactionID";
$nvpStr = "&INVNUM=$invoice";

此处,通过设置适当的 STARTDATE:

// Set additional request-specific fields and add them to the request string.
$startDateStr = "01/01/2010";            // in 'mm/dd/ccyy' format
$endDateStr;            // in 'mm/dd/ccyy' format
if(isset($startDateStr)) {
   $start_time = strtotime($startDateStr);
   $iso_start = date('Y-m-d\T00:00:00\Z',  $start_time);
   $nvpStr .= "&STARTDATE=$iso_start";
  }

if(isset($endDateStr)&&$endDateStr!='') {
   $end_time = strtotime($endDateStr);
   $iso_end = date('Y-m-d\T24:00:00\Z', $end_time);
   $nvpStr .= "&ENDDATE=$iso_end";
}

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

此处,通过添加一个简单的 if 语句来触发是否返回完整的 TransactionSearch API 结果,或仅返回 PayPal 交易 ID (bla.php ?视图=最小):

if($_GET['view'] != "minimal") {
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) {
    echo('TransactionSearch Completed Successfully: '.print_r($httpParsedResponseAr, true));
} else  {
    echo('TransactionSearch failed: ' . print_r($httpParsedResponseAr, true));
}
}
else {
// Output only the TransactionID
echo $httpParsedResponseAr['L_TRANSACTIONID0'];
}


?>

It's easy enough to use the TransactionSearch API to find a transaction by invoice number. All you need to do is send the INVNUM parameter along in the API call to PayPal.

For example (based on PayPal's TransactionSearch PHP sample code):

<?php

/** TransactionSearch NVP example; last modified 08MAY23.
 *
 *  Search your account history for transactions that meet the criteria you specify. 
*/

$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.
    $API_UserName = urlencode('xxxxxxxxxxxx');
    $API_Password = urlencode('yyyyyyyyy');
    $API_Signature = urlencode('zzzzzzzzzzzzzzzzzzzzzzz');
    $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('84.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;
}

Up to here it's all as usual. The only changes to the sample are here:

// Set request-specific fields.
//$transactionID = urlencode('example_transaction_id');
$invoice = urlencode('1234');

// Add request-specific fields to the request string.
//$nvpStr = "&TRANSACTIONID=$transactionID";
$nvpStr = "&INVNUM=$invoice";

Here, by setting a proper STARTDATE:

// Set additional request-specific fields and add them to the request string.
$startDateStr = "01/01/2010";            // in 'mm/dd/ccyy' format
$endDateStr;            // in 'mm/dd/ccyy' format
if(isset($startDateStr)) {
   $start_time = strtotime($startDateStr);
   $iso_start = date('Y-m-d\T00:00:00\Z',  $start_time);
   $nvpStr .= "&STARTDATE=$iso_start";
  }

if(isset($endDateStr)&&$endDateStr!='') {
   $end_time = strtotime($endDateStr);
   $iso_end = date('Y-m-d\T24:00:00\Z', $end_time);
   $nvpStr .= "&ENDDATE=$iso_end";
}

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

And here, by adding a simple if statement to trigger whether or not to return the full TransactionSearch API result, or only return the PayPal transaction ID (bla.php?view=minimal):

if($_GET['view'] != "minimal") {
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) {
    echo('TransactionSearch Completed Successfully: '.print_r($httpParsedResponseAr, true));
} else  {
    echo('TransactionSearch failed: ' . print_r($httpParsedResponseAr, true));
}
}
else {
// Output only the TransactionID
echo $httpParsedResponseAr['L_TRANSACTIONID0'];
}


?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文