# #175 Sell access to a Github Repository. Sell your code!

- URL: https://superails.com/posts/175-sell-access-to-a-github-repository-sell-your-code
- Published: 2024-06-24
- Duration: 22:10
- Access: free
- Topics: GitHub, stripe

Allow users to pay for your product via Stripe Payment Link.
You can place the payment link on any website.
When a user pays, a webhook will be sent to the Rails app.
The webhook will grant the user access to a private GitHub repository. 
Start selling your code now!

Based on this blogpost:
https://blog.corsego.com/github-api-collaborators
Episode source code:
https://github.com/corsego/175-github-api/commit/f739888032d4e2af64f56b2efa49eeb05ff79e80

Stripe gem:
https://github.com/stripe/stripe-ruby
Github API gem:
https://github.com/octokit/octokit.rb

## Transcript

Hey, as a developer, you might be thinking, why should I build a product and sell it if I can just sell access to my code? So in this episode, we are going to build an application where a user can make a payment via Stripe, and he'll automatically be granted access to a private GitHub repository. Here are a few examples of how this can work. For example, jumpstart, you can pay a subscription or pay once and after paying, you will get access, uh, to a private GitHub repository. Similar with Sidekick, there is a free version of the G and then there is a premium gem that you can pay a subscription or just a one time payment for. Similar with the, you can, uh, access the free, but there is also a premium option with the additional features. Also in Super Rails, if you are a paid subscriber, you will also get access to the Super rails.com source code. How does it work? Basically, you uh, click on subscribe, and after the payment is, uh, processed by Stripe, you will, uh, be granted access to a private GitHub repository. Here is a very interesting example from the next j js and file that actually inspired me for this video. Uh, this here is just a one pager. It is just like a landing page with a pay now button. If you click on the Pay Now button, you will be redirected to a Stripe uh, payment page. And after processing the payment, you will be crunched access to a private GitHub repository. That's it. And just with this small product, this, uh, guy seems to be doing quite well in terms of sales by just selling access to a private GitHub repository. So let's see how we can do this. And they're going to do it on an example of, uh, basically any, uh, private GitHub repository here. I have a private GitHub repository, and, uh, I will get, give access to any user that, uh, pays, uh, via Stripe. Why use Stripe? Why not use something like Gumroad? Uh, because Gumroad is an additional middleman that is just going to provide you this landing page, and they're going to take 10% commission on top of the Stripe Commission. So look, Gumroad is taken 10% flat, but if you go lower, what are the fees? Let's go to what are the fees? There is the Stripe fees. So, uh, you'll be paying not only for Stripe, but also for Gumroad. So in this, uh, episode, they're going to build the basic, uh, page. Uh, we're not going to do a lot of style, but it'll look more or less like a page with a pay now button. And, uh, when the user, uh, checks out and the payment is completed, then we are going to use the GitHub, API to give the user access to a private GitHub repository. So we are going to start with a fresh, uh, Stripe organization. I've just created the Sell Fast Organization, and, uh, I'm going to start with a Fresh Rails app. So I have just created this, uh, repository. I named it Cell Fast. And, uh, let's run the app. It's basically an, uh, empty rails application at the moment. Uh, let's, uh, create a basic landing page. I will say rails generate controller, uh, home index. So it'll be a controller named home with an index action. I will go to our roots and drag the route to home controller, index action rail server and have, have a, a basic landing page that we can tell however we want. Now, I'm going to go to Stripe and I'm going to create a product. So, uh, let's say we're going to sell access to our private repository for 1000, uh, dollars. Let's make it 999. So, uh, super ails access, uh, code access. It'll be a one off payment. It'll be in dollars. Let's make it 999 add product. So I have created a product, and now I'm going to go to payment links. This is actually the easiest way to add the external checkout, uh, in, uh, use use using Stripe. I'm going to select this product that I have just created. So I'm creating the checkout link for this product. And, uh, advanced options, this is important. I'm going to add a custom field, and, uh, I will name it GitHub, uh, username. Let's, uh, yeah, that's GitHub username, so that it looks nice here. So the user should input his email, his GitHub username, his card details. Let's create the link. And here is the URL. I will go with this link and put it on my, uh, home index page. I will add the equals link to by now. I will add the link and the target blank so that it opens in a new tab, not the same tab. Okay, let's go back to our application. I have this Buy now button. When I click the button, it opens the stripe checkout. I can input the email. Let's be like full, uh, m.com, whatever. Uh, a valid demo code. So 42 42 always works. Let's click on pay. It is going to process the payment, uh, in this test version. It'll be successful with this card number. If it go back to our Stripe customers, I see that the customer has just, uh, been created. I think this might be the one, uh, but we did not give access to the source code. Uh, we need to add and creation to the GitHub, uh, API. And, uh, first of all, before doing this, we need to let our application listen to the Stripe back webhooks. So it's going to work. So the user has a link to pay on Stripe. He pays, Stripe sends our application information about the payment, and based on this successful payment information and the username from GitHub, they're going to grant access to GitHub repository. So step number two is going to be, um, processing stripe web hooks. So for this, I'm going to install the gem stripe, gem stripe, uh, bundle. Now I'm going to find the gem stripe on GitHub, and we need to add an initializer. And, uh, in the initializer we're going to add the stripe, API key. So going to Initializers, inside config, I will create stripe rb. And here I will add the stripe secret key. So to get the secret key, I'm going to go to the a sentence, API keys, reveal the secret key and copay paste it here. Uh, now let's start the Rails console and see if we can access the stripe. A PII will say stripe price dot list. And we do see the price that we have just created. So $999 zero 0 cents. And we said that these are dollars, uh, because the currency is USD. Okay, uh, so we can access the stripe, uh, API in our console. Now let's listen to Stripe web hooks. To do this, I'm going to go back to the developers, open the web hooks, and, uh, yeah, add a local listener. So first of all, I need to install the Stripe. CLI, I have already installed it, so I'm going to go to Stripe login. Okay, I'm going to log into this organization. Looks good, allow access. Uh, next I'm going to listen to webhooks. Uh, I'm going to listen to webhooks on Port 3000. And I need to add the webhook route to listen to. So, uh, I'm going to go to our roots and say Post, uh, webhook, because here I'm listening to slash webhook, uh, tool webhook grade. That's called webhooks grade. So let's create the webhooks controller. I'm going to go to controllers, a webhooks controller, RB plus web hooks controller that inherits from, uh, application controller. And, and they're going to have a great action. And inside this great action, I'm actually going to go with a Sinatra code. Let's, uh, go with this example and paste it here, and let's clean it up so it folks in inside our rails controller. First of all, I will delete all these extra lines. We don't need to require a stripe 'cause we have already installed it as a gem. The stripe, API key is in present in the stripe initializer. The endpoint secret will be inside the dev crate. It's fine. We don't need to set the port, we don't need to set this post. Um, and we not don't need to return the status. So, uh, this should more or less, uh, work. We have a webhook endpoint, and let's try sending something to it. I'm going to start the rail survey of the window rail server. So here I've got the listener here, I've got the rail server, and in a third tab, I'm going to try to send a web hook. So I will go with this web hook. Let's trigger a web hook. So here we see we had a post request. The response is four to two, and it is some error inval authenticity token. So for this webhooks controller, I'm going to, uh, skip verification of authenticity token so that we can, uh, accept, uh, both requests, uh, from, uh, outside our application. Let's try once again. Okay, here it was a successful, uh, request, and here it was, uh, also a successful request. Let's try putting, uh, binding inside, uh, the payment intent succeeded. So let's say binding B, let's sell, send another request and we can, uh, explore the event object. We can explore the event data object that is the payment intent itself. So it works well. We can, uh, listen to Stripe webhooks. Next point is going to be to, uh, find the GitHub username that was submitted. Let's, uh, click fit and start the server on scan. And let's try, uh, doing a real, uh, checkout. So now, uh, again, I'm going to click on the buy now button. Let's input an email, a GitHub username, let's say yaro one to three card information. I will, uh, before clicking pay, I will, uh, uh, listen not to payment intent succeeded, but to the checkout, uh, session completed. So then checkout session completed, because when that checkout session is completed here in the payload, we'll have the GitHub username. We'll not have it inside the payment intended succeeded. So I'll put the band in here instead. Let's, uh, click on pay. And let's look at our console. So, uh, here we are inside checkout session completed, and we are in the binding. Let's get the event. Uh, let's try finding the GitHub username. And we actually have it here. Uh, we see there is a value Y 1, 2 3. So let's try exiting this, uh, value. We have, uh, event data object, uh, custom fields, and it can be an area of custom fields. So we're going to find the one that has the, uh, key GitHub username. So I'm gonna say find, uh, inside find. I will have, uh, uh, like object. We're going to run the one, but object key equals GitHub. Username, I'm doing not just, uh, custom fields dot first in case there can be multiple custom fields. So let's find that this one, if it is present, and it gives, uh, just this, uh, element outside of the array, they're going to say dot, uh, text and dot value. So this is how we can get this, uh, custom value of the GitHub username. So let's say GitHub username equals this big line. Okay? So, uh, we approaching the strive webhooks, and if there was a successful payment, we, uh, find the GitHub username that we want to give access to. So the next step is going to be to actually integrate the GitHub API and, uh, give the user access to our code base. Uh, let's, uh, install, uh, the Octo Kit GEM OK kit, uh, Ruby Gem. So this is a strange name of a gem, but it is actually the gem for, uh, working with the GitHub, API. So, uh, I'm going to add the Gem Okta kit to our gem file and run bundle. And now let's try, uh, playing with this gem a bit. So, uh, let's, uh, open the auto kit client, but we need an access token. So let's, uh, see how we can get a GitHub access token. Let's open GitHub. I will go to settings here, develop a settings, personal access token, find trained tokens. I will generate a new token. As you see here, I already have a token for super rails to, uh, grant access, uh, read and write access to other repositories. So now we are going to add a new token. Let's say sell fast. And description is, uh, give access to invoice app. Uh, only select repositories. Invoice app, and account permissions are going to be, uh, no, actually not account, but repository permissions are going to be administration. So read and write. So I'm going to letter this API token, uh, give access and take away access, uh, from GitHub repositories. Let's generate a token. Have, have the token. Let's scope it somewhere. Um, okay, now let's try interacting with the API, uh, in, uh, our console, the GitHub, API. So access token equals this token client, uh, client user dot login. Okay, so we get the information about the user looks good. Let's, uh, try get information about, uh, the repository, uh, collaborators of the repository that want to give access to, let's say client dot, uh, collaborators. And we're going to need a repository path. The repository path is going to be, um, OV slash invoice app. And here I see that, uh, two GitHub accounts have access to this repository. Let's, uh, have a look at it. Uh, once scan, I will go to settings, collaborators. And you see there is me and there is also Adrian who has access to this repository. Let's try, I programmatically give access to another user to this repository. So, um, I will say, uh, client dot add collaborator. The re Oh, no, let's try and scan client, add collaborator, uh, OV slash invoice app. And the username would be something like, let's say secret Prey. Let's give this user access. So I will click enter. It gives me true, let us refresh the page. And you see, this user has been invited to access the repository. Okay, so we managed to programmatically, uh, using the GitHub, API give access, uh, to a GitHub repository to a specific username. Let's try, uh, once again, I will delete this use access, and let's try integrating this into our Stripe checkout flow. So, uh, uh, here I'm going to start the GitHub client. I will say, uh, GitHub client equals, uh, auto Client New Access Token equals, uh, this Access token. And, uh, now I will say GitHub client dot add collaborator. Uh, the repository name is going to be, let's put it somewhere else, like repo path equals slash invoice app. So they're going to add the access to this repository path to this, uh, GitHub username that is provided in the checkout. And that's, uh, that's it. Let's see if it works. So, uh, once again, I'm going to start the rail server Rails s basically the Rails app is needed here only to process the web hooks. That's, uh, it, so, uh, let's go to buy now. Let's just double check. Only me and Adrian have access to this repository. I will input an email, uh, GitHub user name, uh, called information, uh, And I expect to receive, uh, a web hook from Stripe and, uh, make a request to GitHub to give access to the repository. So, uh, if I just refresh the page, you see Alexander has actually been added as a collaborator to the repository. And, uh, that's basically it. In our case, we need the Rails application only to process the web hooks. We only need the web hooks controller, uh, the route. And that's it. You can, uh, put a pin now, link anywhere on any website, and, uh, uh, just this way, let's, uh, just demonstrate it once again. One last time. Uh, let's find some, uh, GitHub user like Marco Roth, for example. Let's try giving him, uh, access. So I'm going to create a new checkout. I'm going to go to buy now. So I put in this GitHub username. I click Pay Stripe will send the web to our application that there has been a successful payment and that we should give, uh, access to the username. And if I go back, uh, to the repository, you see the user has been invited to the application. And just like this, you can, uh, uh, use Stripe, uh, payment links and GitHub API to sell your code. That's it.
