2015-04-07 98 views
0

我正在嘗試將支付網關集成到由後端上的前端和PHP Yii Framework上的AngularJS驅動的網站中。後端是一個REST API。通過RESTful API發送支付網關POST請求

我的網站上有我的所有購買選項。當用戶點擊「購買」按鈕以及任何這些選項時,我需要將用戶發送到支付網關服務提供商的支付頁面以及作爲POST請求發送的所需詳細信息。

要做到這一點,首先我要發送一個JSON給我的一個API。這個JSON包含了一些產品相關的細節,就是這個。它如下。

$scope.payment = { 
    key: '', 
    txnid: '', 
    amount: '1250', 
    productinfo: '3', 
    firstname: '', 
    email: '', 
    phone: '', 
    surl: '', 
    furl: '', 
    hash: '', 
    service_provider: '' 
    }; 

除金額和產品信息外,其他所有值均爲空。

API接收到此JSON後,API將解碼此JSON並將其填充爲其他所有值。 API代碼如下。

public function actionMakePayment() { 

    $returnInfo = array("requireLogin"=>false); 

    if (!$this->isLogedIn()) { 
     $returnInfo['requireLogin'] = true; // Checking if user is logged in 
    } else { 
     $userId = Yii::app()->user->id; // Getting user id 
     $currentUserModel = User::model()->findByPk($userId); // Extracting user model 
     $email = $currentUserModel->email; // Extracting email ID 
     $phone = $currentUserModel->contact_no; // Extracting contact number 
     $first_name = $currentUserModel->first_name; // Extracting first name 

     $action = ''; 

     $json = file_get_contents('php://input'); // Taking in the posted JSON 
     $posted = json_decode($json, true); // Decoding JSON 

     $MERCHANT_KEY = "XXXXXX"; // Setting merchant key 
     $SALT = "XXXXXXXX"; // Setting merchant SALT 
     $PAYU_BASE_URL = "https://paymentgateway.com"; // Gateway domain name 
     $SERVICE_PROVIDER = "service_provider"; // Gateway provider ID 

     $RETURN_URL = "http://domain.com/rest/api/resolvePayment"; // Setting URL for redirection after payment is made or cancelled 

     $formError = 0; // POST data error check 

     // Assigning txnid 
     if (empty($posted['txnid'])) { 
      $txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20); 
      $posted['txnid'] = $txnid; 
     } else { 
      $txnid = $posted['txnid']; 
     } 

     $posted['key'] = $MERCHANT_KEY; // assigning the merchant key 
     $posted['surl'] = $RETURN_URL; // assigning success URL 
     $posted['furl'] = $RETURN_URL; // assigning failure URL 
     $posted['service_provider'] = $SERVICE_PROVIDER; // assigning 
     $posted['firstname'] = $first_name; // assigning name 
     $posted['phone'] = $phone; // assigning contact number 
     $posted['email'] = $email; 

     $hash = ''; 
     $hashSequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10"; 

     if (empty($posted['hash']) && sizeof($posted) > 0) { 
      if (
       empty($posted['key']) || 
       empty($posted['txnid']) || 
       empty($posted['amount']) || 
       empty($posted['firstname']) || 
       empty($posted['email']) || 
       empty($posted['phone']) || 
       empty($posted['productinfo']) || 
       empty($posted['surl']) || 
       empty($posted['furl']) || 
       empty($posted['service_provider']) 
      ) { 
       $formError = 1; 
      } else { 
       $hashVarsSeq = explode('|', $hashSequence); 
       $hash_string = ''; 

       foreach($hashVarsSeq as $hash_var) { 
        $hash_string .= isset($posted[$hash_var]) ? $posted[$hash_var] : ''; 
        $hash_string .= '|'; 
       } 

       $hash_string .= $SALT; 

       $hash = strtolower(hash('sha512', $hash_string)); 
       $posted['hash'] = $hash; 
       $action = $PAYU_BASE_URL . '/_payment'; 
      } 

     } else if (!empty($posted['hash'])) { 
      $hash = $posted['hash']; 
      $action = $PAYU_BASE_URL . '/_payment'; 
     } 
    } 

    echo json_encode($posted); 

    **$this->send_post($action, $posted);** 

} 

當我回復$ posted作爲對API的響應時,它將返回確切的JSON,我需要POST到支付網關URL。現在是我掙扎的部分。我使用最後一行代碼將數據作爲POST請求發送到URL $ action。函數「send_post」的代碼如下。我不得不註釋掉CURLOPT_FAILonerror選項,因爲它會引發錯誤。不知道爲什麼。另外,在完成所有這些代碼之後,當我點擊前端的「購買」按鈕時,API會執行並返回$ posted,但我沒有轉到付款頁面,我不知道數據發佈與否。對API調用的響應沒有錯誤。它盡我所能地完美執行。

有人可以幫助我嗎?我正確發佈數據?或者我應該發佈$ hash_string變量?最後幾部分讓我感到困惑。

+0

你也許該鏈接會很有用... http://stackoverflow.com/questions/5576619/php-redirect-with-post-data – rikkyrk

+0

嘿。非常感謝。我其實做了非常類似的事情。我在PHP上創建了一箇中間頁面,並將這些變量作爲GET請求發送。然後,我使用這些GET變量在頁面上將它們作爲POST請求發送到網關。不是嚴格的回答我的問題,但它的工作,這就是我關心的。 :) – srthu

回答

0

這不完全是對我遇到的CURL問題的回答。但我想到了一個解決方法。我將參數作爲GET變量發送到我構建的中間PHP頁面。然後我在PHP頁面中捕獲這些GET變量,並將它們作爲POST請求發送到支付網關。

我不知道CURL爲什麼不起作用,並且在與支付網關技術人員交談後,他們非常反對使用CURL。所以我嘗試使用AngularJS本身向網關發送POST請求。但是這是Angular中響應頭的另一個巨大(並且看似很受歡迎)的問題。我經歷了很多論壇來檢查解決方案,但沒有爲我工作。所以我最終決定構建和中間的PHP頁面,並按上面所述進行處理。