paypal支付代码-PHP

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值