nagorik/stripe

Stripe支付的一次性和周期性支付系统的终极解决方案

1.0.7 2024-08-28 12:50 UTC

This package is auto-updated.

Last update: 2024-09-28 07:03:39 UTC


README

Nagorik Tech Logo

关于Nagorik Stripe

欢迎使用Nagorik Stripe包!此Composer包旨在简化将Stripe支付服务集成到您的Laravel应用程序中的过程。无论是构建电子商务平台、基于订阅的服务还是简单地处理支付,此包旨在使过程顺畅且易于开发。

  • 创建支付URL。
  • 创建订阅。
  • 创建价格。
  • 创建发票。
  • 创建产品。
  • 订阅webhook管理。
  • 支付验证。
  • 完全管理的仪表板。
  • 管理用户订阅。
  • 取消订阅。
  • 发送订阅发票。
  • 下载发票和收据
  • 等等...

安装Nagorik/Stripe

接下来,运行Composer命令安装最新稳定版本

composer require nagorik/stripe

发布配置文件

php artisan vendor:publish --tag=ngstripe

设置配置

您必须更改配置文件:路径:config/ngstripe.php

<?php
    return [
        /**
        * Set pacakge base url
        * Example: https://example.com
        * Your hosted url where you want to use this package
        */
        "base_url" => env("BASE_URL", "https://22c1-103-203-93-214.ngrok-free.app"),

        /**
        * Set app icon
        * Example: /ngstripe/icons/favicon.png
        * Favicon path
        */
        "app_icon" => "/ngstripe/icons/favicon.png",
        "fav_icon" => "/ngstripe/icons/favicon.png",

        /**
        * Assets/resource path will use https if enable
        * Redirection will use https if enable
        */
        "https" => true,

        /**
        * Set payment information
        * api_key: Stripe api secret key [secret_key]
        * redirect_url: After payment success where to redirect default is null
        * go_back_url: After payment where to redirect by click on back button on payment success/failed page. default is /
        * txn_prefix: Its very important. If you have multiple project on same stripe account then you must set this. Its should be unique for each project.
        * subscription_holding_days: After subscription expired, how many days you want to hold the subscription. default is 1 day
        */

        "payment" => [
            "api_key" => "sk_test_51MEW0yLRN9lOZ6qkcEBkuprYMsI51MEW0yLRN9lOZ6qkcEBkuprYMsI51MEW0yLRN9lOZ6qkcEBkuprYMsI",
            "redirect_url" => null,
            "go_back_url" => "/",
            "txn_prefix" => "ns_chobi_ai",
            "subscription_holding_days" => 1
        ],

        /**
        * Notificaiton settings
        * class: Class name with namespace
        * method: Method name
        * This method will call after payment success/failed
        */
        "notify" => [
            'class' => 'App\\Http\\Controllers\\StripeController',
            'method' => 'receivedPayment'
        ],

        /**
        * Webhook settings
        * class: Class name with namespace
        * method: Method name
        * This method will call after payment success/failed
        */
        "webhook" => [
            'class' => 'App\\Http\\Controllers\\StripeController',
            'method' => 'receivedWebhook'
        ],

        /**
        * Package settings
        * Info: Its your own local database table. Where are you storing your package information.
        * table: Table name
        * primary_key: Primary key of the table like id.
        * Uses: This package will use this table to get package information and store payment information and manage subscription.
        * Its very important to set this table and primary key.
        */
        'package' => [
            'table' => 'packages',
            'primary_key' => 'id',
        ],

        /**
         * Message settings
         * You can change the messages here
         */
        "message" => [
            "payment" => [
                "success" => [
                    "title" => "Payment Done!",
                    "message" => "Your payment has been successfully completed.",
                ],
                "failed" => [
                    "title" => "Payment Failed!",
                    "message" => "Your payment has been failed.",
                ],
            ],
            "subscription" => [
                "success" => [
                    "title" => "Subscription Done!",
                    "message" => "Your subscription has been successfully completed.",
                ],
                "failed" => [
                    "title" => "Subscription Failed!",
                    "message" => "Your subscription has been failed.",
                ],
            ],
        ]
    ];

安装

要将此包安装和配置到您的Laravel应用程序中

