Stripe与PayPal Webhooks-如何检索数据

时间:2018-07-03 12:26:39

标签: php json paypal webhooks

我正遇到类似的问题,就像我在此帖子中发现的:PHP Listener Script for Paypal Webhooks。问题是,给出的答案并没有太大帮助,我不确定我需要做什么。

在我的网站上,我正在使用STRIPE进行结帐。一切工作正常,网络钩子也按预期运行。现在,我已经将PayPal Express Checkout集成为第二种付款方式。设置单笔付款或定期付款都可以。现在,我需要使用PayPal Webhooks更新有关定期付款的数据库。我进行了很多搜索,但是找不到如何从Webhook检索数据的解决方案。

STRIPE:这是一个有关在单笔付款成功后如何使用Stripe Webhook的示例:

<?php
include_once('init.php');

\Stripe\Stripe::setApiKey("$stripe_publish_key_live");
// Retrieve the request's body and parse it as JSON:
$input = @file_get_contents('php://input');
$event_json = json_decode($input);

//====== SINGLE PAYMENT SUCCEEDED =========
if ($event_json->type == 'charge.succeeded') {
    $charge_id = $event_json->data->object->id;
    $customer = $event_json->data->object->customer;
    $date = $event_json->data->object->created;
    $price = $event_json->data->object->amount;
    $user = $event_json->data->object->metadata->user_id;
    $firstname = $event_json->data->object->metadata->firstname;
    $lastname = $event_json->data->object->metadata->lastname;
    $comments = $event_json->data->object->description;

    $userinfo=getUserInfo($user,0);

    $content=array("invoice"=>$charge_id,
        "user"=>$user,
        "stripe_cust_id"=>$customer,
        "stripe_sub_id"=>0,
        "billing_date"=>$date,
        "plan"=>0,
        "price"=>$price,
        "stamp_created"=>$date,
        "period_start"=>$date,
        "period_end"=>$userinfo["delivery_first"][0],
        "status"=>'paid',
        "printed"=>0);
    insertGWInfo('billing',$content);
}

?>

因此,一旦触发了webhook charge.succeeded,我就会在结算表中插入新的一行。正如我对STRIPE所说的那样,它工作正常。我现在想对PayPal Webhooks做完全一样的事情。我去了贝宝(PayPal)的开发人员中心,创建了一个新的Rest API APP,然后输入了我的Webhook URL。现在,我不知道我的PHP代码需要什么样才能将结果作为json获得。我正在使用来自GitHub的Paypal PHP SDK,这是到目前为止我尝试过的工作:

<?php
include_once('../../autoload.php');

$webhook = new \PayPal\Api\Webhook();

$webhook== file_get_contents('php://input');
$webhook_array = json_decode($webhook, true);

//====== SINGLE PAYMENT SUCCEEDED =========
if ($webhook_array->type == 'PAYMENT.SALE.COMPLETED') {
    $charge_id = $webhook_array->data->object->id;
    $customer = $webhook_array->data->object->customer;
    $date = $webhook_array->data->object->created;
    $price = $webhook_array->data->object->amount;
    $user = $webhook_array->data->object->metadata->user_id;
    $firstname = $webhook_array->data->object->metadata->firstname;
    $lastname = $webhook_array->data->object->metadata->lastname;
    $comments = $webhook_array->data->object->description;
    $userinfo=getUserInfo($user,0);

    $content=array("invoice"=>$charge_id,
        "user"=>$user,
        "stripe_cust_id"=>$customer,
        "stripe_sub_id"=>0,
        "billing_date"=>$date,
        "plan"=>0,
        "price"=>$price,
        "stamp_created"=>$date,
        "period_start"=>$date,
        "period_end"=>$userinfo["delivery_first"][0],
        "status"=>'paid',
        "printed"=>0);
    insertGWInfo('billing',$content);
?>

我缺少什么或需要更改什么?有人可以帮我吗?

0 个答案:

没有答案