custom stripe plugin

时间:2016-02-27 作者:RiotAct

我正在使用pippins插件中的一些代码将stripe集成到wordpress中。我有一些自定义功能,我需要在付款后运行。代码运行得很好,除了一件事:当我添加一个webhook来条带化时,它会失败,并出现302错误。代码如下:

<?php

function pippin_stripe_event_listener() {

if(isset($_GET[\'wps-listener\']) && $_GET[\'wps-listener\'] == \'stripe\') {

    global $stripe_options;

    require_once(STRIPE_BASE_DIR . \'/lib/Stripe.php\');

    if(isset($stripe_options[\'test_mode\']) && $stripe_options[\'test_mode\']) {
        $secret_key = $stripe_options[\'test_secret_key\'];
    } else {
        $secret_key = $stripe_options[\'live_secret_key\'];
    }

    Stripe::setApiKey($secret_key);

    // retrieve the request\'s body and parse it as JSON
    $body = @file_get_contents(\'php://input\');

    // grab the event information
    $event_json = json_decode($body);

    // this will be used to retrieve the event from Stripe
    $event_id = $event_json->id;

    if(isset($event_json->id)) {

        http_response_code(200); // PHP 5.4 or greater

        try {

            // to verify this is a real event, we re-retrieve the event from Stripe 
            $event = Stripe_Event::retrieve($event_id);
            $invoice = $event->data->object;

            // successful payment
            if($event->type == \'charge.succeeded\') {
                // send a payment receipt email here

                // retrieve the payer\'s information
                $customer = Stripe_Customer::retrieve($invoice->customer);
                $email = $customer->email;

                $amount = $invoice->amount / 100; // amount comes in as amount in cents, so we need to convert to dollars

                $subject = __(\'Payment Receipt\', \'pippin_stripe\');
                $headers = \'From: "\' . html_entity_decode(get_bloginfo(\'name\')) . \'" <\' . get_bloginfo(\'admin_email\') . \'>\';
                $message = "Hello " . $customer_name . "\\n\\n";
                $message .= "You have successfully made a payment of " . $amount . "\\n\\n";
                $message .= "Thank you.";

                wp_mail($email, $subject, $message, $headers);
            }

            // failed payment
            if($event->type == \'charge.failed\') {
                // send a failed payment notice email here

                // retrieve the payer\'s information
                $customer = Stripe_Customer::retrieve($invoice->customer);
                $email = $customer->email;

                $subject = __(\'Failed Payment\', \'pippin_stripe\');
                $headers = \'From: "\' . html_entity_decode(get_bloginfo(\'name\')) . \'" <\' . get_bloginfo(\'admin_email\') . \'>\';
                $message = "Hello " . $customer_name . "\\n\\n";
                $message .= "We have failed to process your payment of " . $amount . "\\n\\n";
                $message .= "Please get in touch with support.\\n\\n";
                $message .= "Thank you.";

                wp_mail($email, $subject, $message, $headers);
            }

        } catch (Exception $e) {
            // something failed, perhaps log a notice or email the site admin
        }
    }
}
}
add_action(\'init\', \'pippin_stripe_event_listener\');
这是我的事件侦听器:(显然是我自己的域)

http://domain.com/?wps-listener=stripe

这些是pippins网站上的说明,然而,这些是2012年写的,所以从那以后可能有些变化。我问皮平自己,他说其他人也有类似的问题,但不知道原因是什么。

1 个回复
最合适的回答,由SO网友:RiotAct 整理而成

好吧,在浪费了两天的时间后,我发现由于我的站点需要用户登录,所以它将stripe重定向到登录页面。现在我删除了这个功能,stripe就可以连接到我的网站了。