php artisan ngstripe:install

用法

要使用此包,请在任何PHP文件中使用此命名空间

use Nagorik\Stripe\NgStripe;

创建支付URL

// create instance of the ngstripe class
$ngStripe = new NgStripe();

// provide the datas
$data = [
    "package_id" => $id, // set package id from your package table
    "price" => 10, // set checkout price
    "user_id" => 1, // user id
    "currency" => "usd", // default usd [must use which currency is supported by stripe]
    "user_email" => 'user@gmail.comd', // user email
    "discount" => 10, // discount if need otherwise comment it
    "discount_type" => "flat", // discount type [flat, percentage] if need
    "coupon" => "test-coupon", // if you have set any coupon system on your system
    "redirect" => false, // if its enable, then you will get the object of your payment data otherwise you will get only payment url
];

// make payment url
$url = $ngStripe->createPayment($data);

// if redirect = false set
// $data = $data->data;
// $url = $data->url;
// return redirect($url)

// finallay return the url
return $url;

创建订阅URL

// create instance of the ngstripe class
$ngStripe = new NgStripe();

// provide the datas
$data = [
    "package_id" => $id, // set package id from your package table
    "price" => 10, // set checkout price
    "user_id" => 1, // user id
    "currency" => "usd", // default usd [must use which currency is supported by stripe]
    "user_email" => 'user@gmail.comd', // user email
    "discount" => 10, // discount if need otherwise comment it
    "discount_type" => "flat", // discount type [flat, percentage] if need
    "recurring" => "day", // must use [day, month, year]
    "recurring_period" => 1, // must use reccurning period
    "coupon" => "test-coupon", // if you have set any coupon system on your system
    "redirect" => false, // if its enable, then you will get the object of your payment data otherwise you will get only payment url
];

// make payment url
$url = $ngStripe->createSubscription($data);

// if redirect = false set
// $data = $data->data;
// $url = $data->url;
// return redirect($url)

// finallay return the url
return $url;

在支付/订阅后,您将收到通知

// If you have already set the notify array block on your config/ngstripe.php
// This notify will return from ngstripe pacakge

// you will received
$data = [
   "status" => "paid", // payment status
   "type" => "payment", // payment type [payment, subscription]
   "user_id" => "1", // payment user id [your base table User table id]
   "package_id" => "1", // payment pacakge id [Your package table id]
   "txn_id" => "ns_chobi_ai-3iYOf7HlyF", // payment txn ID
   "pay_id" => "cs_test_a1O2K4OQO98FxvqdO2EcVRXpdcbJV19XXbeW2vVkbdAMkZ6az5RtNCJTjy" // payment pay id from stripe
];

在支付/订阅后,您将收到webhook

// If you have already set the webhook array block on your config/ngstripe.php
// This webhook will return from the stripe dashboard

// you will received data
$data = [
  "invoiceId" => "in_1OJrEvLRN9lOZ6qkn30UqFMP",
  "price_id" => "price_1OJrEmLRN9lOZ6qkzMNGS4GQ",
  "charge" => "ch_3OJrEvLRN9lOZ6qk0rytDcVp",
  "amount" => 9,
  "txn_id" => "ns_chobi_ai-g0i3OJsaws",
  "app_prefix" => "ns_chobi_ai",
  "subscription" => "sub_1OJrEvLRN9lOZ6qkC89vTMSi",
  "invoiceUrl" => "https://invoice.stripe.com/i/acct_1MEW0yLRN9lOZ6qk/test_YWNjdF8xTUVXMHlMUk45bE9aNnFrLF9QODdUWUhXMFp1RnlRbGE0NnYyYkVhQ3JDVEtZSXV4LDkyMjk1MTM30200YeTEifzV?s=ap"
]; // you will get and json endcode string from stripe

方法

获取用户订阅


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgString();

$userId = 1; // set your user id
$subscriptions = $ngStripe->userSubscription($id);

// its return the all subscriptions as an array

获取用户单个套餐订阅


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgString();

$userId = 1; // set your user id
$packId = 1; // set your package id
$subscriptionPackage = $ngStripe->userPackageSubscription($id, $packId);

// it will return the the subscription if found or null

