[英]How to send a POST Request to an external API payment Gateway in woocomerce?
[英]Sending a payment gateway POST request through a RESTful API
我正在嘗試將支付網關集成到由前端AngularJS和后端PHP Yii Framework驅動的網站中。 后端是REST API。
我的網站上有一個頁面,其中包含所有購買選項。 當用戶單擊這些選項中任意一個旁邊的“購買”按鈕時,我需要將用戶以及作為POST請求發送的所需詳細信息發送到付款網關服務提供商的付款頁面。
為此,首先我要向我的一個API發送JSON。 該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);**
}
當我回顯$ post作為對API的響應時,它將返回我需要POST到付款網關URL的確切JSON。 現在是我正在努力的部分。 我正在使用最后一行代碼將數據作為POST請求發送到URL $ action。 函數“ send_post”的代碼如下。
private function send_post($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
// curl_setopt($ch, CURLOPT_FAILonerror, TRUE); //Fail on error
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // return into a variable
curl_setopt($ch, CURLOPT_POST, TRUE); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // add POST fields
$result = curl_exec($ch); // run the whole process
curl_close($ch);
return $result;
}
我不得不注釋掉CURLOPT_FAILonerror選項,因為它引發了錯誤。 不知道為什么。 同樣,在放置所有這些代碼之后,當我單擊前端上的“購買”按鈕時,該API會執行並返回$ posted,但是我沒有進入付款頁面,而且我不知道數據是否發布。 對API調用的響應沒有錯誤。 據我所知,它執行得很好。
有人可以幫我弄這個嗎? 我正確地發布了數據嗎? 還是我應該發布$ hash_string變量? 最后幾部分使我感到困惑。
嚴格來說,這並不是我遇到的CURL問題的答案。 但是我想了個辦法。 我將參數作為GET變量發送到了我構建的中間PHP頁面。 然后,我在PHP頁面中捕獲了這些GET變量,並將它們作為POST請求發送到付款網關。
我不知道CURL為什么不起作用,在與支付網關技術人員交談后,他們非常反對使用CURL。 因此,我嘗試使用AngularJS本身將POST請求發送到網關。 但這是Angular中響應標頭的另一個巨大(且似乎很流行)的問題。 我參加了很多論壇來檢查解決方案,但對我來說沒有任何用處。 因此,我最終決定構建並中介PHP頁面,並如上所述進行工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.