如何将instamojo支付网关与codeigniter rest服务器集成?

如何将instamojo支付网关与codeigniter rest服务器集成?,rest,codeigniter,payment-gateway,codeigniter-restserver,instamojo,Rest,Codeigniter,Payment Gateway,Codeigniter Restserver,Instamojo,我正在尝试将Instamojo支付网关集成到Chris Kacerguis的REST服务器中 问题: 以下代码: public function instamojotest_post() { $api = new Instamojo\Instamojo(‘abcd1234’, ‘efgh5678’, 'https://test.instamojo.com/api/1.1/'); try { $response = $api->paymentRequestC

我正在尝试将Instamojo支付网关集成到Chris Kacerguis的REST服务器中

问题:

以下代码:

public function instamojotest_post()
{
    $api = new Instamojo\Instamojo(‘abcd1234’, ‘efgh5678’, 'https://test.instamojo.com/api/1.1/');

    try {
        $response = $api->paymentRequestCreate([

            'amount'    => 100,
            'purpose'   => 'New Product Purchase',
            'buyer_name'    => 'Test User',
            'email' => 'testuser@gmail.com',
            'phone' => '9876543210',
            'redirect_url'  => 'http://www.example.com/products_api/validate_payment'
        ]);

        header('Location: ' . $response['longurl']);

    } catch (Exception $e) {

        $this->response([
            'success'   => false,
            'message'   => $e->getMessage()
        ], 500);
    }
}
未重定向到Instamojo支付网站,且未显示任何错误

它工作良好,并成功重定向与香草CodeIgniter

问题:

1) 是否可以从REST服务器Post方法中重定向

2) 如果以上是可能的,那么我的代码有什么问题

3) 有没有其他方法可以实现我的目标


我在网上找到了很多教程,但没有一本是使用REST服务器的。

我在谷歌搜索时无意中发现了这个问题。我也面临着同样的问题,下面是我如何解决它的

注意:这不完全是一种解决方案,而是一种变通方法。我也承认,这可能不是最好的解决方案,但它对我很有效

从Rest服务器返回支付url,并从Rest客户端重定向到url

Rest客户端代码:

class Test extends CI_Controller
{
    public function instamojo_make_payment()
    {
        $url = "http://www.example.com/products_api/instamojotest";
        $params = []; //You will obviously be needing this in real life implementation :)

        $curl_handle = curl_init();
        curl_setopt($curl_handle, CURLOPT_URL, $url);
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_handle, CURLOPT_POST, 1);
        curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $params);
        $response = curl_exec($curl_handle);
        curl_close($curl_handle);

        if ($response['success'])
            header('Location: ' . $response['payment_url']);
        else
            $this->load->view('payment_failed_page');
    }
}
Rest服务器代码:

class Products_api extends REST_Controller
{
    public function instamojotest_post()
    {
        $api = new Instamojo\Instamojo('abcd1234', 'efgh5678', 'https://test.instamojo.com/api/1.1/');

        try {
            $response = $api->paymentRequestCreate([
                //Make sure to pass these data from the Rest Client
                'amount'    => 100,
                'purpose'   => 'New Product Purchase',
                'buyer_name'    => 'Test User',
                'email' => 'testuser@gmail.com',
                'phone' => '9876543210',
                'redirect_url'  => 'http://www.example.com/products_api/validate_payment'
            ]);

            $this->response([
                'success'   => true,
                'payment_url'   => $response['longurl']
            ], 200);

        } catch (Exception $e) {

            $this->response([
                'success'   => false,
                'message'   => $e->getMessage()
            ], 500);
        }
    }
}
在给出这个答案时,我假设Api是开放的。如果不是,则确保在进行curl调用时传递您的凭据

更新

感谢@AshwiniChaudhary在下面的评论,其中指出:

REST API不用于重定向。RESTAPI返回JSON、XML 等等,接受者负责所有应该做的事情


“为什么REST服务器不让我们执行重定向”这一事实背后的实际原因变得非常清楚。

谢谢分享。我也在想同样的技术。。。天知道为什么不允许从Rest服务器重定向…@Raja Rest API不适用于重定向。RESTAPI返回JSON、XML等,接收者会处理所有应该做的事情。@AshwiniChaudhary非常感谢您的澄清。那么我猜我们正在朝着正确的方向前进,对吗?:)@Raja我不熟悉CodeIgniter,但基本想法是正确的。