溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP如何實(shí)現(xiàn)PayPal支付功能

發(fā)布時(shí)間:2021-07-05 15:36:51 來源:億速云 閱讀:237 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“PHP如何實(shí)現(xiàn)PayPal支付功能”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

簡(jiǎn)單整理一下PHP項(xiàng)目整合PayPal支付功能。

一、表單的構(gòu)建:

<form method="post" name="form" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" name="rm" value="2"/>
<input type="hidden" name="cmd" value="_xclick"/>
<input type="hidden" name="business" value="商家賬號(hào)"/>
<input type="hidden" name="return" value="返回網(wǎng)址"/>
<input type="hidden" name="cancel_return" value="取消返回網(wǎng)址"/>
<input type="hidden" name="notify_url" value="用于接收PayPal發(fā)送的即時(shí)付款通知的URL,必須是有效的URL"/>
<input type="hidden" name="item_name" value="物品名稱"/>
<input type="hidden" name="item_number" value="可用于跟蹤購買或捐贈(zèng)的傳遞變量,在付款完成時(shí)傳回給您"/>
<input type="hidden" name="amount" value="物品的價(jià)格(購物車中所有物品的總價(jià)格"/>
<input type="hidden" name="currency_code" value="幣種"/>
</form>

二、IPN驗(yàn)證部分

<?php
class paypal {
  var $ipn_data = array();     // array contains the POST values for IPN
  var $fields = array();      // 提交給Paypal的表單參數(shù),例:item_name=iPhone 6,quantity=1,amount=499,currency_code=USD等所有表單信息,
                     // 在調(diào)用驗(yàn)證IPN之前事先需要初始化好
  var $paypal_url = 'https://www.paypal.com/cgi-bin/webscr';//sandbox:https://www.sandbox.paypal.com/cgi-bin/webscr
  
  function validate_ipn() {//驗(yàn)證IPN
      // parse the paypal URL
      $url_par=parse_url($this->paypal_url);
      // 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.
      $post_str = '';
      foreach ($_POST as $field=>$value) {
        $this->ipn_data["$field"] = $value;
        $post_str .= $field.'='.urlencode(stripslashes($value)).'&';
      }
      $post_str.="cmd=_notify-validate"; // append ipn command
      // open the connection to paypal
      $fp = fsockopen($url_par[host],"80",$errnum,$errstr,30);
      if(!$fp) {
        // could not open the connection.
        return false;
      } else {
        // Post the data back to paypal
        fputs($fp, "POST ".$url_par[path]." HTTP/1.1\r\n");
        fputs($fp, "Host: ".$url_par[host]."\r\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: ".strlen($post_str)."\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $post_str . "\r\n\r\n");
        // loop through the response from the server and append to variable
        while(!feof($fp)) {
          $this->validate_ipn_response .= fgets($fp, 1024);
        }
        fclose($fp); // close connection
     }
    if (eregi("VERIFIED",$this->validate_ipn_response)) {
      return true;
    } else {
      return false;
    }
  }
}
?>

“PHP如何實(shí)現(xiàn)PayPal支付功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI