繁体   English   中英

满满前30天后,如何取消每月的经常性费用?

[英]How do I cancel a monthly recurring charge after the first 30 days have been filled?

我正在使用Stripe创建订阅计划,并且一切进展顺利,但是我想创建一个cancel订阅按钮,我已经通过stackoverflow查看了答案,但是我找不到任何帮助,任何有用的资源或代码都非常有用感谢!

premium.php:

<?php
require_once 'app/init.php';
include 'app/dbh.inc.php';
?>
<html>
<body>
    <p>You're about to go premium.</p>
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Premium</title>
      </head>
      <body>
    <form action="premium_charge.php" method="POST">
      <script
        src="https://checkout.stripe.com/checkout.js"
        class="stripe-button"
        data-key="<?php echo $stripe['publishable']; ?>"
        data-name="Connect Kitty"
        data-description="Catwalk"
        data-email="<?php echo $user->email; ?>"
        data-amount="599"
        data-currency="usd">
      </script>
    </form>
  </body>
</html>

premium_charge.php:

<?php

 require_once 'app/init.php';

if(isset($_POST['stripeToken'])) {

   $token = $_POST['stripeToken'];

   try {

      Stripe_Charge::create([
      "amount" => 599,
      "currency" => "usd",
      "source" => $token,
      "description" => $user->email
      ]);
      $db->query("
        UPDATE tbl_twitter_user SET premium = 1
        WHERE user_id = {$user->user_id}
      ");

   } catch(Stripe_CardError $e) {

   }
   header('Location: index1.php');
   exit();
}
?>

init.php:

<?php
session_start();

require_once 'vendor/autoload.php';

$stripe = [
   'publishable' => 'pk_test_QR1JpboiLh5acjEhK6vwclar00N1Y0Evjd',
   'private' => 'sk_test_N62K1YeWqBN1WyEsWmK149Rh00It8OTxqg'
];

Stripe::setApiKey($stripe['private']);

$db = new PDO('mysql:host=127.0.0.1;dbname=am', 'nt', 'f9');

$userQuery = $db->prepare("
   SELECT user_id, username, email, premium
   FROM tbl_twitter_user
   WHERE username = :username
");

$userQuery->execute(['username' => $_SESSION['username']]);

$user = $userQuery->fetchObject();
?>

我已经包含了所有代码,因为我不确定我需要使用哪个文件。

如果有人担心SQL注入!

创建订阅时,应将订阅ID保存在数据库中。 当用户要求取消订阅时,请从数据库中获取其订阅ID并调用Stripe API:

$stmt = $db->prepare("SELECT subscription_id FROM tbl_twitter_user WHERE user_id = :userid");
$stmt->execute(['user_id' => $user->user_id]);
$row = $stmt->fetch(PDO::FETCH_OBJ);
$sub = \Stripe\Subscription::retrieve($row->subscription_id);
$sub->cancel();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM