存储客户订阅ID问题

时间:2016-09-15 02:05:32

标签: php stripe-payments

我在将数据库订阅ID存储到数据库时遇到问题。据我所知,我已根据条带文档做了一切正确的事情,但是我仍然无法存储数据。数据注册为NULL。有人可以帮我解决这个问题吗?请放轻松我,我刚刚在3周前学过PHP,并且在PHP中对OOP相对较新。

谢谢!

创建客户订阅后,这是我的条带webhook

  {
  "id": "evt_18tlxxHPUld5pqVxOX3YKx6g",
  "object": "event",
  "api_version": "2016-06-15",
  "created": 1473892493,
  "data": {
    "object": {
      "id": "sub_9CAIqC9yUTkAPG",
      "object": "subscription",
      "application_fee_percent": null,
      "cancel_at_period_end": false,
      "canceled_at": null,
      "created": 1473892492,
      "current_period_end": 1474497292,
      "current_period_start": 1473892492,
      "customer": "cus_9CAI8co126bZId",
      "discount": null,
      "ended_at": null,
      "livemode": false,
      "metadata": {},
      "plan": {
        "id": "product0",
        "object": "plan",
        "amount": 4700,
        "created": 1472151609,
        "currency": "usd",
        "interval": "month",
        "interval_count": 1,
        "livemode": false,
        "metadata": {
          "subscription": "product0"
        },
        "name": "Product0 Plan",
        "statement_descriptor": null,
        "trial_period_days": null
      },
      "quantity": 1,
      "start": 1473892492,
      "status": "trialing",
      "tax_percent": null,
      "trial_end": 1474497292,
      "trial_start": 1473892492
    }
  },
  "livemode": false,
  "pending_webhooks": 2,
  "request": "req_9CAIgRMh75t4cd",
  "type": "customer.subscription.created"
}

以下是我的webhook.php文件中的相应案例陈述:

  case 'customer.subscription.created':
  //DEFINE EVENT OBJECT IN ACCORDANCE TO STRIPE WEBHOOK DOCUMENTATION
  $event_json = json_decode($input);
  $event = \Stripe\Event::retrieve($event_json->id);
  //END DEFINE EVENT OBJECT

  //ATTEMPTS TO RETRIEVE CUSTOMER INFORMATION FROM WEBHOOK.
  $customer = \Stripe\Customer::retrieve($event->data->object->customer);
  $plan = $event->data->object->plan->id;
  $subscription_id=$event->data->object->id;
    switch($plan)
    {
      case 'product0':
        //GET STRIPE CUSTOMER 
        $customer = \Stripe\Customer::retrieve($event->data->object->customer);
        //GET CUSTOMER EMAIL FROM STRIPE DATABASE
        $email = $customer->email;

        //UPDATE SQL TABLE WITH STRIPE SUBSCRIPTION ID 
        $sql    = "UPDATE users_stripe_infoProduct SET product0 = '$subscription_id' WHERE email = '$email' ";
        $query  = mysqli_query($conn, $sql);

        //SEND EMAIL TO CUSTOMER IF QUERY WAS SUCCESSFUL
        if ($query)
        {
          $email = $customer->email;
          // Sending your customers the amount in pennies is weird, so convert to dollars
          $from    = "billing@anthonymoto.com";
          $to      = $email;
          $subject = "Subscription Created";
          $email_message = 'Hi,<br> We wanted to let you know what your subscription was successfully created!
                      <br><br>
                      If you have already verified your email address, can login and access your content by simply by clicking <a href ="www.anthonymoto.com/login.php">this link. </a>
                      <br><br>

                      <br>
                      Anthony Moto Support Team';
          $headers = "From: " . $from . "\r\n";
          $headers .= "MIME-Version: 1.0\r\n";
          $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
          mail($to,$subject,$email_message, $headers);
        }
      break;

0 个答案:

没有答案