【问题标题】:How do I cancel a monthly recurring charge after the first 30 days have been filled?在前 30 天已满后,如何取消每月定期收费?
【发布时间】:2019-12-31 00:33:14
【问题描述】:

我正在使用 stripe 创建订阅计划,一切进展顺利,但我想创建一个取消订阅按钮,我已通过 stackoverflow 查找答案,但找不到任何帮助、任何有用的资源或代码非常感谢!

高级.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 注入问题,请联系!

【问题讨论】:

标签: php mysql stripe-payments


【解决方案1】:

创建订阅时,应将订阅 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();

【讨论】:

  • 抱歉,我没用过他们的按钮。我使用 stripe.js 只是为了获取信用卡号,我们使用 PHP API 来处理其他所有事情。
  • 我已经添加了您的代码,但这样做时我收到了一个红色的 X。这是我放置它之后的位置:WHERE user_id = {$user-&gt;user_id} "); 在第二条 stmt 行上,我收到一个红色 X
  • 我在获取会话变量的问题中有一些拼写错误。现在试试,我改用$user-&gt;user_id
  • 先生,代码应该放在哪里?我们在添加它的地方遇到错误。
  • 当他们点击你的取消按钮时,代码进入你运行的脚本中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
  • 2018-02-19
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多