2011-12-23 22 views
4

私は、PHPで書かれたPayPalチェックアウトコンポーネントを使用する電子商取引Webサイトで働いています。会計上の目的のために、私はPayPal PHP SOAP APIを使用していくつかの追加情報を取得したいと考えています。請求書IDからPayPalの取引IDを取得する方法

私はトランザクション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 - 

しかし、私は私が最初で12文字の文字列のトランザクションIDを見つけることができるようにこれを強化する必要があります私はローカルMySQLデータベース(PayPalウェブサイトのトランザクションでも参照されています)で利用可能な請求書IDを使用します。

私はTransaction Searchを使用しなければならないと思いますが、PHP SOAP APIでこれを行う方法はわかりません。請求IDのトランザクションIDを取得するにはどうすればよいですか?

+0

あなたの請求書IDはありますが、支払い手続きのトランザクションIDはありません。 –

+0

はい、正確です。私は、私が知る限り、チェックアウト時にPHPコンポーネントによって生成された請求書IDを持っています。私が必要とするのは、PayPalがトランザクションに対して生成するトランザクションIDです。 – hbit

+0

あなたのアプリは請求書IDを生成しますか、またはこの情報をpaypalから受け取りますか? –

答えて

3

私は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 - 
2

TransactionSearch APIを使用して、請求書番号でトランザクションを見つけるだけで十分です。あなたがする必要があるのは、PayPalへのAPIコールでINVNUMパラメータを送信することだけです。 (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設定することにより、:if文のフルTransactionSearchを返すかどうかをトリガするために、単純なを追加することにより、

// 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); 

そして、ここをAPIの結果を返すか、またはPayPalのトランザクション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']; 
} 


?> 
+0

これを最も確実に修正したいと思うでしょうが、少なくともこれを始めるべきです。 – Robert

+0

あなたの答えは、名前 - 値ペア(NVP)APIで動作し、SOAP APIでは動作しません。さらに、そのほとんどは私の問題とは関係ありません。自分で問題を解決するのに役立ちましたので、私はそれを投票しました。ありがとう! – hbit

関連する問題