2010-06-15 36 views
2

我想在php中開發一個購物車。我也想用paypal整合在線支付工具。 它允許支付通過信用卡和decbit卡。購物車在PHP和如何整合PayPal在它?

那麼請告訴我如何將paypal集成到我自己的購物車中以實現安全交易?

謝謝!

回答

1

貝寶提供various APIs和服務,所以你需要選擇,或更多,他們根據其API執行它。

+0

嗨,約翰! 感謝您的快速回復。 只要告訴我朋友是那些服務和API是免費的? 我在哪裏可以獲得有關如何在購物車中實現這些API和服務的信息? – 2010-06-15 15:38:32

+0

使用API​​是免費的。服務本身花錢。但是,您應該可以使用PayPal Sandbox來爲您提供的任何API提供代碼,而不會產生任何費用。 – 2010-06-15 16:00:23

0

如果您的客戶需要在PayPal賬戶上存款,那麼Paypal會爲您提供適當的文檔以登錄他們的系統並查詢賬戶的餘額,驗證等信息。 ..

這是一個示例(工作代碼)與貝寶IPN互動:

<?php 
error_reporting(E_ALL^E_NOTICE); 

// devdaily.com paypal php ipn example. 
// version 1.0 
// this example built on the paypal php ipn example, with bug fixes, 
// and no need for ssl. 

$header = ""; 
$emailtext = ""; 

// read the post from paypal and add 'cmd' 
$req = 'cmd=_notify-validate'; 
if(function_exists('get_magic_quotes_gpc')) 
{ 
    $get_magic_quotes_exits = true; 
} 

// handle escape characters, which depends on setting of magic quotes 
foreach ($_POST as $key => $value) 
{ 
    if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) 
    { 
    $value = urlencode(stripslashes($value)); 
    } 
    else 
    { 
    $value = urlencode($value); 
    } 
    $req .= "&$key=$value"; 
} 

// post back to paypal to validate 
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n"; 
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; 

// here you can use ssl, or not 
// $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); 
$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30); 

// process validation from paypal 
// TODO: This sample does not test the http response code. 
// All HTTP response codes must be handles or you should use an HTTP 
// library, such as cUrl. 

if (!$fp) 
{ 
    // HTTP ERROR 
} 
else 
{ 
    // NO HTTP ERROR 
    fputs ($fp, $header . $req); 
    while (!feof($fp)) 
    { 
    $res = fgets ($fp, 1024); 
    if (strcmp ($res, "VERIFIED") == 0) 
    { 
     // get variables from the paypal post to us 
     // see these pages for possible variables: 
     // https://developer.paypal.com/us/cgi-bin/devscr 
     // https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=develo... 

     // payment info 
     $payment_status = $_POST['payment_status']; 
     $payment_amount = $_POST['mc_gross']; 
     $payment_currency = $_POST['mc_currency']; 
     $txn_id = $_POST['txn_id']; 

     // product info 
     $item_name = $_POST['item_name']; 
     $item_number = $_POST['item_number']; 

     // buyer info 
     $payer_email = $_POST['payer_email']; 
     $first_name = $_POST['first_name']; 
     $last_name = $_POST['last_name']; 
     $address_city = $_POST['address_city']; 
     $address_state = $_POST['address_state']; 
     $address_country = $_POST['address_country']; 

     // receiver_email, that's our email address 
     $receiver_email = $_POST['receiver_email']; 

     // put your actual email address here 
     $our_email = '[email protected]'; 

     // if all these conditions are true, send the email 
     // note: should also verify that $txn_id was not used before 
     if (($payment_status == 'Completed') && 
     ($receiver_email == $our_email) && 
     ($payment_amount == $amount_they_should_have_paid) && 
     ($payment_currency == "USD")) 
     { 
     foreach ($_POST as $key => $value) 
     { 
      $emailtext .= $key . " = " .$value ."\n\n"; 
     } 
     mail($payer_email, "Live-VERIFIED IPN", $emailtext . "\n\n" . $req); 
     } 
    } 
    else if (strcmp ($res, "INVALID") == 0) 
    { 
     // If 'INVALID', send an email. TODO: Log for manual investigation. 
     foreach ($_POST as $key => $value) 
     { 
     $emailtext .= $key . " = " .$value ."\n\n"; 
     } 
     mail($payer_email, "Live-INVALID IPN", $emailtext . "\n\n" . $req); 
    } 
    } 
} 
fclose ($fp); 
?> 

PS: 這是基於貝寶開發者網站,從一個API可利用的。有很多! ;)請訪問PayPal Development瞭解更多信息!