【发布时间】:2015-04-07 06:56:22
【问题描述】:
我正在尝试将支付网关集成到由前端的 AngularJS 和后端的 PHP Yii 框架驱动的网站中。后端是一个 REST API。
我的网站上有一个页面,其中包含我的所有购买选项。当用户单击这些选项旁边的“购买”按钮时,我需要将用户发送到支付网关服务提供商的支付页面以及作为 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 的响应时,它会返回我需要 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 变量?最后几部分让我感到困惑。
【问题讨论】:
-
嘿。非常感谢。我实际上做了一些非常相似的事情。我在 PHP 上创建了一个中间页面,并将变量作为 GET 请求发送。然后我在页面上使用这些 GET 变量将它们作为 POST 请求发送到网关。严格来说不是我的问题的答案,但它有效,这就是我所关心的。 :)
标签: php angularjs rest yii payment-gateway