【问题标题】:How to integrate instamojo payment gateway with codeigniter rest server?如何将 instamojo 支付网关与 codeigniter 休息服务器集成?
【发布时间】:2018-01-14 04:41:33
【问题描述】:

我正在尝试将 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 支付网站,也没有显示错误。

使用 vanilla CodeIgniter 可以正常工作并成功重定向。

问题:

1) 是否可以从 REST 服务器发布方法中进行重定向?

2) 如果上述情况可行,那么我的代码有什么问题?

3) 有没有其他方法可以实现我想要做的事情?

我在网上找到了很多教程,但都没有使用 REST Server。

【问题讨论】:

    标签: rest codeigniter payment-gateway codeigniter-restserver instamojo


    【解决方案1】:

    我在谷歌搜索时偶然发现了这个问题。我也遇到了同样的问题,下面是我的解决方法。

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

    从 Rest 服务器返回了支付 url,并从 Rest Client 中重定向到了该 url

    休息客户端代码:

    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');
        }
    }
    

    休息服务器代码:

    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 不适用于重定向。 REST API 返回 JSON、XML 等等,接收者负责处理应该做的任何事情。

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

    【讨论】:

    • 谢谢分享。我也在考虑同样的技术……天知道为什么不允许从 Rest Server 重定向……
    • @Raja REST API 不适用于重定向。 REST API 返回 JSON、XML 等,接收者负责处理应该完成的任何事情。
    • @AshwiniChaudhary 非常感谢您的澄清。那么猜猜我们正在朝着正确的方向前进,对吧? :)
    • @Raja 我对 CodeIgniter 不熟悉,但是基本的想法是正确的。
    猜你喜欢
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    相关资源
    最近更新 更多