Foreword
- I'm using Zeit‘s now - it might somewhat different with other providers.
- I'm also using Hugo as my Static Site Generator (SSG), again, there might be some differences using other SSG's. Though, not much, I guess.
- This will also be done using a monorepo-structure, but it'll probably be easier to implement using multiple repos. Only thing I can think of that might cause an issue, is our good ol’ friend - CORS. They can also be quite the pain in the *beep*.
Doing this post we'll be using the Strike API, a bitcoin payment service. Just FYI, this is the first time ever I'll be implementing a micropayment to a site. Thus, I've been doing some research, and Strike seemed to be the best - and prettiest - of said service out there in the wild, wild web.
Anyhow, forward!
API
For this implementation I'll be using Go as I'm a gopher by heart.
Thus, let's start by adding a new go module - this will not be needed for this simple program, but always a good way to begin - at least for me. So, follow along, and we shall sing! - Might be a lie.
|
|
Pay attention to the highlighted line, that the magic.
How does this so-called program look, then? Well, I cannot tell you now, but keep on reading, and it shall be reviewed!
First of, let's start by adding the skeleton.
|
|
To be continued…
Config
Now, how does one implement such awesomeness, using a monorepo to a serverless platform?
Luckily, we have now.json to the rescue! Or rather - To the annoyance!
Joking, of course, now.json is my friend - alas they can be annoying at times. For instance:
|
|
If using this - recommended - structure of the now.json, one would think that everything should work as before - it should be said, that we're still using hugo as the SSG - Alas, this is not the case.
As you can see, if you're following along with me. You'll get a rather blunt site. With no styling applied - this is not what we wanted. We just wanted something that works.
Anyhow, the fix is simple, but weird. By adding the highlighted line below, it'll fetch that styling as easy as 1..3 ๐คทโโ
|
|
Now that we got the styling in place. Let's work a tad more on our API.
API - continued
Now, back to the awesome Gopher code! Psych! First navigate to the api documentation of Strike. This time, scroll down until you find Create a charge.
Select the language of choice, in this case, the golang-tab. You can simply copy'n paste the code. It's almost fully working. Only thing missing is our API-key.
|
|
Well, if you check the code. You'll see that that I lied just a tad. I've change a couple of lines. More specifically:
- Line
11has been removed. Because, why the empty line? - On line
12I've removed the.dev-part of the uri. I Know this is working, though, keep it for testing, if you want. - Changed the
"sk_your_api_key"toos.Getenv("STRIKE_KEY"), as we'll be getting this from the environment. - Line
15has been modified, whereas I've removed some of the\(back-slash), and added (back-ticks) around the the string withinstrings.NewReader(...).
|
|
Now, all there's left, is to add our secret api-key.
Secrets
First, let's add the key to our own environment.
|
|
Next up, add the key to the now-environment.
|
|
You'd simply, instead of typing/pasting the key again, infer it from our own environment. I'm not lazy enough, so I've pasted it again.
Lastly, expose the secret to the now-environment.
|
|
JavaScript
So, how do we call this magnificent function, that we've just created.
Good question, my dear Watson.
Let's add a new script to our site.
|
|
I'll take a random assumption, that you already can read JavaScript, thus, no explaination is needed ๐ค
- Thanks,
Mads Cordes