通过 RESTful API 发送支付网关 POST 请求

Sending a payment gateway POST request through a RESTful API

我正在尝试将支付网关集成到前端由 AngularJS 和后端 PHP Yii Framework 驱动的网站中。后端是 REST API.

我的网站上有一个页面,其中包含我所有的购买选项。当用户单击任何这些选项旁边的 "Buy" 按钮时,我需要将用户发送到支付网关服务提供商的支付页面以及作为 POST 请求发送的所需详细信息。

为此,我首先要向我的一个 API 发送一个 JSON。 JSON 包含一些与产品相关的详细信息,仅此而已。具体如下。

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

除了金额和产品信息,其他值为空。

一旦 JSON 被 API 接收到,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 的回应时,它 return 正是 JSON 我需要 POST支付网关 URL。现在是我挣扎的部分。我使用最后一行代码将数据作为 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 选项,因为它会引发错误。不知道为什么。此外,在放置所有这些代码之后,当我单击前端的 "buy" 按钮时,API 执行并 return $posted,但我是没有进入支付页面,我不知道数据是否被 posted。对 API 调用的响应没有错误。据我所知,它执行得非常完美。

有人可以帮我解决这个问题吗?我 post 正确地输入了数据?还是我应该 post $hash_string 变量?最后几部分让我很困惑。

严格来说,这并不是我遇到的 CURL 问题的答案。但我想出了一个解决办法。我将参数作为 GET 变量发送到我构建的中间 PHP 页面。然后,我在 PHP 页面中捕获了这些 GET 变量,并将它们作为 POST 请求发送到支付网关。

我不知道为什么 CURL 不起作用,在我与支付网关技术人员交谈后,他们非常反对使用 CURL。所以我尝试使用 AngularJS 本身向网关发送 POST 请求。但这是 Angular 中响应 headers 的另一个巨大(且看似流行)问题。我浏览了很多论坛来寻找解决方案,但对我没有任何帮助。所以我最终决定构建和中间 PHP 页面并按照上述方法进行处理。