簡體   English   中英

PHP中的購物車以及如何在其中集成PayPal?

[英]Shopping cart in php and how to integrat PayPal in it?

我想用php開發購物車。 我還想使用Paypal集成在線支付工具。 允許通過信用卡和借記卡付款。

因此,請告訴我如何在我自己的購物車中整合Paypal以實現安全交易?

謝謝!

貝寶提供各種API和服務,因此您需要選擇一個或多個API,然后它們會根據其API來實現它。

您(如果您的客戶是這種情況)將需要在PayPal擁有一個帳戶,然后推斷,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 = 'foobar@example.com';

      // 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:這是基於PayPal開發人員網站,有一個可用的API。有很多! ;)請訪問貝寶開發以獲取更多信息!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM