测试php-paypal-sandbox全程
上一篇 / 下一篇 2008-04-23 10:03:28 / 個人分類:php
1,到developer.paypal.com注册若鄰博客k3fetZ8y9P
EMail激活,登录后创建一个business和一个personal帐号(要记住密码,最好弄个简单容易记的,测试而已)若鄰博客2T2]~ P` Vn
Y1Y.d^jL5Z&kp02,php环境问题若鄰博客hRt p$jm:Y1L
好不容易成功启动了php的curl和openssl,我用的是php的msi安装程序,开始会出现“访问拒绝”的提示,后来将curl和openssl取消后,然后重新选择安装,OK
3,研读paypal的文档 PP_OrderManagement_IntegrationGuide.pdf若鄰博客;aqcQR6q'suu9K]
通过business帐号登录,在激活IPN,步骤是:
;d(oh9X%p
vs0 在Profile的Selling Preferences一栏,进入“Instant Payment Notification Preferences”,然后Edit,选中checkbox,填入notify_url地网址
j*~|-N3]M4{d
C'y0
j3L;Ec!S0 走了很多弯路,才知道AutoReturn,可是设置return的网址,步骤是:
-G7@`2Ad,J&c3L6~0 同样在Profile的Selling Preferences一栏,进入“Web Payment Preferences”,第一项是AutoReturn,设置为On,并填入自己要return到的网址;若鄰博客8cd
s#Ihxg(a1}2}@
第二项是PDT,设置为On,否则return的网址得不到相应数据。若鄰博客J3nA|wc:ys
xD/z r4r:l^'@04,弄清楚了notify_url,return这两个网址的作用
U\xx
Er5vd0 POST FORM代码
<input type="hidden" name="notify_url" value="http://mydomain.com/paypal/validate.php" />
bk8y/[R2A0<input type="hidden" name="return" value="http://mydomain.com/paypal/success.php" />
S"m6X7ec9}2y0<input name="cmd" value="_cart" type="hidden">若鄰博客c-s C(j4E T~4H@
<input name="add" value="1" type="hidden">若鄰博客2^Zj}l@ q B^)h
<input name="business" value="mytest_1193721986_biz@hotmail.com" type="hidden">若鄰博客*~i`5G2za
<br />
CW1i&}/km\QO0<!--商品名称-->若鄰博客]ts(sg%N(l-I!t
<input name="item_name" value="Stuff Test" type="text">若鄰博客C!yH~/gbK2tt
<!--商品单价-->若鄰博客$x ?8IKb#Z4_0W p*x
<input name="amount" value="3.00" type="text">
C[H2`#K8u(E0<!--商品数量-->若鄰博客U,?Q$F-eX9[;EO
<input name="quantity" value="2" type="text">若鄰博客7EpBAW4f
5O"vY-p|x:^B0<input name="currency_code" value="USD" type="hidden">若鄰博客.`QD c gYA&^S;?d
<input name="lc" value="US" type="hidden">若鄰博客&~(`Q,Tp4Wb
<input type="submit" value="Add Cart"/>若鄰博客)tRKq*wfVK[!t
</form>若鄰博客0C&d;P3Gg(~#X
若鄰博客2E2aNN!xi |R7OK2@
其实在Payment Complete的时候,notify_url会被后台运行,用来核对交易数据(数据通过POST传递),return则是用来给用户交易完成后显示(可以不使用AutoReturn,那样的话就是用户点击"return mytest's test Store"按钮后转向的页面,打开PDT的话,就会把交易数据通过GET传至此页面)若鄰博客"t'l9yV ^U
(~qG8V3TSF:E5?0 notify_url: 需要激活,后台运行,POST传入数据,提交给curl处理,获取交易信息验证(在Profile统一设置,也可在提交的Form里用notify_url参数指定特殊指向,最好附带一个自定义的秘密参数,参与最后的验证);
\jB;mn8A$ajO%AQ0 return: 前台显示,用户点击转至或者设置AutoReturn自动转至,GET传入数据,用于显示交易信息(可在Profile统一设置,也可在提交的Form里用return参数指定特殊指向)。
X6k/Tbq0
| <?php g!s;sv*Z1Qv0// read the post from PayPal system and add 'cmd'若鄰博客{v&{ m?|V{ $req = 'cmd=_notify-validate'; 8_*S[HaB0 2uSya'}(c~&N0foreach ($_POST as $key => $value) { L$V@5s:C3~0$value = urlencode(stripslashes($value)); !ht~@ a)i[0$req .= "&$key=$value"; A S\+IO7?5h0} N@5Jakb5y1r(t` E0 :}rY2ZZ"C0Mr0// post back to PayPal system to validate若鄰博客%fO#T*]PIq] $ppCurl = curl_init(); // initialize curl handle i/a7T-i"U5N0KEw zo0curl_setopt($ppCurl, CURLOPT_POST, true); // set POST method 8i*Z `mN tB0curl_setopt($ppCurl, CURLOPT_URL, "http://www.sandbox.paypal.com/cgi-bin/webscr"); // set url若鄰博客PoT~Q/f curl_setopt($ppCurl, CURLOPT_POSTFIELDS, $req); // fields to POST若鄰博客+l&X'c4B*i curl_setopt($ppCurl, CURLOPT_RETURNTRANSFER, true); // return var 2E#V,~1{ v;[3M(J0curl_setopt($ppCurl, CURLOPT_TIMEOUT, 4); // time out after 5 secs若鄰博客v wP'xw,G4]^Iaw curl_setopt($ppCurl, CURLOPT_FAILONERROR, true); (Q^&z-\(Xt0curl_setopt($ppCurl, CURLOPT_FOLLOWLOCATION, true); // allow redirects若鄰博客v}*j6S~ A curl_setopt($ppCurl, CURLOPT_FRESH_CONNECT, true); // no caching若鄰博客Mi:Cq [(`!S $result = curl_exec($ppCurl); // engage! c2?oZ8L _sg0 j"`%k*xdxA+lQ0$curlErrorNum = curl_errno($ppCurl); // save error code; 0=none若鄰博客6KB]7r6m:xX%CR A $curlErrorText = curl_error($ppCurl); // save error message; ""=none %fV;sB Yn3g0curl_close($ppCurl); Sr#wT_b0 6d|.px8U$Nl0// assign posted variables to local variables +R.U#ff9nHl0$item_name = $_POST['item_name'];若鄰博客vd3aTP;b+J $item_number = $_POST['item_number']; l.U9fC5n-o} S i8q3\0$payment_status = $_POST['payment_status']; (G'c:G}*I\O u0P*c0$payment_amount = $_POST['mc_gross'];若鄰博客wSl([5g f/I$R0?^ $payment_currency = $_POST['mc_currency']; (LqB2TY#BifG0$txn_id = $_POST['txn_id']; S+}1F2?8c0U]b0$receiver_email = $_POST['receiver_email'];若鄰博客W`&B~3I9qfD $payer_email = $_POST['payer_email'];若鄰博客e3u!R sEC,lC *u+Z_)vo0if ($curlErrorNum != 0) {若鄰博客$Do{*u-aok0z)f // CURL error若鄰博客!Hk.x\r ,lj1fG*F0} else if ($result == "VERIFIED") {若鄰博客;u/G3TEp N&nN // check the payment_status is Completed ^yT*y9?%d,?orQq0// check that txn_id has not been previously processed若鄰博客F&?_;tg0~? // check that receiver_email is your Primary PayPal email 7LXx@d0nvF)\v(|0// check that payment_amount/payment_currency are correct若鄰博客#L+o?#C]5M-uH _ // process payment若鄰博客#nDY6MC:t 若鄰博客4zA$Ev.gw)H j 若鄰博客"N S9Tw'eZ } else if ($result == "INVALID") { ~!z pH,V0// handle false claim若鄰博客8X\F"T8~M\;|Q 若鄰博客%\O4U+@reI } \b)m@S4Lm#Q"?0若鄰博客/d:Hv_'T,U&t ?> |
1Z_m#yBI+\*L4e0若鄰博客'D$lE3y8en3H
附success.php代码若鄰博客6D
mq$~Y Q{5a
| <?php若鄰博客s{:\.q0i-|v3F $token = "your_pdt_token_id"; {!LV"^b$M s&s7EV0$req = 'cmd=_notify-synch&tx='.$_GET["tx"]."&at=".$token; '] N#v1K p#cz;^0/*/若鄰博客k(SlwgH^j/KST foreach ($_GET as $key => $value) {若鄰博客_HI _V6A}v $value = urlencode(stripslashes($value));若鄰博客1xN5qi~+SD(BA | $req .= "&$key=$value"; 4SLmHx$|3a-x0}若鄰博客 }&ff.N ~/L //*/ )a En1co:fun0若鄰博客PS$s7S#aW5A // post back to PayPal system to validate若鄰博客^+iM"AP)p[%{ i $ppCurl = curl_init(); // initialize curl handle _Id]+UEb+r0curl_setopt($ppCurl, CURLOPT_POST, true); // set POST method若鄰博客 ^.e~+~8e5? z5]0J curl_setopt($ppCurl, CURLOPT_URL, "http://www.sandbox.paypal.com/cgi-bin/webscr"); // set url *q_q hX!N ~b o0a0curl_setopt($ppCurl, CURLOPT_POSTFIELDS, $req); // fields to POST若鄰博客-p rj0bD Y curl_setopt($ppCurl, CURLOPT_RETURNTRANSFER, true); // return var #a;ij _P"jJ:NT0curl_setopt($ppCurl, CURLOPT_TIMEOUT, 4); // time out after 5 secs E Br-P$DhM0curl_setopt($ppCurl, CURLOPT_FAILONERROR, true); ~'w ?j*V9sU0curl_setopt($ppCurl, CURLOPT_FOLLOWLOCATION, true); // allow redirects若鄰博客w2W7Wz} curl_setopt($ppCurl, CURLOPT_FRESH_CONNECT, true); // no caching #E I^|e$F0$result = curl_exec($ppCurl); // engage!若鄰博客}8I7E$Cm 若鄰博客&x @N#TPUR $curlErrorNum = curl_errno($ppCurl); // save error code; 0=none若鄰博客(I1X#V/q}|;t6U(v $curlErrorText = curl_error($ppCurl); // save error message; ""=none :RQxs/|h0curl_close($ppCurl);若鄰博客!T ?Am L*CCh $y0?/a+i(z0// assign posted variables to local variables若鄰博客E5f`2|.K%g3@gz $item_name = $_POST['item_name']; 3q#?`1Eup0P0$item_number = $_POST['item_number'];若鄰博客d&xB!@XL"Z $payment_status = $_POST['payment_status'];若鄰博客*k1^0jlS9xV$s j $payment_amount = $_POST['mc_gross'];若鄰博客S7K_ jSB $payment_currency = $_POST['mc_currency']; l?Io)`0$txn_id = $_POST['txn_id']; Y8a(J J|%qi0V0$receiver_email = $_POST['receiver_email']; 0d IfLe6HA0$payer_email = $_POST['payer_email']; X:WxO+Q'^;A0 4|n,o7q fR#`a0// parse the data +q^.aa\(Js0$lines = explode("\n", $result);若鄰博客;?gGs h%t"f $keyarray = array(); *{5o6lI(V a@7^6kE9f z0if (strcmp ($lines[0], "SUCCESS") == 0) { [aO-\:Q"|0e0 for ($i=1; $i<count($lines);$i++){ n P.g;y*E:~0 list($key,$val) = explode("=", $lines[$i]);若鄰博客+|:w*w(A:yvbh G $keyarray[urldecode($key)] = urldecode($val);若鄰博客/qV&z@ l'J } xG/^\%h/p*WM0 // check the payment_status is Completed :TN v)l%OA1h0 // check that txn_id has not been previously processed )|*_WV8E9p }0 // check that receiver_email is your Primary PayPal email ~u2GHgo@0 // check that payment_amount/payment_currency are correct若鄰博客(f-`(E%b+T z2d3q // process payment tyG:I7c#i0 $firstname = $keyarray['first_name'];若鄰博客R ?M9P)C $lastname = $keyarray['last_name']; d&oMz+MK;Zcs%d3j0 $itemname = $keyarray['item_name'];若鄰博客7Z5NOo-v $amount = $keyarray['payment_gross']; }K\e%EV0若鄰博客M#h&`d$\y X^ echo ("<p><h3>Thank you for your purchase!</h3></p>");若鄰博客p(j%L'C\6S#`P'@Xe 若鄰博客2SIhRN echo ("<b>Payment Details</b><br>\n");若鄰博客 D~:ofh[1I foreach ($keyarray as $key => $value) {若鄰博客wYS x9Nl d] $value = urlencode(stripslashes($value));若鄰博客,Z U;g"W} UB8p echo "<li>$key: $value</li>\n"; R6?O x Oi-k Ku${0 } l7L)j R,o3A}0 echo (""); |