duanpacan9388 2013-07-01 14:53
浏览 118

Laravel 4 Paypal IPN路由示例

I want to use the following PHP IPN package:

https://github.com/mike182uk/paypal-ipn-listener

Now here is the example code from github:

    $request = new PayPal\Ipn\Request\Curl();

$request->secure(true); //dont need to do this as its done by default, just demonstrating configuring the request component

$listener = new PayPal\Ipn\Listener($request);

$listener->setMode('sandbox');

try {
    $status = $listener->verifyIpn();
}
catch (Exception $e) {
    $error = $e->getMessage();
    $status = false;
}

if ($status) {
    // verified...
}
else {
    // invalid...
    $report = $listener->getReport();
}

So since it looks like the request and the listener are executed in the same action, how would this work with routing? I thought you'd set a post or data variable ipn_notification_url to the url where your ipn was at but it looks like if I set it to the same route as this above that it would resend the request?

I just don't see how the initial request is made (thru a form/post?) I'd like it to work with a cart so I'd imagine you'd send it to a route that feeds the cart contents array to the data field to initialize the $request. But with the example above it looks like the ipn listener is in the same route as the initial request?

I'm sorry for the newbness but small examples help noobs along way

  • 写回答

1条回答 默认 最新

  • doudao5287 2013-11-21 15:44
    关注

    Here's how I added this package to my L4 install:

    1: Add the package to your composer.json file in your site root.

    "require" : {
    ... (existing requirements),
    "mike182uk/paypal-ipn-listener": "dev-master"
    }
    
    "autoload" : {
    ... (existing autoload stuff, certainly the classmap array),
    "psr-0": {"PayPal": "src/"}
    

    2: Run composer update from your site root and let it do its thing.

    3: Add a POST route to your routes.php file for PayPal to hit with the IPN:

    Route::post('payment/ipn.php', array('as' => 'paypal.payment-ipn', 'uses' => 'PaymentsController@paypal_ipn'));
    

    4: In your PaymentsController (or whatever the name of the Controller you're using to handle Payments is) add a function named paypal_ipn(). That's where the sample code in the question goes:

    /*
    ==================
        PayPal IPN Stuff
    ==================
    */
    
    public function paypal_ipn(){
    
        $request = new PayPal\Ipn\Request\Curl();
    
        $listener = new PayPal\Ipn\Listener($request);
    
        $listener->setMode('sandbox');
    
        try {
            $status = $listener->verifyIpn();
        }
        catch (Exception $e) {
            $error = $e->getMessage();
            $status = false;
        }
    
        if ($status) {
            // verified...
            error_log('Verified');
        }
        else {
            // invalid...
            $report = $listener->getReport();
        }
    
    }
    

    This works for me. Please let me know if any of it is the wrong way to go.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c