使用codeigniter Webhook PHP

时间:2017-04-18 09:10:39

标签: php codeigniter stripe-payments webhooks

我有这个代码用于我的webhook条带

\Stripe\Stripe::setApiKey("you_api_key");

$postdata = @file_get_contents("php://input");
$event = json_decode($postdata);
if ($event->type == 'invoice.payment_succeeded') {
    $customer_id = $event->data->object->customer;
    $customer = \Stripe\Customer::retrieve($customer_id);
    $invoice = \Stripe\Invoice::retrieve($event->data->object->id);

    // This is where we'd normally e-mail the invoice, but we'll just write out the invoice to a file instead.
    $from = "From: Me";
    $to = "To: ".$customer->email;
    $subject = "Subject: You have made a payment for another month of Wilde quotes";
    $body = "You have made a new payment for $".($invoice->total / 100.0).":\n\n";

    foreach($invoice->lines->data as &$line) {
        if ($line->type == 'subscription') {
            $body .= "Subscription - ".$line->plan->name.": ".$line->amount."\n";
        }
        else if ($line->type == 'invoiceitem') {
            $body .= "Additional -".$line->description.": ".$line->amount;
        }
    }

    $email_file = fopen($customer->id."-".$invoice->date, 'a');
    $email = $from."\n".$to."\n".$subject."\n".$body;
    fwrite($email_file, $email);
}

我的问题是,如何通过webhook条带接收电子邮件订阅或电子邮件费用

1 个答案:

答案 0 :(得分:0)

您的webhook检索哪些事件取决于条纹信息中心中的设置。

我建议您接收所有事件类型并使用if语句,因为您在代码中对您感兴趣的事件作出反应。确保您的应用程序始终返回200 OK状态或Stripe将重试webhook。

这里有一个发送电子邮件以回应付款失败的示例: https://stripe.com/docs/recipes/sending-emails-for-failed-payments

Stripe发送的所有事件列表如下: https://stripe.com/docs/api/php#event_types

如果您有具体的实施问题或收到错误消息,我建议您通过电子邮件发送Stripe支持,因为它们可以直接帮助您解决有关您的代码和帐户的问题。