(Laravel)无法从PayPal IPN获得响应,但模拟的响应返回已经过验证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(Laravel)无法从PayPal IPN获得响应,但模拟的响应返回已经过验证相关的知识,希望对你有一定的参考价值。

我已经在我的网站上的会员页面添加了一个PayPal插件,我使用沙盒模式付款,但我没有从PayPal获得任何IPN响应,但我可以模拟IPN响应并返回握手验证

我已经转到我的帐户>销售首选项> IPN并在那里更新了URL,并且在我使用的插件中我已经将notify_url位更改为IPN网址

插件:https://github.com/srmklive/laravel-paypal

在我下面的IPN处理程序中,我将post响应插入到数据库表中,这样我就可以看到我是否从PayPal获得了任何内容

插件配置

return [
    'mode'    => env('PAYPAL_MODE', 'sandbox'), // Can only be 'sandbox' Or 'live'. If empty or invalid, 'live' will be used.
    'sandbox' => [
        'username'    => env('PAYPAL_SANDBOX_API_USERNAME', ''),
        'password'    => env('PAYPAL_SANDBOX_API_PASSWORD', ''),
        'secret'      => env('PAYPAL_SANDBOX_API_SECRET', ''),
        'certificate' => env('PAYPAL_SANDBOX_API_CERTIFICATE', ''),
        'app_id'      => 'APP-80W284485P519543T', // Used for testing Adaptive Payments API in sandbox mode
    ],
    'live' => [
        'username'    => env('PAYPAL_LIVE_API_USERNAME', ''),
        'password'    => env('PAYPAL_LIVE_API_PASSWORD', ''),
        'secret'      => env('PAYPAL_LIVE_API_SECRET', ''),
        'certificate' => env('PAYPAL_LIVE_API_CERTIFICATE', ''),
        'app_id'      => '', // Used for Adaptive Payments API
    ],

    'payment_action' => 'Sale', // Can only be 'Sale', 'Authorization' or 'Order'
    'currency'       => 'GBP',
    'notify_url'     => 'https://www.mywebsite.com/paypal/ipn', // Change this accordingly for your application.
    'locale'         => '', // force gateway language  i.e. it_IT, es_ES, en_US ... (for express checkout only)
    'validate_ssl'   => false, // Validate SSL when creating api client.
];

IPN处理程序

public function ipn(Request $request)
{
    $data = [
        'post' => json_encode($request->all()),
        'created_at' => date('Y-m-d H:i'),
    ];
    // Import the namespace SrmklivePayPalServicesExpressCheckout first in your controller.
    $provider = new ExpressCheckout;

    $request->merge(['cmd' => '_notify-validate']);
    $post = $request->all();

    $response = (string) $provider->verifyIPN($post);
    $data['response'] = $response;

    DB::table('ipn_responses')->insert($data);

    if ($response === 'VERIFIED') {
        // Your code goes here ...
    }
}
答案

事实证明我使用的插件是错误的。我需要在success_url上“支付”付款

因此,例如,当PayPal将我返回到网站时,付款尚未完成。以下代码处理付款然后检查。我一直以为它是通过IPN发送的

/**
 * Payement success
 * @param Request $request
 * @return IlluminateHttpRedirectResponse
 */
public function success(Request $request)
{
    $token = $request->get('token');
    $PayerID = $request->get('PayerID');
    $cart = $this->getCartDataFromToken($token);

    // Verify Express Checkout Token
    $response = $this->provider->getExpressCheckoutDetails($token);

    // Check express checkout was a success
    if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {

        // Perform transaction on PayPal
        $response = $this->provider->doExpressCheckoutPayment($cart, $token, $PayerID);
        $order = $this->finalizeOrder($cart, $response);

        if($order->paid) {

            $order->user->startMembership($order->membership);

            FormResponse::set('Your order has been successfully completed and your membership has begun. You may check your My Account page to view your membership status.');
        }
        else {
            FormResponse::set('Your payment could not be completed (Error 2)');
        }
    }
    else {
        FormResponse::set('Your payment could not be completed (Error 1)');
    }

    return redirect()->route('memberships');
}

以上是关于(Laravel)无法从PayPal IPN获得响应,但模拟的响应返回已经过验证的主要内容,如果未能解决你的问题,请参考以下文章

从 PayPal 的沙盒 IPN 获得无效响应

没有 IPN 的 Paypal 集成

如何使用 sandbox.paypal 获取 IPN 进行支付

Paypal 自适应支付 IPN 费用信息

Paypal IPN 模拟器问题

Laravel Paypal IPN 与 localtunnel 随机 400 错误请求