首页> 实战笔录 >PHP开发笔记 >ThinkPHP ThinkPHP

PayPal支付ThinkPHP3.2源代码

作者:小萝卜 2019-08-09 浏览 2346

简介tp3.2 PayPal支付流程:在网页上点击付款按钮,或者表单提交按钮,post提交通过php代码构造好参数表单,然后post提交到达PayPal的结账页面,输入自己的PayPal用户名和密码并确认支付

tp3.2 PayPal支付流程如下:

1,在网页上点击付款按钮,或者表单提交按钮,post提交到服务器某个对应的脚本

2,通过php代码构造好参数表单,然后post提交到达PayPal的结账页面,输入自己的PayPal用户名和密码并确认支付

3,PayPal会根据是否支付成功来决定返回网站的哪个页面,并在后台对网站的某个页面发起post请求,这个动作称作IPN,告诉网站这笔付款的到账情况,比如completed即为完成付款

4,网站收到PayPal的notify通知后,即可给用户发货或者其他的处理逻辑

这里萝卜就省略了前端的提交表单了代码了(你可以自己构造个按钮),直接上后端php代码:


/* 
前台表单提交到这个方法开始支付
*/
Public function Paypal(){

   $paypal_email = '卖家的账号';    //卖家账号

   $order_sn = $order;            //订单号

   $custom = '12';                //自定义额外参数

   $currency_code = 'USD';        //货币类型

   $charset = 'utf-8';            //编码

   $item_name = '两颗萝卜';        //商品名称

   $amount = 500;            //金额

   $formurl = 'https://www.sandbox.paypal.com/cgi-bin/webscr';//测试提交地址

   //$formurl = 'https://www.paypal.com/cgi-bin/webscr';//正试提交地址

   $weburl = 'http://'.$_SERVER['HTTP_HOST'];

   $notifyurl = $weburl.'/CoUser/Meeting/notifyurl'; //IPN地址

   $cancel_return = $weburl.'/CoUser/Meeting/index';  //用户取消支付后返回的地址

   $return = $weburl.'/CoUser/Meeting/returnurl';  //支付成功后返回的地址

   //更多表单变量,请访问paypal官网

   //http://www.ebay.cn/public/paypal/integrationcenter/list__resource_7.html
   
   //php构造form并提交到paypal
   $html = '< form id="paypalsubmit" action="'.$formurl.'" method="post">';

   $html .= '< input type="hidden" name="cmd" value="_xclick">
             < input type="hidden" name="charset" value="'.$charset.'">
             < input type="hidden" name="business" value="'.$paypal_email.'">
             < input type="hidden" name="receiver_email" value="'.$paypal_email.'">
             < input type="hidden" name="item_name" value="'.$item_name.'">
             < input type="hidden" name="item_number" value="'.$order_sn.'">
             < input type="hidden" name="currency_code" value="'.$currency_code.'">
             < input type="hidden" name="custom" value="'.$custom.'">
             < input type="hidden" name="amount" value="'.$amount.'">
             < input type="hidden" name="notify_url" value="'.$notifyurl.'" />
             < input type="hidden" name="cancel_return" value="'.$cancel_return.'" />
             < input type="hidden" name="return" value="'.$return.'" />';

             $html .= '< /form>< script>document.forms["paypalsubmit"].submit();< /script>';

   echo $html;
 
}


/*IPN即时通知*/
Public function notifyurl() {

   //如果不是POST方式

   if (!IS_POST) exit('非法请求');

   //将POST变量记录在本地变量中

   $data = array();

   $req = 'cmd=_notify-validate';

   foreach ($_POST as $key => $value) {

      $data[$key] = $value;

      $value = urlencode(stripslashes($value));

      $req .= "&$key=$value";

   }

   //将信息POST给paypal验证

   $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";

   $header .= "Content-Type: application/x-www-form-urlencoded\r\n";

   //如果是测试的则用sandbox

   $header .= "Host: www.sandbox.paypal.com\r\n";

   //$header .= "Host: www.paypal.com\r\n";

   $header .= "Content-Length: " . strlen($req) ."\r\n";

   $header .= "Connection: close\r\n\r\n";

   //在sandobx情况下,设置:

   $fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

   //$fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);

   //判断回复POST是否创建成功

   if (!$fp) {

      fclose($fp);

      return false;

   }else {

      //将回复POST信息写入socket端口

      fputs($fp, $header.$req);

      //file_put_contents('data.txt', http_build_query($data), FILE_USE_INCLUDE_PATH);

      //开始接受paypal对回复POST信息的认证信息

      while(!feof($fp)) {

         $res = fgets($fp, 1024);

         //file_put_contents('aa.txt', $res, FILE_APPEND);

         //已通过认证

         if (strcmp($res, 'VERIFIED') == 0) {

            //检查付款状态

            //检查txn_id 是否已经是处理过

            //检查receiver_email是否是您的paypal账户中的email地址

            //检查付款金额和货币单位是否正确

            //处理这次付款,更新订单状态

            fclose($fp);

            return false;

        }else if (strcmp($res, 'INVALID') == 0) {

            //echo 'ERROR';

            //未通过认证,有可能是编码错误的或非法的POST信息

            fclose($fp);

            return false;

        }

     }

     fclose($fp);

     return false;

   }

}


/*页面跳转处理方法

* 在贝宝支付完成后,进行跳转的地址

*/
Public function returnurl() {
    $meet=M('Meeting');
    $tx = I('get.tx', '');   //paypal交易号

    $st = I('get.st', '');   //支付状态

    $amt = I('get.amt', ''); //付款金额

    $cc = I('get.cc', '');   //货币类型

    $cm = I('get.cm', '');  //额外自定义参数

    $order_sn = I('get.item_number', '');    //订单号
    //判断交易状态
    if($_POST['payment_status']=='Completed' || $_POST['payment_status']=='completed'){
        $data['pay']=1;
        $meet->where('orders='.$_POST['item_number'])->save($data);
        echo "< script>alert('支付成功');location.href='".__ROOT__."/CoUser/Meeting'< /script>";
        exit;
    }else{
        $data['pay']=3;
        $meet->where('orders='.$_POST['item_number'])->save($data);
        echo "< script>alert('支付失败');location.href='".__ROOT__."/CoUser/Meeting'< /script>";
        exit;
    }

   // print_r($_POST); 
} 

/* 
*用户取消支付后跳到该地址 
*/ 
public function index(){ 
   $meet=M('Meeting'); 
   $user=M('user'); 
   $data=$meet->where('uid='.$_COOKIE['user']['id'])->find(); 
   if($data){ 
      $this->assign('data',$data); 
   } 
   $this->display(); 
} 
 

很赞哦! (0)

文章评论

    高端网站建设