无法在 CodeIgniter 4 中进行 PayPal 交易后更新数据库 [英] Can't update database after PayPal transaction in CodeIgniter 4

查看:51
本文介绍了无法在 CodeIgniter 4 中进行 PayPal 交易后更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Codeigniter 4 中集成了 PayPal,但在交易后我无法获取更新数据库的数据

I integrating PayPal in Codeigniter 4 but I can't get data after the transaction for an updated database

我想要做的是从 IPN 侦听器获得 PayPal 响应,以便我可以相应地修改我的数据库,但无论我做什么,它都不起作用.我已经在我的 PayPal Sandbox 帐户中完成了以下操作:

What I want to do is to get a PayPal response from the IPN listener so that I can modify my database accordingly, but no matter what I do, it just won't work. I have already done the following in my PayPal Sandbox account:

  1. 启用自动返回
  2. 设置自动返回 URL(仪表板")
  3. 启用支付数据传输 (PDT)
  4. 启用 IPN 消息接收
  5. 设置 IPN URL ('http:myDomain/Controller/index')重定向到自动返回 URL 也无法正常工作,交易后,用户无法返回商店,直到单击返回按钮.

在此处输入代码

我无法获取数据库中的数据,我正在发送数据

I can't get data in the database, I am sending data

        function index()
{
    $raw_post_data = file_get_contents('php://input');
    $raw_post_array = explode('&', $raw_post_data);
    $myPost = array();
    foreach ($raw_post_array as $keyval) {
        $keyval = explode('=', $keyval);
        if (count($keyval) == 2)
            $myPost[$keyval[0]] = urldecode($keyval[1]);
    }
    $req = 'cmd=_notify-validate';
    if (function_exists('get_magic_quotes_gpc')) {
        $get_magic_quotes_exists = true;
    }
    foreach ($myPost as $key => $value) {
        if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
            $value = urlencode(stripslashes($value));
        } else {
            $value = urlencode($value);
        }
        $req .= "&$key=$value";
    }
    if (sandbox == true) {
        $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
    } else {
        $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
    }
    $ch = curl_init($paypal_url);
    if ($ch == FALSE) {
        return FALSE;
    }
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    if (DEBUG == true) {
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
    }
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

    $res = curl_exec($ch);
    if (curl_errno($ch) != 0) // cURL error
    {
        if (DEBUG == true) {
            error_log(date('[Y-m-d H:i e] ') . "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
        }
        curl_close($ch);
        exit;
    } else {
        // Log the entire HTTP response if debug is switched on.
        if (DEBUG == true) {
            error_log(date('[Y-m-d H:i e] ') . "HTTP request of validation request:" . curl_getinfo($ch, CURLINFO_HEADER_OUT) . " for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
            error_log(date('[Y-m-d H:i e] ') . "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
        }
        curl_close($ch);
    }
    // Inspect IPN validation result and act accordingly
    // Split response headers and payload, a better way for strcmp
    $tokens = explode("\r\n\r\n", trim($res));
    $res = trim(end($tokens));
    if (strcmp($res, "VERIFIED") == 0) {
        // assign posted variables to local variables
        $paypalInfo    = $this->input->post();
        $aData = array();
        $item_name = $_POST['item_name'];
        $data['product_id']    = $paypalInfo["item_number"];
        $data['txn_id']    = $paypalInfo["txn_id"];
        $data['payment_gross'] = $paypalInfo["mc_gross"];
        $data['currency_code'] = $paypalInfo["mc_currency"];
        $data['payer_email'] = $paypalInfo["payer_email"];
        $data['payment_status']    = $paypalInfo["payment_status"];

        // check whether the payment_status is Completed
        $isPaymentCompleted = false;
        if ($paypalInfo["payment_status"] == "Completed") {
            $isPaymentCompleted = true;
        }
        if ($isPaymentCompleted) {
            $oPaypalBuilder = $this->modelHelper->getBuilder('Paypal');
            $oPaypalBuilder->skipValidation();
            $oPaypalBuilder->insert($data);
        }
        // process payment and mark item as paid.

        if (DEBUG == true) {
            error_log(date('[Y-m-d H:i e] ') . "Verified IPN: $req " . PHP_EOL, 3, LOG_FILE);
        }
    } else if (strcmp($res, "INVALID") == 0) {
        // log for manual investigation
        // Add business logic here which deals with invalid IPN messages
        if (DEBUG == true) {
            error_log(date('[Y-m-d H:i e] ') . "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
        }
    }
}

我无法获取数据库中的数据,我正在发送数据

I can't get data in the database, I am sending data

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr"
        method="post" class="d-none" id="paypal" target="_top">
        <input type='hidden' name='business'
            value=''> <input type='hidden'
            name='item_name' value=''> <input type='hidden'
            name='item_number' value=''> <input type='hidden'
            name='amount' value=''> <input type='hidden'
            name='no_shipping' value=''> <input type='hidden'
            name='currency_code' value=''> <input type='hidden'
            name='notify_url'
            value=''>
        <input type='hidden' name='cancel_return'
            value=''>
        <input type='hidden' name='return'
            value=''>
        <input type="hidden" name="cmd" value="_xclick">
    </form>

推荐答案

重定向到自动返回 URL 也无法正常工作,交易完成后,用户必须单击返回按钮才能返回商店.

The redirect to Auto Return URL not working fine too, after the transaction, the user can't back to the store until clicking the return button.

这将始终是您使用的纯 HTML(无 API)集成的问题,永远无法保证返回.PayPal 可能有法律义务向付款人出示收据,因此他们可能不会点击返回,或者他们的浏览器可能在他们返回之前直接崩溃.

This will always be an issue with the HTML-only (no API) integration you are using, the return is never guaranteed to happen. PayPal may be legally obligated to show the payer a receipt, and so they may not click to return, or their browser might simply crash before they return.

为确保发生返回并且您的服务器收到已完成交易的通知,您应该切换到基于 API 的适当服务器端集成.

To ensure that a return happens and that your server is notified of completed transactions, you should switch to a proper API-based server-side integration.

在您的服务器上设置两条路线,一条用于创建订单",另一条用于捕获订单",记录在此处:https://developer.paypal.com/docs/business/checkout/server-side-api-calls/ .成功后,Capture Order 路由应该会更新您的数据库.

Set up two routes on your server, one for 'Create an Order' and one for 'Capture Order', documented here: https://developer.paypal.com/docs/business/checkout/server-side-api-calls/ . On success, the Capture Order route should update your database.

与您的两条路线配对的最佳审批流程是:https://developer.paypal.com/demo/checkout/#/pattern/server

The best approval flow to pair with your two routes is: https://developer.paypal.com/demo/checkout/#/pattern/server

这篇关于无法在 CodeIgniter 4 中进行 PayPal 交易后更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