转载地址:http://www.lamp99.com/paypal-php-butt.html
项目需要要在php中使用paypal支付。之前本站也提供了很多的PayPal的资料,有网上分享的,也有个人经验,好吧,正题,
先吐槽下paypal,以前做过国内的一些支付接口,有些经验。想到的是直接找paypal客服要接口文档。
尼玛加paypal客服QQ 不在线。几天如此。这么大的支付居然没技术支持,真不能理解。文档后面还是在官网7找8找才找到了。
支付过程首先第一步是提交表单给paypal 当然post方式。
01 | <form id= "form_starPay" name= "form_starPay" action= "https://www.sandbox.paypal.com/cgi-bin/webscr" method= "post" > |
02 | <input type= "hidden" name= "cmd" value= "_xclick" > |
03 | <input type= "hidden" name= "business" value= "XXXXXX@XXXXX.com" > |
04 | <input type= "hidden" name= "item_name" value= "name" > |
05 | <input type= "hidden" name= "amount" value= "10000" > |
06 | <input type= "hidden" name= "currency_code" value= "JPY" > |
07 | <input type= "hidden" name= "return" value= "http://localhost/frontend/pay/PDT_order" > //支付成功后网页跳转地址 |
08 | <input type= "hidden" name= "notify_url" value= "http://localhost/frontend/pay/IPN_Order" > //支付成功后paypal后台发送订单通知地址 |
09 | <input type= "hidden" name= "invoice" value= "" > |
10 | <input type= "hidden" name= "custom" value= "" > |
11 | <input type= "hidden" name= "lc" value= "JP" > |
12 | <input style= "visibility:hidden" type= "image" src= " https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif " border= "0" name= "submit" alt= " PayPal - The safer, easier way to pay online" >//支付按钮 |
当支付成功后 paypal就会跳转到我们设置的 “return” 地址,会带上一个流水号 我们get方式就可以取到值。流水号键名 “tx”。
拿到流水号然后加上身份标识跟cmd变量就可以到paypal请求刚才支付的订单的交易内容。
例如:cmd=_notify-synch&tx=123sflsfjlw12&tx_token=fsfljvw3lwejloj43jfvdflf2
cmd=_notify-synch是告诉paypal你要做什么,这里是查询订单交易。
tx_token值要在paypal后台取,刚才注册帐号的时候要开通pdt功能才行。
这个过程paypal称为:PDT (Payment Data Transfer 付款数据传输)。
我们接收到返回的一些数据后就可以进行一些支付后的操作,比如发金币,发货等等。
pdt 详细的返回参数最后给出文档。
代码:
02 | $tx_token = $_GET [ 'tx' ]; |
04 | $auth_token = "CHANGE-TO-YOUR-TOKEN" ; |
06 | $req = " cmd=_notify-synch&tx=$tx_token&at=$auth_token" ; |
08 | $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n" ; |
09 | $header .= "Content-Type: application/x-www-form-urlencoded\r\n" ; |
10 | $header .= "Content-Length: " . strlen ( $req ) . "\r\n\r\n" ; |
11 | $fp = fsockopen ( 'www.paypal.com' , 80, $errno , $errstr , 30); |
15 | fputs ( $fp , $header . $req ); |
20 | $line = fgets ( $fp , 1024); |
21 | if ( strcmp ( $line , "\r\n" ) == 0) { |
24 | } else if ( $headerdone ){ |
30 | $lines = explode ( "\n" , $res ); |
32 | if ( strcmp ( $lines [0], "SUCCESS" ) == 0) { |
33 | for ( $i =1; $i < count ( $lines ); $i ++){ list( $key , $val )= "explode("="," $lines [ $i ]);= "" $keyarray [urldecode( $key )]= "urldecode($val);" }= "" 检查交易付款状态= "" payment_status= "" 是否为= "" „completed‟= "" 检查交易流水号= "" txn_id= "" 是否已经被处理过= "" 检查接收= "" email= "" receiver_email= "" 是否为您的= "" paypal= "" 中已经注册的= "" 检查金额= "" mc_gross= "" 是否正确= "" ……= "" 处理此次付款明细= "" 该付款明细所有变量可参考:= "" https:= "" www.paypal.com= "" integrationcenter= "" ic_ipn-pdt-variable-reference.html= "" $name = "$keyarray['first_name']" .= "" '="" $keyarray[' last_name '];="" $itemname="$keyarray[' item_name '];" $amount="$keyarray[' mc_gross'];" echo = "" ( "<p=" "><h3>Thank you for you purchase!</h3><p></p>" ); |
34 | echo ( "<b>Payment Details:</b><br>\n" ); |
35 | echo ( "<li>Name: $name</li>\n" ); |
36 | echo ( "<li>Item: $itemname</li>\n" ); |
37 | echo ( "<li>Amount: $amount</li>\n" ); |
38 | } else if ( strcmp ( $lines [0], "FAIL" ) == 0) { |
43 | </ count ( $lines ); $i ++){> |
为了防止用户关掉浏览器,订单通知不到的情况。paypal还提供了一种通知方式:IPN (Instant Payment Notification 即时付款通知);
IPN 是在后台进行http请求通知。
当开通了ipn功能,并且订单状态发生改变的时候,paypal会主动请求我们支付表单中变量“notify_url”提供的地址。
我们在接收到paypal请求后要拿到所有的数据。
然后用 “cmd=_notify-validate”加上刚才的数据,http发送给paypal进行验证,以防请求是伪造的。
代码:
02 | $req = 'cmd=_notify-validate' ; |
03 | foreach ( $_POST as $key => $value ) { |
04 | $value = urlencode( stripslashes ( $value )); |
05 | $req .= "&$key=$value" ; |
09 | $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n" ; |
10 | $header .= "Content-Type:application/x-www-form-urlencoded\r\n" ; |
11 | $header .= "Content-Length:" . strlen ( $req ) . "\r\n\r\n" ; |
14 | $fp = fsockopen ( 'www.paypal.com' , 80, $errno , $errstr , 30); |
18 | $item_name = $_POST [ 'item_name' ]; |
19 | $item_number = $_POST [ 'item_number' ]; |
20 | $payment_status = $_POST [ 'payment_status' ]; |
21 | $payment_amount = $_POST [ 'mc_gross' ]; |
22 | $payment_currency = $_POST [ 'mc_currency' ]; |
23 | $txn_id = $_POST [ 'txn_id' ]; |
24 | $receiver_email = $_POST [ 'receiver_email' ]; |
25 | $payer_email = $_POST [ 'payer_email' ]; |
32 | fputs ( $fp , $header . $req ); |
35 | $res = fgets ( $fp , 1024); |
37 | if ( strcmp ( $res , "VERIFIED" ) == 0) { |
43 | } else if ( strcmp ( $res , "INVALID" ) == 0) { |
接口对接就这样好了,搞清楚了流程很简单。
如果币种不是paypal默认的,在订单支付后订单状态一直是pending, 用卖家帐号登录在订单状态哪里点击接收,paypal就会自动设置接收该币种功能。
技术文档地址:https://www.paypal-biz.com/developer/documentation/2134.html