在将表单字段加密为密码后,使用表单字段中的json将数据发送到Web服务

时间:2013-11-12 10:22:51

标签: php json web-services encryption

我正在创建一个网络支付表格。根据pci我不能存储信用卡号码,所以我使用第三方api加密信用卡号码。根据第三方文档,我必须在我的脚本中添加他们的脚本html表单<script type="text/javascript" src="./-client-2.1.2.js"></script>以及表单

的唯一值
<input id="txtEncryptionKey" name="txtEncryptionKey" class="_encryptionkey"
        type="hidden" value="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvWpIQFjQQCPpaIlJKpeg
        irp5kLkzLB1AxHmnLk73D3TJbAGqr1QmlsWDBtMPMRpdzzUM7ZwX3kzhIuATV4Pe
        7RKp3nZlVmcrT0YCQXBrTwqZNh775z58GP2kZs+gVfNqBampJPzSB/hB62KkByhE
        Cn6grrRjiAVwJyZVEvs/********+aE16emtX12RgI5JdzdOiNyZEQteU6zRBRJE
        ocPWVxExaOpVVVJ5+UnW0LcalzA+lRGRTrQJ5JguAPiAOzRPTK/lYFFpCAl/F8wt
        oAVG1c8zO2NcQ0Pko+fmeidRFxJ/did2btV+9Mkze3mBphwFmvnxa35LF+Cs/XJH
        DwIDAQAB" />

并且我希望加密信用卡号码字段:

<input type="text" name="txtCreditCard" id="txtCreditCard" class="_data"

以及提交按钮

<input type="submit" name="btn_process" value="Submit" id="btn_process" class="_submit btn btn-success">

这是我的整个代码

 <html>   
    <head>
     <title>Test Page</title>
    <script type="text/javascript">

     </script>
    <script type="text/javascript" src="./-client-2.1.2.js"></script>
    </head>   
    <body>   
        <h2>Data Collection</h2><p>

    <form action="process.php" method="post">
    <input id="txtEncryptionKey" name="txtEncryptionKey" class="encryptionkey"
    type="hidden" value="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvWpIQFjQQCPpaIlJKpeg
    irp5kLkzLB1AxHmnLk73D3TJbAGqr1QmlsWDBtMPMRpdzzUM7ZwX3kzhIuATV4Pe
    7RKp3nZlVmcrT0YCQXBrTwqZNh775z58GP2kZs+gVfNqBampJPzSB/hB62KkByhE
    Cn6grrRjiAVwJyZVEvs/2vrxaEpO+aE16emtX12RgI5JdzdOiNyZEQteU6zRBRJE
    ocPWVxExaOpVVVJ5+UnW0LcalzA+lRGRTrQJ5JguAPiAOzRPTK/lYFFpCAl/F8wt
    oAVG1c8zO2NcQ0Pko+fmeidRFxJ/did2btV+9Mkze3mBphwFmvnxa35LF+Cs/XJH
    DwIDAQAB" />
        Name: <input type="text" name="name"><br>
        Credit card number: <input type="text" name="credit" id="credit" class="_data"><br>
        <input type="submit" name="btn_process" value="Submit" id="btn_process" class="_submit btn btn-success">
    </form>

    </body>
    </html>

所以这里发生的是,我的信用卡号码是在我点击提交后立即通过javascript获取并在页面中转换为密码文本,它会返回一个独特的密码文本,如下所示

_cipherText=EIQ4H1Tmmxb0wvyfX9HvbSg0SH0ez1GyZSZjQ8OQqKOI8wtY%2B06uq9XlsDSQdmvRtZtCwJv%2FFbo6xxQ4ClPQZN06nO%2BB8Hw3PddPFLqGtViOMCpBif9Tv0LXPy4%2FQ2L%2F5crTjVQa6WdoJABTgFlOcJ8x%2Bs%2FSSmR5Hd7R9SznfpJQp64IQ6FP%2F2ASxpU14YswgDvTumYZ%2BPElbdKG5u71snNWoQNUClWFn4d8yk6%2BaJ%2FDUGWqotpxchhOFvHMePXsdE8%2F2mGlmz5iiOSH5LlvHptenQMtTvHjBuwdMo4rnutjJ%2FRqaR3sWcndZIWYmEZ7OfA%3D%3D

现在使用php我需要将此密码存储到变量中并将其发送到Web服务

<!DOCTYPE html>
<html>
<body>

<?php
function writeMsg()
{
echo "Hello world!";
}

//creates a token.
function Tokenize()
{
// see details here http:/.turnapi.com/docs/1.0/rest-tokenize
$_id = "763994532109974";
$api_key = "0za2fOfdWU8575BnTH";
$encrypted_data = "acAx/CwWGCURIhwf7gIw36TFmXoGFrFa5l9hCgcGEW4/mVQAAzZuT4XRjktb7XR0sAthHTuSPYegNYUy7g1stP+ypfVBcH0hNiI72N22yy3WYp0VUfAKDp33HBgUVQwg0TWAenRSNbUwC0Qv49E5bubYo4YBnERWi4JNLJZPlEQUfjMovvWQsQdFHd7U79XJZnZQdW92CKFDrTX8bCS4/n0LDEEVBILJGBnjnvKOQjQarsX8OuU6/73qpy36f9Gz3+X6IRfRhVbINNV0Seii6qSXT03NyvbERDsU/CiOrZ1tY0RuiKh4rsvCfPYrX2h67ZZ7nzrz0DeV+BYyo0e06A=="; 
// CC data to tokenize..

$data = array(
    'ID' =>_id,
    'APIKey' => $api_key,
    'EncryptedData' => $encrypted_data,
    'TokenScheme' => 4 
);
//convert to JSON
$json = json_encode($data);

 echo "Step1 done... ";
 echo $json;

//curl config
$url = 'https://test-api..com:8081/TokenServices.svc/REST/TokenizeFromEncryptedValue';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                    'Content-Type: application/json', //we are using json in this example, you could use xml as well
                                    'Content-Length: '.strlen($json),
                                    'Accept: application/json')       //we are using json in this example, you could use xml as well
                                    );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

//call web service
$result = curl_exec($ch);

//decode result
$jsonResult = json_decode($result, true);

//handle result
if ($jsonResult['Success'] == FALSE)
{
    echo "Error Message: ";
    echo $jsonResult['Error'];
}
else
{
    echo "your token is: ";
    echo $jsonResult['Token'];
}
}

//writeMsg();
Tokenize();
?>

</body>
</html>

点击此处$encrypted_data = "acAx/CwWGCURIhwf7gIw36TFmXoGFrFa5l9hCgcGEW4/mVQAAzZuT4XRjktb7XR0sAthHTuSPYegNYUy7g1stP+ypfVBcH0hNiI72N22yy3WYp0VUfAKDp33HBgUVQwg0TWAenRSNbUwC0Qv49E5bubYo4YBnERWi4JNLJZPlEQUfjMovvWQsQdFHd7U79XJZnZQdW92CKFDrTX8bCS4/n0LDEEVBILJGBnjnvKOQjQarsX8OuU6/73qpy36f9Gz3+X6IRfRhVbINNV0Seii6qSXT03NyvbERDsU/CiOrZ1tY0RuiKh4rsvCfPYrX2h67ZZ7nzrz0DeV+BYyo0e06A==" 我需要在按下提交按钮时使用的密文。我需要php中的语句将这个密文文本转换为变量$encrypted_data(截至目前我已经给出了一个独立的数据)

另外在php部分中存在一些问题,其中一些有效负载正在使用JSON发送到web服务,但我似乎没有得到响应(即,作为来自webservice的响应发送的令牌,我发送了我的token_id ,api密钥和加密数据

有人可以帮助使这个代码有效吗?我非常熟悉php,我在他们的文档和在线步骤的帮助下完成所有这些。做这个长期的问题,我是一个初学者,所以我必须提出这个完整的。

提前致谢

1 个答案:

答案 0 :(得分:0)

如果CURL请求完全失败,您应该在curl_error($ch)中出错,或者如果它返回一个奇怪的结果,您可以使用curl_info($ch)获取有关上一次请求的信息,因此两者的某些组合应该帮助您找出可能发生错误的位置。