我正在尝试将支付网关集成到由后端上的前端和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变量?最后几部分让我感到困惑。
你也许该链接会很有用... http://stackoverflow.com/questions/5576619/php-redirect-with-post-data – rikkyrk
嘿。非常感谢。我其实做了非常类似的事情。我在PHP上创建了一个中间页面,并将这些变量作为GET请求发送。然后,我使用这些GET变量在页面上将它们作为POST请求发送到网关。不是严格的回答我的问题,但它的工作,这就是我关心的。 :) – srthu