php中的购物车以及如何在其中集成PayPal?

Posted

技术标签:

【中文标题】php中的购物车以及如何在其中集成PayPal?【英文标题】:Shopping cart in php and how to integrat PayPal in it? 【发布时间】:2011-03-04 00:52:57 【问题描述】:

我想用 php 开发一个购物车。我还想使用贝宝集成在线支付功能。 允许通过信用卡和借记卡付款。

那么请告诉我如何将 paypal 集成到我自己的购物车中以实现安全交易?

谢谢!

【问题讨论】:

【参考方案1】:

您,或者如果是您的客户,将需要在 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。有很多! ;) 请访问PayPal Development 了解更多信息!

【讨论】:

【参考方案2】:

Paypal 提供various APIs 和服务,因此您需要选择或更多,他们根据其 API 实现它。

【讨论】:

嗨,约翰!感谢您的快速答复。告诉我朋友,这些服务和 API 是免费的吗?我从哪里获得有关如何在我的购物车中实现这些 API 和服务的信息? 使用 API 是免费的。服务本身要花钱。但是您应该能够使用 Paypal Sandbox 将您的代码开发到他们提供的任何 API 上,而不会产生任何费用。

以上是关于php中的购物车以及如何在其中集成PayPal?的主要内容,如果未能解决你的问题,请参考以下文章

PHP Paypal 标准集成

如何在不使用“立即购买”按钮的情况下为自定义购物车集成 Paypal IPN

“立即付款”按钮集成的贝宝选项 (PHP)

用php加密动态“添加到购物车”贝宝按钮的问题

Paypal 与 iphone 中的购物车按钮

使用 c# 在 asp.net 中集成 paypal