关闭

paypal支付代码-PHP

1279人阅读 评论(0) 收藏 举报

aypal支付原理

首先根据paypal的要求构造出一个表单,然后提交到paypal去处理,paypal处理后会以隐藏方式post数据到商家网站指定的页面,在商家网站指定的页面,我们先要把payapal的post到该页面上的值进行处理,然后再投递到paypal上去验证,如果是正确的,则paypal会返回VERIFIED,否则返回INVALID.

看不懂原理没有关系,直接看代码。

下面是构造的提交到PAYPAL得表单。

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0Transitional//EN”“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<htmlxmlns=”http://www.w3.org/1999/xhtml”>

<head>

<meta http-equiv=”Content-Type” content=”text/html;charset=utf-8″ />

<title>无标题文档</title>

</head>

<body>

<form style=”text-align:center;”action=”https://www.sandbox.paypal.com/cgi-bin/webscr”method=”post”>

<input type=’hidden’ name=’cmd’value=’_xclick’>

<input type=’hidden’ name=’business’value=’selle3_1263191856_per@qq.com’>

<input type=’hidden’ name=’item_name’value=’201001114262121′>

<input type=’hidden’ name=’amount’value=’1.00′>

<input type=’hidden’ name=’currency_code’value=’USD’>

<input type=’hidden’ name=’return’value=’http://www.xxx.com/return.php’>

<input type=’hidden’ name=’invoice’value=’82′>

<input type=’hidden’ name=’charset’value=’utf-8′>

<input type=’hidden’ name=’no_shipping’value=’1′>

<input type=’hidden’ name=’no_note’value=”>

<input type=’hidden’ name=’notify_url’value=’http://www.xxx.com/return.php’>

<input type=’hidden’ name=’rm’value=’82′>

<input type=’hidden’ name=’cancel_return’value=’http://www.xxx.com/index.html’>

<input type=’submit’ value=’立即使用 paypal支付’>

</form>

</body>

</html>

下面是处理代码:

<?php

$merchant_id = ’selle3_1263191856_per@qq.com’; ///获取商户paypal账户

// read the post from PayPal system and add ‘cmd’

$req = ‘cmd=_notify-validate’;

foreach ($_POST as $key => $value)

{

$value = urlencode(stripslashes($value));

$req .= “&$key=$value”;

}

// post back to PayPal system 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”;

$fp = fsockopen (‘www.sandbox.paypal.com’, 80, $errno, $errstr,30); //如果是测试账户,则投递到www.sandbox.paypal.com,否则投递到www.paypal.com

// assign posted variables to local variables

$item_name = $_POST['item_name'];

$item_number = $_POST['item_number'];

$payment_status = $_POST['payment_status'];

$payment_amount = $_POST['mc_gross'];

$payment_currency = $_POST['mc_currency'];

$txn_id = $_POST['txn_id'];

$receiver_email = $_POST['receiver_email'];

$payer_email = $_POST['payer_email'];

$order_sn = $_POST['invoice'];

$memo = !empty($_POST['memo']) ? $_POST['memo'] : ”;

//$action_note = $txn_id . ‘(’ . $GLOBALS['_LANG']['paypal_txn_id']. ‘)’ . $memo;

if (!$fp)

{

fclose($fp);

return false;

}

else

{

fputs($fp, $header . $req);

while (!feof($fp))

{

$res = fgets($fp, 1024);

if (strcmp($res, ‘VERIFIED’) == 0)

{

//付款成功的代码

}

elseif (strcmp($res, ‘INVALID’) == 0)

{

//付款失败的代码

fclose($fp);

return false;

}

}

}

需要注意的是上面是用的paypal的sandbox.


原文地址:http://paypal999.blog.163.com/blog/static/13737488320107306370292/


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:531251次
    • 积分:6875
    • 等级:
    • 排名:第3487名
    • 原创:120篇
    • 转载:215篇
    • 译文:0篇
    • 评论:28条
    博客专栏
    文章分类
    最新评论