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

时间:2015-04-07 06:56:22

标签: php angularjs rest yii payment-gateway

我正在尝试将支付网关集成到由前端的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);**

}

当我回复$ 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变量?最后几部分让我感到困惑。

1 个答案:

答案 0 :(得分:0)

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

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

相关问题