Code Monkey home page Code Monkey logo

funstripe's Introduction

FunStripe

An F# library to connect to the Stripe API, including code generators to update the model and requests. Targets .NET Framework 4.7.2, .NET Standard 2.1 and .NET 6.

Latest updates

2023-11-13: version 0.10.2 makes some minor performance enhancements.

2023-10-13: version 0.10.1 makes some minor tweaks to normalise the folder structure.

2023-10-11: version 0.10.0 fixes an issue with form serialisation that meant that JsonField names were only applied to the top level elements. It also tidies the code up a little.

2023-08-29: version 0.9.3 updates the Stripe API from version 2022-11-15 to 2023-08-16, which contains some breaking changes. See the Stripe API changelog for details. It also updates the package dependencies for FSharp.Core and FSharp.Data to 6.0.7 and 6.2.0 respectively.

2022-11-22: version 0.9.2 updates the Stripe API from version 2022-08-01 to 2022-11-15, which contains some breaking changes. See the Stripe API changelog for details. As .NET 5 is out-of-support, this has been removed from the target frameworks.

2022-10-04: version 0.9.0 updates the Stripe API from version 2020-08-27 to 2022-08-01, which contains some breaking changes. See the Stripe API changelog for details.

2022-04-22: a lightweight version of this library without the code generators is now available as FunStripeLite.

2021-07-18: version 0.8.0 updates the Stripe API from version 2020-03-02 to 2020-08-27, which contains some breaking changes. See the Stripe API changelog for details.

Installation

Get the latest version from Nuget

Usage

Open the StripeModel and StripeRequest modules.

Here's an example of how to create a new payment method:

let settings = RestApi.StripeApiSettings.New(apiKey = Config.StripeTestApiKey)

let getNewPaymentMethod () =
    asyncResult {
        return! 
            PaymentMethods.CreateOptions.New(
                card = Choice2Of2 (PaymentMethods.Create'CardTokenParams.New("tok_visa")),
                type' = PaymentMethods.Create'Type.Card
            )
            |> PaymentMethods.Create settings
    }

The result value is an AsyncResult<PaymentMethod,ErrorResponse>, giving you the requested object in case of success or a detailed error response if not.

The general format of API requests is <module>.<method> settings options.

To instantiate the settings you need to pass in your Stripe API key. Having local rather than global settings allows you to use different keys for different Stripe accounts if you need to.

If you don't specify the API key in the settings record, it will look for a default test API key to use, and to keep the keys out of source code, it uses UserSecrets. It is recommended to use UserSecrets during development and web-server configuration settings in production, but if your source code will not be made public you can simply specify the API key as a string, at least for testing purposes. Config.fs contains some notes to help you.

The options can be provided using record notation or if there are many uninitialised properties you can use the static New method to instantiate the record more effiently.

Code Generation

By cloning the source repository, as a developer you can use ModelBuilder.fs and RequestBuilder.fs to generate the code in StripeModel.fs and StripeRequest.fs respectively.

Using Visual Studio Code, you simply select all the text in each document and hit Alt + Enter to send the code to F# Interactive. This will overwrite the contents of the target modules.

The Stripe OpenAPI specification is stored locally as /res/spec3.sdk.json. See the references below for the link to the latest online version.

By replacing the local copy with the latest one from online, you can regenerate the source code and build it to get the latest changes.

You could also customise how the source code is represented by editing the builder code files.

References

Stripe Documentation

Developer Tools

API Reference

Official Stripe .NET Library

Stripe OpenAPI Specification

funstripe's People

Contributors

simontreanor avatar thorium avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

thorium edgarfgp

funstripe's Issues

Possible future improvements

  • Multiple Stripe versions within the same version of the FunStripe library?
  • Handle lists and paging better
  • Order the types to avoid recursion
  • Simplify names by using nested modules?
  • Ensure XML comments are visible from calling code

Code generation

Hey Simon, I saw this pass by on Twitter found it an interesting read.
I was curious how you are generating the F# code and it seems like some good old string concatenation.

If you are interested you could actually use Fantomas to generate the code instead.
You could create the matching F# Syntax tree and pass it to Fantomas to print the source code.

Example:
https://github.com/nojaf/presentations/blob/b6dde5e49ba850f312082bfc32ce9a3354e2a0ee/formatting-fsharp-code/sql-type-generation/build.fsx#L190-L195

You could use the online tool to easily discover what AST nodes you need.

Just wanted to let you know...

Missing endpoints?

The Cards endpoint is not present in StripeRequest, likely because it is not defined in the paths in the OpenAPI spec. The API documentation lists a number of methods available on this endpoint that are useful, e.g. find a card by ID, but this cannot currently be done with the FunStripe library. But so far no one has said they're missing this functionality.

Stripe.net comparison

Hi Simon! Thanks for this library ๐Ÿ™‚

It would be useful to have a comparison against Stripe.net, perhaps in the README. I'm working for a client who wants Stripe integration and, given Stripe.net is supported by the Stripe team, I'd like some justification if choosing this library over Stripe.net.

What was the reason that you created this library in the first place? Does it enable you to get access to API updates sooner? Write more idiomatic F#? Handle errors in a way that you prefer? Something else? Basically anything that points out why an F# dev might choose FunStripe over Stripe.net would be useful.

Thanks!

Inconsistent casing in expand field

Stripe has an optional "expand" field that takes a list of strings representing fields to expand, e.g. ["latest_charge"] on a payment intent retrieve request will expand the field from a simple payment-intent ID string to a full-blown payment intent object.

However, sometimes the "expand" field is a query-string parameter and sometimes it is a form parameter. In the former case it needs to be formatted as expand = ["latest_charge"] but in the latter case as expand = ["latestCharge"]. This is inconsistent behaviour and needs addressing but might break existing implementations.

Improvements to strong typing

Many string values in requests could be strongly typed if the type can be identified unambiguously.

Also, country codes and currency codes could be sourced from the relevant ISO lists.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.