How to get PayPal transaction ID from invoice ID

hbit picture hbit · Dec 23, 2011 · Viewed 7.6k times · Source

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?

Answer

hbit picture hbit · Jan 3, 2012

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 -