raw-focus/webshop

为Laravel 10+应用程序提供的一个简单网店模块。

该软件包的规范仓库似乎已消失,因此该软件包已被冻结。

v1.0.4 2023-06-11 12:57 UTC

This package is auto-updated.

Last update: 2024-02-11 16:09:21 UTC


README

这是一个Laravel 10软件包,它为您提供了一个API来处理由Stripe支持的简单网店。

待办事项

  • 产品版本支持(多变体产品)

安装

软件包

composer require raw-focus/webshop:^1.0

环境文件

将以下条目添加到您的.env文件中

# Your Stripe API key
STRIPE_PRIVATE_KEY=

# Your Stripe success & fail URLS where your customer will be redirected to. The endpoints are appended with the /:order-id
STRIPE_SUCCESS_URL=https://:5173/webshop/checkout/succeeded
STRIPE_CANCEL_URL=https://:5173/webshop/checkout/failed

数据库

安装软件包后,您应使用以下命令迁移软件包的迁移文件

php artisan migrate

我们定义了一些用于测试目的的seeds,您可以将其发布、编辑并自行使用

php artisan vendor:publish --tag=webshop-seeders

然后将seeds添加到您的database\seeders\DatabaseSeeder.php文件中

<?php

namespace Database\Seeders;

...

use RawFocus\Webshop\database\seeders\OrderSeeder;
use RawFocus\Webshop\database\seeders\ProductSeeder;

...

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        ...

        $this->call(ProductSeeder::class);
        $this->call(OrderSeeder::class);
    }
}

配置

使用以下命令发布配置文件

php artisan vendor:publish --tag=webshop-config

语言文件

使用以下命令发布语言文件

php artisan vendor:publish --tag=webshop-lang

使用

该软件包主要为您提供ProductOrder模型以及一个API,您可以使用该API来管理这些产品与订单。

对于所有执行的操作,都会触发events,您可以在您的应用程序中监听这些事件。该软件包不提供任何发送电子邮件或其他通常在例如下单后发生的后果。因此,您需要自行构建该功能。

模型

该软件包提供以下模型

Product > ProductVariant > ProductVariantOption
        > ProductImage

Order` > OrderProduct

API端点

  • GET api/webshop/data

  • POST api/webshop/checkout

  • POST api/webshop/checkout/retry

  • GET api/webshop/products

  • GET api/webshop/products/find-by-id/{slug}

  • GET api/webshop/products/find-by-slug/{slug}

  • POST api/webshop/products/create

  • POST api/webshop/products/update

  • POST api/webshop/products/delete

  • GET api/webshop/orders

  • GET api/webshop/orders/find-by-id/{id}

  • GET api/webshop/orders/find-by-uuid/{uuid}

  • POST api/webshop/orders/flag-as-shipped

  • POST api/webshop/orders/flag-as-arrived

  • POST api/webshop/stripe/endpoint

安全

您可以在webshop.php配置文件中指定您想用于保护路由的middleware。默认情况下,该软件包使用auth:sanctum middleware来保护除Stripe webhook之外的所有端点。

Stripe

将以下环境变量添加到您的.env

STRIPE_PRIVATE_KEY=stripe-private-key 
STRIPE_WEBHOOK_SECRET=stripe-webhook-secret # only used when webshop.payments.enable_webhook_signature_validation is set to true

在订单创建后,将创建Stripe结账会话

client_reference_id => order uuid
success_url => success url taken from config("webshop.payments.urls.success") url + order uuid
cancel_url => cancel url taken from config("webshop.payments.urls.cancel") url + order uuid
payment_method_types => list of payment methods, see https://stripe.com/docs/invoicing/payment-methods
metadata => [["source" => env("APP_ENV")]]  
# using the source we can differentiate between the different environments
# useful for when using local listeners for webhooks

并将行项目(产品)添加

price_data => currency, amount, name
quantity => quantity will be displayed to the user
# This ID corresponds to a 21% tax rate. These settings can be managed here: https://dashboard.stripe.com/test/tax-rates
# https://stripe.com/docs/invoicing/taxes/tax-rates
tax_rates => [config("webshop.payments.tax_rates.high")],

结账流程

Alt text

Webhooks

请参阅StripeController.php以获取webhook监听器的信息。

两个重要方法

  1. handlePaymentIntentCreated -> 处理在结账会话创建后立即触发的webhook事件。在此阶段,我们可以将订单支付状态更新为“待处理”。

  2. handleCheckoutEvent -> 处理在尝试进行支付后触发的webhook事件。此方法将支付状态更新为已支付或失败,并处理发送电子邮件等操作。

签名webhook验证

为了防止“重放”攻击,您可以使用webhook签名验证。有关更多信息,请参阅https://stripe.com/docs/webhooks/signatures

请确保将STRIPE_WEBHOOK_SECRET设置为正确的值。您可以在Stripe仪表板上找到此值:[https://dashboard.stripe.com/test/webhooks](https://dashboard.stripe.com/test/webhooks)

并将config("webshop.payments.enable_webhook_signature_validation")的值设置为true。

Stripe本地webhook监听器

  1. 下载正确的Stripe监听器cli exe。更多信息请参阅:[https://stripe.com/docs/stripe-cli](https://stripe.com/docs/stripe-cli) 或直接访问:[https://github.com/stripe/stripe-cli/releases/tag/v1.14.7](https://github.com/stripe/stripe-cli/releases/tag/v1.14.7)
  2. 运行sh stripe login并完成登录流程。它应该会重定向到您的网页浏览器,在那里您应该登录Stripe。
  3. 运行sh stripe listen --forward-to https:///api/webshop/stripe/endpoint(将--forward-to更改为指向您的Laravel webhook控制器)