PayPal's Restful-api Way refunds, web payments, callbacks

Source: Internet
Author: User

The use of records.

1. One of the easiest ways to use PayPal in a mobile app that you want to pay in PayPal (Web version).

   

This is the way to pay when the experience is not very good, easy 500 error, network restrictions.


2. After the payment is completed that is the callback (the PayPal callback mechanism used here):

/**     * PAYPAL Callback Notification      */     Public function paypal ()     {        $ Ipn = file_get_contents (' php://input ');//       $ipn  =  "mc_ gross=399.00&protection_eligibility=eligible&address_status=confirmed&payer_id=xemq4lgll3e8u& address_street=%bf%c6%d4%b0%c2%b72%ba%c5a8%d2%f4%c0%d6%b4%f3%cf%c3&payment_date=19%3a18%3a52+apr+07%2c+ 2017+pdt&payment_status=completed&charset=gb2312&address_zip=518000&first_name=li&mc_fee= 15.86&address_country_code=cn&address_name=%c9%ee%db%da%ca%d0%a1%a1%ca%a5%c2%eb%bf%c6%bc%bc&notify _version=3.8&custom=664&payer_status=verified&business=yu.yu-facilitator%40zmartec.com&address _country=china&address_city=%c9%ee%db%da%ca%d0&quantity=1&verify_sign= Aomrs5l2w2xlt2an.gasrazpcl-nai.rf7w.qgkzsuidf5rmnpdPvdzw&payer_email=zmartec%40zmartec.com&txn_id=32u88448k71697337&payment_type=instant&last_ name=tao&address_state=guangdong&receiver_email=yu.yu-facilitator%40zmartec.com&payment_fee=15.86 &receiver_id=PEAD5Y5KWXRJU&txn_type=web_accept&item_name=SIGMA%2BSD1000&mc_currency=USD& Item_number=&residence_country=us&test_ipn=1&transaction_subject=&payment_gross=399.00&ipn _track_id=b2b22318a3e9 ";         vendor (' PaypalIPN.PaypalIPN ')  or  die ("Scenario 7 introduced failure");         $PIPN  = NEW \PAYPALIPN ();          $IPN _response =  $pipn->verifyipn ($IPN);                 //  Verification Through & The conversion data type is array        if  (true ===  $ipn _response)  {              $raw _post_array = explode (' & ',  $IPN);              $myPost  = array ();             foreach  ($raw _post_array as  $keyval )  {                $ Keyval = explode (' = ',  $keyval);                 if  (Count ($keyval)  == 2)  {                     //  Convert UrlEncode + to%2b                     if  ($keyval [0] ===  ' payment_date ')  {                         if  (Substr_count ($ keyval[1],  ' + ')  === 1)  {                              $keyval [ 1] = str_replace (' + ',  '%2b ',  $keyval [1]);                         }                     }                       $myPost [$keyval [0]] = urldecode ($keyval [1]);                 }             }             $fields  =  "od. ' uid ', od. ' Currency ', OD. ' product_id ' ";             $order _detail  =  $this->_model->orderdetail ($myPost [' Custom '],  $fields);             //  Card number iccid              $iccid  =  $this->_model->geticcidbyid ($order _detail[0][' uid ');p Rint_r ($iccid );             $iccid  =  $iccid [0][' Iccid ' ];            //  Package id              $product _id =  $this->_model-> Getproductidbyid ($order _detail[0][' product_id ');              $product _ID =  $product _id[0][' product_id '];            //  ① Check payment_status status             if  (" Completed " ==  $myPost [' payment_status '])  {                 // ② ensure txn_id does not repeat                  if  ($this->_model->checkpaypaltxn ($myPost [' txn_id ']))  {                     // ③ Make sure the message has not been changed                      if  ($this->paypayl_receiver_email ==   $myPost [' Receiver_email '])  {                         // ④ ensure that prices, goods and orders meet                          if  ($order _detail[0][' order_total '] ==  $myPost [' Mc_gross '] &&  $order _detail[0][' Currency '] ==  $myPost [' mc_currency '])  {                               $data  = array (                                   ' status ' = ' Completed ',                                   ' Payment_methods '  =>  ' PayPal ',                                  ' Trade_ No ' = ' $myPost [' txn_id '],                                  ' Changed_time ' =>time ()                              );                               $pay _result =  $this->_model->saveorderdata (  $data  ,  $myPost [' Custom '] );                              $this->hong_cha_package_order ($ order_detail[0][' out_trade_no '],  $myPost [' Custom '],  $product _id,  $iccid,  $order _detail[0][ ' Product_amount ']);                         } else {                              echo  "Currency, price does not match the order, please contact the administrator.";                          }                     } else {                         echo  "The order information has been modified, please contact the administrator.";                      }                }  else {                     echo  "The order has been processed, do not repeat.";                 }             } else {                 echo  "Order not paid to complete.";             }         } else {            echo   "Data is black and cannot be verified through.";         }    } 

    /**     * paypal Refunds      */     public function paypal_refund ()     {          $txn _id = i (' txn_id ');         $id  = i (' id ');        if  ($this->is_data_valid (Array ($ txn_id))  {             $url  =  " https://api.sandbox.paypal.com/v1/payments/sale/$txn _id/refund ";              $post _data =  "{}";              $access _token =  $this->ger_access_token ();              $header  = array (                  "Content-type: application/json",                  "authorization: bearer  $access _token"              );              $output  = http_post_paypal ($url,  $post _data,  $header);                 if  (false  ===  $output  | |  false ===  $access _token)  {                  $this->_data[' data '] =  ';                  $this->_data[' ERROR '] = true;                  $this->_ Data[' mEssage '] =  ' network exception, please try again later ';             }                  $output  =  json_decode ($output,  true);             if  ($txn _id ==  $output [' sale_id '] &&  ' completed '  ==  $output [' Completed '])  {                  $this->_model->saveorderdata ( array (' status ' = ' refund_success ',  ' changed_time ' = Time ()),  $id  );                  $this->_data[' data '] =  ';                  $this->_data[' ERROR '] = false;                  $this->_data[' message '] =  ' refund submitted ';             } elseif  ("transaction_refused"  == $ output[' name '])  {                  $this->_data[' data '] =  ';                  $this->_data[' ERROR '] = false;                  $this->_data[' message '] =   ' refunds have been submitted, do not repeat ';            }         }         $this->response ($this->_ data,  ' json ');     }

/**     * curl post request   Use as PayPal callback      * @ param string  $url      *  @param  string  $post _data      *  @return  array     */    function  http_post_paypal ($url,  $post _data,  $header =array (),  $client _id= "',  $secret = ')   {             $ch  = curl_init ($url);         curl_setopt ($ch, curlopt_customrequest,  "POST");         curl_setopt ($ch, curlopt_postfields,  $post _data);         if  ($client _id &&  $secret)  {             curl_setopt ($ch, curlopt_userpwd,  "[$client _id]:[$secret]"); //  passes the user name and password required in a connection in the format: "[Username]:[password]".         }        curl_setopt ($ ch, curlopt_returntransfer, true)  ; //  get Data back          curl_setopt ($ch, curlopt_httpheader,  $header)  ; //  on  curlopt_ enabled returntransfer  will get the data back to Array (' Expect: ')          $output  =  curl_exec ($ch);                 if  ( curl_errno ($ch)  ) {             return false;        }             return  $output;     }

PS: Refunds need to get tokens, have encountered problems can refer to another article http://tengteng412.blog.51cto.com/4751263/1917336

This article from "for the Future" blog, declined reprint!

PayPal's Restful-api Way refunds, web payments, callbacks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.