Rumah >pembangunan bahagian belakang >tutorial php >PayPal网站付款标准版(for PHP),paypal标准版_PHP教程

PayPal网站付款标准版(for PHP),paypal标准版_PHP教程

WBOY
WBOYasal
2016-07-13 09:51:10891semak imbas

PayPal网站付款标准版(for PHP),paypal标准版

简单整理一下PHP项目整合PayPal支付功能。

一、表单的构建:

<span><</span><span>form </span><span>method</span><span>="post"</span><span> name</span><span>="form"</span><span> action</span><span>="https://www.paypal.com/cgi-bin/webscr"</span><span>></span>
<span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="rm"</span><span> value</span><span>="2"</span><span>/></span>
<span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="cmd"</span><span> value</span><span>="_xclick"</span><span>/></span>
<span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="business"</span><span> value</span><span>="商家账号"</span><span>/></span>
<span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="return"</span><span> value</span><span>="返回网址"</span><span>/></span>
<span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="cancel_return"</span><span> value</span><span>="取消返回网址"</span><span>/></span>
<span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="notify_url"</span><span> value</span><span>="用于接收PayPal发送的即时付款通知的URL,必须是有效的URL"</span><span>/></span>
<span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="item_name"</span><span> value</span><span>="物品名称"</span><span>/></span>
<span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="item_number"</span><span> value</span><span>="可用于跟踪购买或捐赠的传递变量,在付款完成时传回给您"</span><span>/></span>
<span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="amount"</span><span> value</span><span>="物品的价格(购物车中所有物品的总价格"</span><span>/></span>
<span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="currency_code"</span><span> value</span><span>="币种"</span><span>/></span>
<span></</span><span>form</span><span>></span>

二、IPN验证部分

<?<span>php
</span><span>class</span><span> paypal {
  </span><span>var</span> <span>$ipn_data</span> = <span>array</span>();         <span>//</span><span> array contains the POST values for IPN</span>
  <span>var</span> <span>$fields</span> = <span>array</span>();           <span>//</span><span> PayPal接受到客户的付款后,Paypal会向网站POST回客户提交的表单信息,<br />                     //  必须将收到的POST信息对原样返回给PayPal进行验证,<br />                     //  内容有:item_name=iPhone 6,quantity=1,amount=499,currency_code=USD等所有表单信息,<br />                     // 在调用验证IPN之前事先需要初始化好</span>
  <span>var</span> <span>$paypal_url</span> = 'https://www.paypal.com/cgi-bin/webscr';<span>//</span><span>sandbox:https://www.sandbox.paypal.com/cgi-bin/webscr</span>
  <span>
  </span><span>function</span> validate_ipn() {<span>//</span><span>验证IPN
      // parse the paypal URL</span>
      <span>$url_par</span>=<span>parse_url</span>(<span>$this</span>-><span>paypal_url);
      </span><span>//</span><span> generate the post string from the _POST vars aswell as load the
      // _POST vars into an arry so we can play with them from the calling
      // script.</span>
      <span>$post_str</span> = ''<span>;
      </span><span>foreach</span> (<span>$_POST</span> <span>as</span> <span>$field</span>=><span>$value</span><span>) {
        </span><span>$this</span>->ipn_data["<span>$field</span>"] = <span>$value</span><span>;
        </span><span>$post_str</span> .= <span>$field</span>.'='.<span>urlencode</span>(<span>stripslashes</span>(<span>$value</span>)).'&'<span>;
      }
      </span><span>$post_str</span>.="cmd=_notify-validate"; <span>//</span><span> append ipn command
      // open the connection to paypal</span>
      <span>$fp</span> = <span>fsockopen</span>(<span>$url_par</span>[host],"80",<span>$errnum</span>,<span>$errstr</span>,30<span>);
      </span><span>if</span>(!<span>$fp</span><span>) {
        </span><span>//</span><span> could not open the connection.</span>
        <span>return</span> <span>false</span><span>;
      } </span><span>else</span><span> {
        </span><span>//</span><span> Post the data back to paypal</span>
        <span>fputs</span>(<span>$fp</span>, "POST ".<span>$url_par</span>[path]." HTTP/1.1\r\n"<span>);
        </span><span>fputs</span>(<span>$fp</span>, "Host: ".<span>$url_par</span>[host]."\r\n"<span>);
        </span><span>fputs</span>(<span>$fp</span>, "Content-type: application/x-www-form-urlencoded\r\n"<span>);
        </span><span>fputs</span>(<span>$fp</span>, "Content-length: ".<span>strlen</span>(<span>$post_str</span>)."\r\n"<span>);
        </span><span>fputs</span>(<span>$fp</span>, "Connection: close\r\n\r\n"<span>);
        </span><span>fputs</span>(<span>$fp</span>, <span>$post_str</span> . "\r\n\r\n"<span>);
        </span><span>//</span><span> loop through the response from the server and append to variable</span>
        <span>while</span>(!<span>feof</span>(<span>$fp</span><span>)) {
          </span><span>$this</span>->validate_ipn_response .= <span>fgets</span>(<span>$fp</span>, 1024<span>);
        }
        </span><span>fclose</span>(<span>$fp</span>); <span>//</span><span> close connection</span>
<span>     }
    </span><span>if</span> (<span>eregi</span>("VERIFIED",<span>$this</span>-><span>validate_ipn_response)) {
      </span><span>return</span> <span>true</span><span>;
    } </span><span>else</span><span> {
      </span><span>return</span> <span>false</span><span>;
    }
  }
}
</span>?>

即时付款通知(IPN)示意图如下:

 

1) 客户点击“付款”按钮向您的账户付款;

2) PayPal 接受到客户的付款后,向您的服务器指定的 URL 通过 POST 方式发送 IPN;

3) 在您的服务器收到 IPN 之后,您必须将收到的 POST 信息对原样返回给 PayPal 进行验证,PayPal 通过此方法帮您防范欺骗或“中间人”攻击;(对IPN信息的验证过程我们称之为通知确认)

4) PayPal 返回验证信息,通过验证为 VERIFIED,不通过则为 INVALD;

5) 根据验证信息处理付款明细。

相关资料:

开发者:https://developer.paypal.com/
即时付款通知:https://www.paypal-biz.com/development/documentation/PayPal_IPN&PDT_Guide_V1.0.pdf
paypal标准版:https://www.paypal-biz.com/development/documentation/PayPal_WPS_Guide_CN_V2.0.pdf 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1014831.htmlTechArticlePayPal网站付款标准版(for PHP),paypal标准版 简单整理一下PHP项目整合PayPal支付功能。 一、表单的构建: form method ="post" name ="form" action ="htt...
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn