如何从控制器操作重定向发布请求?



我一直在Cakephp中集成支付网关。以下是使用的版本:

  • 蛋糕:3.4.13

  • PHP: 5.6.15

问题:

我需要使用POST请求向支付网关网站提交表格,但某些字段包含机密数据(商家ID等(,因此我无法在表格中显示它们。否则,有人可以使用开发人员工具(检查元素(读取它们。此外,在提交表单之前,我还需要添加一些自定义字段。

因此,我想使用控制器操作的 POST 请求将用户重定向到带有必填字段的支付网关网站。

我试图找到解决方案,但没有成功。在这里发现了一个类似的问题,但没有答案。有什么方法可以在 Cakephp 3.x 中做同样的事情吗?

支付网关形式:

<?= $this->Form->create(false, ['url' => <URL>, 'id'=>'payForm']) ?>
<?= $this->Form->hidden('payment_notification_url', ['value'=> $this->Url->build('/payment/notify', true)]); ?>
<?= $this->Form->hidden('payment_redirect_url', ['value'=>$this->Url->build('/payment/getMoney', true)]); ?>
<?= $this->Form->hidden('merchant_id', ['value'=> <merchant_id>]); ?>
<?= $this->Form->hidden('reference', ['value'=> <reference>]); ?>
<?= $this->Form->hidden('email', ['value'=> <email>]); ?>
<?= $this->Form->hidden('fname', ['value'=> <first_name>]); ?>
<?= $this->Form->hidden('lname', ['value'=> <last_name>]); ?>
<?= $this->Form->hidden('address', ['value'=> <address>]); ?>
<?= $this->Form->hidden('town', ['value'=> <state>]); ?>
<?= $this->Form->hidden('country', ['value'=> <country>]); ?>
<?= $this->Form->hidden('postcode', ['value'=> <zipcode>]); ?>
<?= $this->Form->hidden('amount', ['class' => 'amount', 'value'=> <amount>]); ?>
<?= $this->Form->hidden('currency', ['value'=> 'US']); ?>
<?php echo $this->Form->end() ?>

阅读文档可能会有所帮助。您可以将表单提交到特定操作,然后从那里您可以使用以下代码提出http请求。

有关完整文档 Http 客户端

use CakeHttpClient;
$http = new Client();
// Simple get
$response = $http->get('http://example.com/test.html');
// Simple get with querystring
$response = $http->get('http://example.com/search', ['q' => 'widget']);
// Simple get with querystring & additional headers
$response = $http->get('http://example.com/search', ['q' => 'widget'], [
'headers' => ['X-Requested-With' => 'XMLHttpRequest']
]);

要重定向到外部网址,您可以使用此网址

$this->redirect('http://www.google.com');

最新更新