西部数码主机 | 阿里云主机| 虚拟主机 | 服务器 | 返回乐道官网
当前位置: 主页 > php教程 > magento教程 >

magento shell 脚本通过api获取paypal的订单状态

时间:2016-01-21 00:27来源: 作者: 点击:
magento shell 脚本通过api获取paypal的订单状态 ?php /** *Magento * *NOTICEOFLICENSE * *ThissourcefileissubjecttotheOpenSoftwareLicense(OSL3.0) *thatisbundledwiththispackageinthefileLICENSE.txt. *Itisalsoavailablethroughtheworld-wi
magento shell 脚本通过api获取paypal的订单状态
 
  1. <?php  
  2. /**  
  3.  * Magento  
  4.  *  
  5.  * NOTICE OF LICENSE  
  6.  *  
  7.  * This source file is subject to the Open Software License (OSL 3.0)  
  8.  * that is bundled with this package in the file LICENSE.txt.  
  9.  * It is also available through the world-wide-web at this URL:  
  10.  * http://opensource.org/licenses/osl-3.0.php  
  11.  * If you did not receive a copy of the license and are unable to  
  12.  * obtain it through the world-wide-web, please send an email  
  13.  * to license@magentocommerce.com so we can send you a copy immediately.  
  14.  *  
  15.  * DISCLAIMER  
  16.  *  
  17.  * Do not edit or add to this file if you wish to upgrade Magento to newer  
  18.  * versions in the future. If you wish to customize Magento for your  
  19.  * needs please refer to http://www.magentocommerce.com for more information.  
  20.  *  
  21.  * @category    Mage  
  22.  * @package     Mage_Shell  
  23.  * @copyright   Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)  
  24.  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)  
  25.  */  
  26.   
  27. require_once 'abstract.php';  
  28.   
  29. /**  
  30.  * Magento Compiler Shell Script  
  31.  *  
  32.  * @category    Mage  
  33.  * @package     Mage_Shell  
  34.  * @author      Magento Core Team <core@magentocommerce.com>  
  35.  */  
  36.    
  37. class Mage_Shell_SyncPaypalOrderStatus extends Mage_Shell_Abstract  
  38. {  
  39.     const PAGE_SIZE = 50;  
  40.       
  41.     protected function _construct(){  
  42.          
  43.     }  
  44.     public function run()  
  45.     {  
  46.           
  47.          if (isset($this->_args['syncpaypalorderstatus'])) {  
  48.             //echo $this->getPaypalTimeFormat(Mage::getModel("core/date")->date("Y-m-d H:i:s"));  
  49.             //return;  
  50.             $this->syncPaypalOrderStatus();  
  51.             return;  
  52.            
  53.         }else{  
  54.               
  55.             return;  
  56.         }  
  57.           
  58.     }  
  59.     public function getPaypalTimeFormat($time){  
  60.         $time1 = trim(substr($time,0,10));  
  61.         $time2 = trim(substr($time,11));  
  62.           
  63.         return $time1."T".$time2."Z";  
  64.           
  65.     }  
  66.     public function getLastTime($time,$add){  
  67.         $b = date('Y-m-d H:i:s',strtotime($add,strtotime($time)));  
  68.         return $b;  
  69.     }  
  70.     public function syncPaypalOrderStatus(){  
  71.         $STARTDATE = "";  
  72.         $ENDDATE = "";  
  73.         //得到开始时间和结束时间(paypal)  
  74.         $model_count = Mage::getModel("syncpaypalorderstatus/syncpaypalorderstatus")->getCollection()->count();  
  75.         $syncpaypalorderstatus = Mage::getModel("syncpaypalorderstatus/syncpaypalorderstatus");  
  76.         $now_date = Mage::getModel("core/date")->date("Y-m-d H:i:s");  
  77.         if($model_count<1){  
  78.             //在同步记录表中没有数据,则添加初始值。  
  79.             $STARTDATE_d = $this->getLastTime($now_date,"+5 hours");  
  80.             $ENDDATE_d = $this->getLastTime($STARTDATE_d,"+20 minute");  
  81.             $ENDDATE_E = $this->getLastTime($STARTDATE_d,"+10 minute");  
  82.             $STARTDATE = $this->getPaypalTimeFormat($STARTDATE_d);  
  83.             $ENDDATE = $this->getPaypalTimeFormat($ENDDATE_d);  
  84.             $syncpaypalorderstatus->setProcessTime($now_date)  
  85.                         ->setBeginTime($STARTDATE_d)  
  86.                         ->setEndDate($ENDDATE_E)  
  87.                         ->setStatus(1)  
  88.                         ->save()  
  89.                         ;  
  90.         }else{  
  91.         //在同步表中有同步记录,则获取最后一条记录  
  92.             $model_coll = Mage::getModel("syncpaypalorderstatus/syncpaypalorderstatus")->getCollection()->setOrder("id","DESC");  
  93.             $model  = $model_coll->getFirstItem();  
  94.             $status = $model->getStatus();  
  95.             //如果状态是完成,则进行抓取下一个十分钟的数据。  
  96.             if($status+0 == 2){  
  97.                 $STARTDATE_d = $model->getEndDate();  
  98.                 $ENDDATE_d = $this->getLastTime($STARTDATE_d,"+20 minutes");  
  99.                 $ENDDATE_E = $this->getLastTime($STARTDATE_d,"+10 minutes");  
  100.                 $STARTDATE = $this->getPaypalTimeFormat($STARTDATE_d);  
  101.                 $ENDDATE = $this->getPaypalTimeFormat($ENDDATE_d);  
  102.                 $syncpaypalorderstatus->setProcessTime($now_date)  
  103.                         ->setBeginTime($STARTDATE_d)  
  104.                         ->setEndDate($ENDDATE_E)  
  105.                         ->setStatus(1)  
  106.                         ->save()  
  107.                         ;  
  108.             }else{  
  109.             //如果上次抓取失败,则继续抓取上次的记录。  
  110.                 $ENDDATE_d = $model->getBeginTime();  
  111.                 $ENDDATE_d = $model->getEndDate();  
  112.                 $STARTDATE = $this->getPaypalTimeFormat($STARTDATE_d);  
  113.                 $ENDDATE = $this->getPaypalTimeFormat($ENDDATE_d);  
  114.                 $syncpaypalorderstatus = $model;  
  115.             }  
  116.               
  117.         }  
  118.           
  119.         //得到订单列表  
  120.         $TranslateList = $this->getTranslateList($STARTDATE,$ENDDATE);  
  121.         echo "*******************************\n";  
  122.         echo $STARTDATE."\n";  
  123.         echo $ENDDATE."\n";  
  124.         //var_dump($TranslateList);  
  125.         if($TranslateList){  
  126.             //得到只有交易号的数组  
  127.             $TranslateArray = $this->getTranslateArray($TranslateList);  
  128.         }  
  129.         var_dump($TranslateArray);  
  130.         $order_array = array();  
  131.         $i = 0;  
  132.         $increment_array = array();  
  133.         $i_arr = array();  
  134.         foreach($TranslateArray as $translate_id){  
  135.             $order_detial = $this->getjyinfo5($translate_id);  
  136.             $increment_id = $order_detial['INVNUM'];  
  137.             $payment_status = $order_detial['PAYMENTSTATUS'];  
  138.             //$order_array[$increment_id] = $payment_status;  
  139.             $i_arr[] = $increment_id;  
  140.             $order = Mage::getModel('sales/order')->loadByIncrementId(trim($increment_id));  
  141.             if($order->getId()){  
  142.                 echo "increment_id:".$increment_id."\n";  
  143.                 echo "old_statue:".$order->getState()."\n";  
  144.                 $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();  
  145.                 echo "new_statue:".$order->getState()."\n";  
  146.                 $increment_array[] = $increment_id;  
  147.             }  
  148.               
  149.         }  
  150.         var_dump($i_arr);  
  151.         $increment_string = implode(",",$increment_array);  
  152.         $syncpaypalorderstatus->setStatus(2)->setIncrementIds($increment_string)->save();  
  153.         echo "$increment_string is complate\n";  
  154.         //$order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true)->save();  
  155.       
  156.     }  
  157.     //返回所有的交易号数组。  
  158.     public function getTranslateArray($TranslateList){  
  159.         $TRANSACTIONID_ARRAY = array();  
  160.         foreach($TranslateList as $Translate_array){  
  161.             if(is_array($Translate_array)){  
  162.                 for($i=0;$i<=99;$i++){  
  163.                     $L_TRANSACTIONID = "L_TRANSACTIONID".$i;  
  164.                     if(array_key_exists($L_TRANSACTIONID,$Translate_array)){  
  165.                         $TRANSACTIONID_ARRAY[] = $Translate_array[$L_TRANSACTIONID];  
  166.                           
  167.                     }  
  168.                       
  169.                 }  
  170.             }  
  171.         }  
  172.         return $TRANSACTIONID_ARRAY;  
  173.     }  
  174.       
  175.     //根据交易号得到订单的详细信息。  
  176.     public function getjyinfo5($transaction_serial_number)   
  177.     {  
  178.         // Set request-specific fields.  
  179.         $transactionID = urlencode($transaction_serial_number);  
  180.         // Add request-specific fields to the request string.  
  181.         $nvpStr = "&TRANSACTIONID=$transactionID";  
  182.         // Execute the API operation; see the PPHttpPost function above.  
  183.         $httpParsedResponseAr = self::PPHttpPost5('GetTransactionDetails', $nvpStr);  
  184.         if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) {  
  185.             //'GetTransactionDetails Completed Successfully: '.var_dump($httpParsedResponseAr, true);  
  186.             return $httpParsedResponseAr;   //返回些交易号所有信息,包括是否付款成功,客人地址等  
  187.         } else  {  
  188.             //'GetTransactionDetails failed: ' . var_dump($httpParsedResponseAr, true);  
  189.             return "NO GET";  
  190.         }  
  191.   
  192.     }  
  193.       
  194.       
  195.     public function getTranslateList($STARTDATE,$ENDDATE,$trans_list_all_array = array())   
  196.     {  
  197.         // Set request-specific fields.  
  198.         //$transactionID = urlencode($transaction_serial_number);  
  199.         // Add request-specific fields to the request string.  
  200.         $nvpStr = "&STARTDATE=$STARTDATE&ENDDATE=$ENDDATE";  
  201.         // Execute the API operation; see the PPHttpPost function above.  
  202.         $httpParsedResponseAr = self::PPHttpPost5('TransactionSearch', $nvpStr);  
  203.         if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) {  
  204.             //'GetTransactionDetails Completed Successfully: '.var_dump($httpParsedResponseAr, true);  
  205.               
  206.             $trans_list_all_array[] = $httpParsedResponseAr;  
  207.             if(array_key_exists('L_SHORTMESSAGE0',$httpParsedResponseAr)){  
  208.                 if($httpParsedResponseAr['L_SHORTMESSAGE0'] == "Search warning"){  
  209.                     if(array_key_exists('L_TIMESTAMP99',$httpParsedResponseAr)){  
  210.                         $end_TIMESTAMP = $httpParsedResponseAr['L_TIMESTAMP99'];  
  211.                         $trans_list_array = $this->getTranslateList($STARTDATE,$end_TIMESTAMP,$trans_list_all_array);  
  212.                         return $trans_list_array;  
  213.                     }  
  214.                 }  
  215.             }else{  
  216.                   
  217.                 return $trans_list_all_array;   //返回些交易号所有信息,包括是否付款成功,客人地址等  
  218.             }  
  219.         } else  {  
  220.             //'GetTransactionDetails failed: ' . var_dump($httpParsedResponseAr, true);  
  221.             return "NO GET";  
  222.         }  
  223.   
  224.     }  
  225.       
  226.       
  227.       
  228.       
  229.     function PPHttpPost5($methodName_, $nvpStr_,$i = 0)   
  230.     {  
  231.         global $environment;  
  232.         $environment = 'live';  
  233.         // Set up your API credentials, PayPal end point, and API version.  
  234.         $API_UserName = urlencode('');  //这里你找授予权  
  235.         $API_Password = urlencode(''); //这里你找授予权  
  236.         $API_Signature = urlencode(''); //这里你找授予权  
  237.         $API_Endpoint = "https://api-3t.paypal.com/nvp";  
  238.         if("sandbox" === $environment || "beta-sandbox" === $environment) {  
  239.             $API_Endpoint = "https://api-3t.$environment.paypal.com/nvp";  
  240.         }  
  241.         $version = urlencode('51.0');  
  242.       
  243.         // Set the curl parameters.  
  244.         $ch = curl_init();  
  245.         curl_setopt($ch, CURLOPT_URL, $API_Endpoint);  
  246.         curl_setopt($ch, CURLOPT_VERBOSE, 1);  
  247.       
  248.         // Turn off the server and peer verification (TrustManager Concept).  
  249.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
  250.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
  251.       
  252.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  253.         curl_setopt($ch, CURLOPT_POST, 1);  
  254.       
  255.         // Set the API operation, version, and API signature in the request.  
  256.         $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";  
  257.       
  258.         // Set the request as a POST FIELD for curl.  
  259.         curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);  
  260.       
  261.         // Get response from the server.  
  262.         $httpResponse = curl_exec($ch);  
  263.           
  264.           
  265.         if(!$httpResponse) {  
  266.             $i++;  
  267.             if($i>3){  
  268.                 //获取三次失败后,则推出。  
  269.                 exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');  
  270.             }else{  
  271.                 $httpResponse = $this->PPHttpPost5($methodName_, $nvpStr_,$i);  
  272.             }  
  273.         }else{  
  274.             //第一次获取数据失败,则再次获取。并返回、  
  275.             if($i>0){  
  276.                 return $httpResponse;  
  277.             }  
  278.               
  279.         }  
  280.         $httpResponseAr = explode("&", urldecode($httpResponse));  
  281.           
  282.         $httpParsedResponseAr = array();  
  283.         foreach ($httpResponseAr as $i => $value) {  
  284.             $tmpAr = explode("=", $value);  
  285.             if(sizeof($tmpAr) > 1) {  
  286.                 $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];  
  287.             }  
  288.         }  
  289.       
  290.         if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {  
  291.             exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");  
  292.         }  
  293.       
  294.         return $httpParsedResponseAr;  
  295.     }  
  296.       
  297.                              
  298. }  
  299.   
  300. $shell = new Mage_Shell_SyncPaypalOrderStatus();  
  301. $shell->run();  
  302.  

(责任编辑:好模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------