spackleso/spackle-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

spackleso/spackle-php

Composer 安装命令:

composer require spackleso/spackle-php

包简介

Spackle is the easiest way to integrate your PHP app with Stripe Billing. See https://www.spackle.so for details.

README 文档

README

The Spackle PHP library provides optimized access to billing aware flags created on the Spackle platform.

Documentation

See the PHP API docs.

Setup

Install the Spackle library

composer require spackleso/spackle-php

To use the bindings use Composer's autoload:

require_once('vendor/autoload.php');

Configure your environment

In order to use Spackle, you need to configure your secret key on the Spackle singleton. You can find your secret key in Spackle app settings page.

\Spackle\Spackle::setApiKey('<api key>');

Usage

Pricing tables

Fetch a pricing table

\Spackle\PricingTable::retrieve("abcde123");

Pricing table object

{
  id: string
  name: string
  intervals: string[]
  products: {
    id: string
    name: string
    description: string
    features: {
      id: string
      name: string
      key: string
      type: number
      value_flag: boolean
      value_limit: number | null
    }[]
    prices: {
      month?: {
        id: string
        unit_amount: number
        currency: string
      }
      year?: {
        id: string
        unit_amount: number
        currency: string
      }
    }
  }[]
}

Entitlements

Fetch a customer

Spackle uses stripe ids as references to customer features.

$customer = \Spackle\Customer::retrieve("cus_000000000");

Verify feature access

$customer->enabled("feature_key");

Fetch a feature limit

$customer->limit("feature_key");

Examine a customer's subscriptions

A customer's current subscriptions are available on the subscriptions method. These are valid \Stripe\Subscription objects as defined in the Stripe PHP library.

$customer->subscriptions();

Waiters

There is a brief delay between when an action takes place in Stripe and when it is reflected in Spackle. To account for this, Spackle provides a Waiters class with static methods that can be used to wait for a Stripe object to be updated and replicated.

  1. Wait for a customer to be created
    \Spackle\Waiters::waitForCustomer("cus_00000000");
  2. Wait for a subscription to be created
    \Spackle\Waiters::waitForSubscription("cus_000000000", "sub_00000000");
  3. Wait for a subscription to be updated
    \Spackle\Waiters::waitForSubscription("cus_000000000", "sub_00000000", array("status" => "active"));

These will block until Spackle is updated with the latest information from Stripe or until a timeout occurs.

Usage in development environments

In production, Spackle requires a valid Stripe customer. However, that is not development environments where state needs to be controlled. As an alternative, you can use a file store to test your application with seed data.

/app/spackle.json

{
  "cus_000000000": {
    "features": [
      {
        "type": 0,
        "key": "flag_feature",
        "value_flag": true
      },
      {
        "type": 1,
        "key": "limit_feature",
        "value_limit": 100
      }
    ],
    "subscriptions": [
      {
        "id": "sub_000000000",
        "status": "trialing",
        "quantity": 1
      }
    ]
  }
}

Then configure the file store in your application:

\Spackle\Spackle::setStore(new \Spackle\Stores\FileStore("/app/spackle.json"));

Usage in testing environments

In production, Spackle requires a valid Stripe customer. However, that is not ideal in testing or some development environments. As an alternative, you can use an in-memory store to test your application with seed data.

\Spackle\Spackle::setStore(new \Spackle\Stores\MemoryStore());
\Spackle\Spackle::getStore()->set_customer_data("cus_000000000", array(
  "features" => array(
    array(
      "type" => 0,
      "key" => "flag_feature",
      "value_flag" => true
    ),
    array(
      "type" => 1,
      "key" => "limit_feature",
      "value_limit" => 100
    )
  ),
  "subscriptions" => array(
    array(
      "id" => "sub_000000000",
      "status" => "trialing",
      "quantity" => 1
    )
  )
);

Note: The in-memory store is not thread-safe and state will reset on each application restart.

spackleso/spackle-php 适用场景与选型建议

spackleso/spackle-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 04 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 spackleso/spackle-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 spackleso/spackle-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 spackleso/spackle-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 29
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-08