生成 Delphi Stripe 客户端
Stripe 在公共互联网上发布了最完整的 OpenAPI 规范之一——超过 500 个端点,涵盖支付、计费、身份、终端和 Connect。将其输入 sgcOpenAPI,您即可获得一个强类型的 Delphi 单元,镜像该规范,其中请求和响应类已预先填充、身份验证已接入、分页也已为您处理。
Stripe 在公共互联网上发布了最完整的 OpenAPI 规范之一——超过 500 个端点,涵盖支付、计费、身份、终端和 Connect。将其输入 sgcOpenAPI,您即可获得一个强类型的 Delphi 单元,镜像该规范,其中请求和响应类已预先填充、身份验证已接入、分页也已为您处理。
Stripe 发布并维护官方 OpenAPI 3 规范。sgcOpenAPI 的解析器和代码生成器将其直接转换为可在 Delphi 7 到 RAD Studio 13 上编译的 Pascal 代码。
Bearer 密钥 + 幂等性键
sgcOpenAPI_Stripe
Windows、macOS、Linux、iOS、Android
从 Stripe 的公共仓库下载最新的 spec3.json,并使用 TsgcOpenAPIParser 加载它。解析器解析 $ref 指针、组合关键字和共享错误模型,因此代码生成器看到的是一个完全解引用的 API 模型。
uses sgcOpenAPI_Parser, sgcOpenAPI_Generator; var vParser: TsgcOpenAPIParser; vGen: TsgcOpenAPIGenerator; begin vParser := TsgcOpenAPIParser.Create(nil); try vParser.LoadFromFile('C:\specs\stripe\spec3.json'); Memo1.Lines.Add(Format('Loaded Stripe spec: %d paths, %d schemas', [vParser.Paths.Count, vParser.Schemas.Count])); vGen := TsgcOpenAPIGenerator.Create(nil); try vGen.Parser := vParser; vGen.OutputUnit := 'sgcOpenAPI_Stripe'; vGen.OutputFolder := 'C:\Generated\Stripe'; vGen.Generate; finally vGen.Free; end; finally vParser.Free; end; end;
生成器写入一个名为 sgcOpenAPI_Stripe.pas 的 Pascal 单元,规范中每个标签对应一个强类型类——TsgcStripe_Charges、TsgcStripe_Customers、TsgcStripe_Subscriptions、TsgcStripe_PaymentIntents 等——再加上一个拥有共享 HTTP 客户端和身份验证状态的顶层 TsgcStripe 门面类。
在门面类上设置一次 bearer 令牌,然后在类型化子对象上调用方法。请求体通过流畅的属性填充;响应作为强类型对象返回。
uses sgcOpenAPI_Stripe; var vStripe: TsgcStripe; vCharge: TsgcStripeCharge; begin vStripe := TsgcStripe.Create(nil); try vStripe.ApiKey := 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'; vCharge := vStripe.Charges.Create( Amount := 2000, // cents Currency := 'usd', Source := 'tok_visa', // test token Description := 'Order 1234'); try Memo1.Lines.Add('Charge id: ' + vCharge.Id); Memo1.Lines.Add('Status: ' + vCharge.Status); Memo1.Lines.Add('Paid: ' + BoolToStr(vCharge.Paid, True)); finally vCharge.Free; end; finally vStripe.Free; end; end;
分页由生成的游标对象处理。幂等性键通过每个修改性方法上的可选 IdempotencyKey 参数传递。
// Create a customer + attach a subscription var vCustomer: TsgcStripeCustomer; vSub: TsgcStripeSubscription; vItems: TsgcStripeSubscriptionItemList; begin vCustomer := vStripe.Customers.Create( Email := 'jane@example.com', PaymentMethod := 'pm_card_visa'); vItems := TsgcStripeSubscriptionItemList.Create; vItems.Add(Price := 'price_1JxYzZAbCdEfGhIj'); vSub := vStripe.Subscriptions.Create( Customer := vCustomer.Id, Items := vItems, IdempotencyKey := TGUID.NewGuid.ToString); // Page through invoices for this customer for vInvoice in vStripe.Invoices.List(Customer := vCustomer.Id) do Memo1.Lines.Add(Format('%s — %d %s', [vInvoice.Id, vInvoice.AmountDue, vInvoice.Currency])); end;
由于该单元由官方规范生成,每个有文档记录的端点和每个有文档记录的字段都可使用,无需手动绑定。
Charges、PaymentIntents、PaymentMethods、Refunds、Disputes、Sources、Tokens——完整的经典 Charges API 加上带强客户验证的现代 PaymentIntents 流程。
Customers、Invoices、InvoiceItems、Subscriptions、SubscriptionItems、Prices、Products、Coupons、PromotionCodes、TaxRates。
Accounts、AccountLinks、Transfers、Payouts、BalanceTransactions——从 Delphi 后台运营 Stripe Connect 平台所需的一切。
Identity VerificationSession 对象和 Terminal Reader/Location 对象一应俱全,包括 Stripe 用于 KYC 结果的多态 last_verification_report 模式。
包括带有判别式 data.object 负载的完整 Event 模式。使用与发送原始请求相同的类型解析传入的 webhook 主体。
Files、FileLinks、Reporting——由于规范使用 multipart/form-data 标记这些端点,因此会自动生成多部分上传辅助方法。
Stripe 每月发布新的 API 版本。将规范文件固定到您账户使用的版本(在每个请求上设置 Stripe-Version),并在升级时重新生成 Delphi 单元。生成器是确定性的——相同的规范,相同的输出。
多个 Stripe 模式(payment_method_details、source、balance_transaction.source)使用带判别符的 oneOf。生成的单元发出一个基类加上每种变体一个后代——根据 Type 属性进行向下转型。