检查用户是否已订阅


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgString();

$userId = 1; // set your user id
$packId = 1; // set your package id [Optional]
$isSubscribed = $ngStripe->isSubscriber($id, $packId = null);

// it will return boolean value [true, false]

检查用户所有交易


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgString();

$userId = 1; // set your user id
$packId = 1; // set your package id [Optional]
$txns = $ngStripe->userTransactions($id, $packId = null);

// it will return transactions list of an user

检查用户所有支付交易


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgString();

$userId = 1; // set your user id
$packId = 1; // set your package id [Optional]
$paymentTxns = $ngStripe->userPaymentTransactions($id, $packId = null);

// it will return payment transactions list of an user

检查用户所有订阅交易


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgString();

$userId = 1; // set your user id
$packId = 1; // set your package id [Optional]
$subscriptionTxns = $ngStripe->userSubscriptionTransactions($id, $packId = null);

// it will return subscription transactions list of an user

检查用户所有用户发票


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgStripe();

$userId = 1; // set your user id
$packId = 1; // set your package id [Optional]
$invoices = $ngStripe->userInvoices($id, $packId = null);

// it will return invoices list of an user

获取交易列表


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgStripe();

$status = null; // Optional [Status: 1, 2, 0] default: null
$paginate = true; // Optional [Paginate: true, false] default: true
$perPage = 10; // Optional [Perpage: 10] default: 10

$transaction = $ngStripe->txnLists($status, $paginate, $perPage);

// transaction lists

获取交易详情


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgStripe();

$txn_id = "txn_id"; // your txn id
$transaction = $ngStripe->getTxn($txn_id);

// it will return transaction details

按优惠券获取交易


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgStripe();

$coupon = "test-coupon"; // your txn id
$userId = 1; // [optional] if want to filter against any user id but its optional
$transaction = $ngStripe->getTxnByCoupon($coupon, $userId);

// it will return transaction lists

下载交易发票


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgStripe();

$txnId = "txn_id"; // your txn id
$transaction = $ngStripe->downloadInvoice($txnId);

// it will download the invoice

获取订阅详情


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgStripe();

$subscription_id = "subscription_id"; // your subscription id
$subscription = $ngStripe->getSubscriptionDetails($subscription_id);

// it will return subscription details

获取发票详情


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgString();

$invoice_id = "invoice_id"; // your invoice id
$invoice = $ngStripe->getInvoice($invoice_id);

// it will return invoice pdf url

订阅检查

您可以使用此命令检查用户的订阅有效性

php artisan ngstripe:check-subscription

您可以将此命令设置在您的app/Console/karnel.php文件中,并通过cron或supervisor在服务器上运行"php artisan schedule:run"。

以下示例

    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
        $schedule->command('ngstripe:check-subscription')->dailyAt("00:00");
    }

取消订阅

取消用户所有订阅


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgString();

$userId = 1; // set your user id
$subscriptionTxns = $ngStripe->cancelAllSubscription($userId);

// it will return 
$data = [
  "total_subscription" => 0,
  "total_cancelled" => 0,
  "total_cancelled_failed" => 0
];

通过订阅ID取消用户的订阅


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgString();

$userId = 1; // set your user id
$subscriptionId = 1; // set your user subscription id
$subscriptionTxns = $ngStripe->cancelSubscription($userId, $subscriptionId);

// it will return boolean value [true, false]

取消单个套餐的所有订阅


use Nagorik\Stripe\NgStripe;
$ngStripe = new NgString();

$userId = 1; // set your user id
$packageId = 1; // set your package id
$subscriptionTxns = $ngStripe->cancelPackageSubscription($userId, $packageId);

// it will return 
$data = [
  "total_subscription" => 0,
  "total_cancelled" => 0,
  "total_cancelled_failed" => 0
];

卸载

要从您的Laravel应用程序中卸载此包

php artisan ngstripe:uninstall

最后,从composer.json中移除

composer remove nagorik/stripe

安全漏洞

如果您在此包中发现安全漏洞,请通过munna@nagorik.tech将电子邮件发送给Munna Ahmed。所有安全漏洞都将得到及时处理。

许可

Nagorik Stripe包是开源软件,许可协议为MIT许可