Add in-game purchases to the Android game

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Andredron
Regular
Posts: 176
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Add in-game purchases to the Android game

#1 Post by Andredron » Fri Apr 13, 2018 5:55 pm

My friend (Vladimir Chislov) told how to make purchases in renpy.
I decided to make an alternative to monetization through advertising (AdMob). Installed even easier AdMob.
On iOS is done similarly, just do not need to copy the application key. More I will not write, because there is no iPhone.

1) We purchase a Google Play Developer account, if it does not already exist.

2) In the Google Play Developer Console, on the "All Apps" tab, click "New Application".

3) Enter the name of the game and click "Create".

4) We are thrown to the window of filling in the data about the future game, but we are only interested in the "Services and API" tab on the left side of the screen.

5) Copy the application key from the "Licensing and Selling Content" section.

6) Open your options.rpy and uncomment the line define build.google_play_key = "" and insert the copied key between the quotes. CHECK THAT THERE IS NO SPACE IN THE COPYED KEY!

7) Now you can insert the code into script.rpy. Before starting, we register the purchase as follows:

Code: Select all

init:
    define unlock_lvl = iap.register(product = "unlock_lvl", identifier = "ru.chislov.mygame", google = "My Game",)
unlock_lvl is the identifier of the purchase, the name is coming up yourself
In the identifier, respectively, your package name
google is the name of the game, if you put None, then the name of the game will be the name of the package.

8) Then we do the buttons "Buy level" and "Restore purchase". I usually do it through imagebutton, you can at least through the menu or via imagemap - there is no difference:

Code: Select all

screen billing:
    imagebutton xalign 0.2 yalign 0.3:
        idle ("images/idle_billing.png")
        hover ("images/hover_billing.png")
        action Jump("label_billing")

screen restore:
    imagebutton xalign 0.2 yalign 0.4:
        idle ("images/idle_restore.png")
        hover ("images/hover_restore.png")
        action Jump("label_restore")
9) Next, we check whether the game was purchased earlier or not. In the place where it is necessary to open a new level, prescribe:

Code: Select all

if iap.has_purchased ("unlock_lvl"):
    jump continue_game
else:
    show screen billing
    show screen restore
    "To continue the game you need to buy a level or restore the purchase"
    $ renpy.pause (hard = True)
10) If the game is not purchased, then we transfer the user to label_billing or label_restore, if he bought the previous level:

Code: Select all

label label_billing:
    $ iap.purchase ("unlock_lvl")
    "If the payment is successful, the game will continue." Click to continue "
    if iap.has_purchased ("unlock_lvl"):
        "Thank you for purchasing the level! Continue the game"
        jump continue_game
    else:
        "Payment canceled"

label label_restore:
    $ iap.restore ("unlock_lvl")
    "If the purchase is restored, the game will continue." Click to continue. "
    if iap.has_purchased ("unlock_lvl"):
        "The purchase was restored! We continue to play"
        jump continue_game
    else:
        "The purchase was not reinstated."
11) Now re-configure the project. Open RenPy - Android - Customize. MANDATORY: On the question you like support in-app purchasing choose Google Play and allow access the internet choose Yes.

12) We collect the project and fill apk in the developer console. After downloading apk, go to the tab on the left "Content for sale." Specify: product type - subscription, product ID - unlock_lvl (without quotes) and click "Continue". Next, specify the description, price, etc. - I think you will understand.

All is ready! We can rejoice, now our game is monetized and will bring you revenue (I hope)
That's all, thank you for your attention.


Author gaida: I (https://vk.com/chislov_vladimir)
References: https://www.renpy.org/doc/html/iap.html
If I did not write correctly, or not exactly - please correct me
I know, I have terrible English
I'm writing a Renpy textbook (in Russian). I would be glad if someone translated into an English or German textbook https://yadi.sk/d/ZX_DonP63USRru Update 22.06.18

User avatar
ComputerArt.Club
Veteran
Posts: 270
Joined: Mon May 22, 2017 8:12 am
Completed: Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: Add in-game purchases to the Android game

#2 Post by ComputerArt.Club » Thu May 03, 2018 6:16 am

Thank you so much! I will definitely give this a go. This could be really really useful.

User avatar
gas
Veteran
Posts: 439
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Add in-game purchases to the Android game

#3 Post by gas » Thu May 03, 2018 7:30 am

Nice, thank you for sharing.
10 ? "RENPY"
20 GOTO 10

RUN

Saiffyros
Newbie
Posts: 15
Joined: Sun May 21, 2017 10:13 pm
Contact:

Re: Add in-game purchases to the Android game

#4 Post by Saiffyros » Tue May 29, 2018 9:13 pm

On the seventh step, what actually worked for me was:

Code: Select all

$ unlock_lvl1 = iap.register(product = "unlock_lvl1", identifier = "com.megaclash.unlock_lvl1", google = "unlock_lvl1")
where com.megaclash is the name of the game.

User avatar
ComputerArt.Club
Veteran
Posts: 270
Joined: Mon May 22, 2017 8:12 am
Completed: Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: Add in-game purchases to the Android game

#5 Post by ComputerArt.Club » Tue Jun 12, 2018 11:51 am

Thanks again for this. I'm still trying to get this working, though I have been busy with other things mostly.

I'm getting an error when I click on the purchase button (below is from the emulated version, but the error is the same on my phone.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 1684, in script
    $ iap.purchase ("unlock_lvl")
  File "game/script.rpy", line 1684, in <module>
    $ iap.purchase ("unlock_lvl")
  File "renpy/common/00iap.rpy", line 409, in purchase
    p = get_product(product)
  File "renpy/common/00iap.rpy", line 393, in get_product
    raise Exception("Product %r is has not been registered.")
Exception: Product %r is has not been registered.

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 1684, in script
    $ iap.purchase ("unlock_lvl")
  File "H:\Renpy\renpy-6.99.14.3-sdk\renpy\ast.py", line 862, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "H:\Renpy\renpy-6.99.14.3-sdk\renpy\python.py", line 1888, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 1684, in <module>
    $ iap.purchase ("unlock_lvl")
  File "renpy/common/00iap.rpy", line 409, in purchase
    p = get_product(product)
  File "renpy/common/00iap.rpy", line 393, in get_product
    raise Exception("Product %r is has not been registered.")
Exception: Product %r is has not been registered.

Windows-7-6.1.7601-SP1
Ren'Py 6.99.14.3.3347
bpmf 1.0
Tue Jun 12 23:36:57 2018
I have it down as an in app product rather than subscription. The status is supposedly active. Not sure what is going on there.

Also, not getting an email from google after sending out an internal test to my email address, this is my first time trying that function, too.

code for the product:

Code: Select all

init:
    define unlock_lvl = iap.register(product = "pro", identifier = "bpmf.catmat", google = "Game title",)
the label (as of yet not been adjusted much as this is still being tested):

Code: Select all

label label_billing:
    $ iap.purchase ("unlock_lvl")
    "If the payment is successful, the game will continue."
    if iap.has_purchased ("unlock_lvl"):
        "Thank you for purchasing the level! Continue the game"
        $ MainMenu(confirm=False)()
        #jump continue_game # commented as not relevant.
    else:
        "Payment canceled"
Probably something stupid, either that or I just need to wait for something to happen on Google's side regarding the registration of this in-app product.

Any ideas? Anyone else had a similar problem?

User avatar
Andredron
Regular
Posts: 176
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Add in-game purchases to the Android game

#6 Post by Andredron » Thu Jun 21, 2018 7:50 pm

tell me, your problem has been solved?
I know, I have terrible English
I'm writing a Renpy textbook (in Russian). I would be glad if someone translated into an English or German textbook https://yadi.sk/d/ZX_DonP63USRru Update 22.06.18

User avatar
ComputerArt.Club
Veteran
Posts: 270
Joined: Mon May 22, 2017 8:12 am
Completed: Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: Add in-game purchases to the Android game

#7 Post by ComputerArt.Club » Sun Jun 24, 2018 8:46 pm

Got it working in the end! Didn't want to post my solution till I am 100% everything is working. Today I'll be testing the final version and getting ready to think about promotion.

Here is the finished game: Chinese for Babies, Toddlers and Children Android App

PyTom helped me out. This is the way the init section looks now:

Code: Select all

init python:
     iap.register("pro", identifier="pro")
Here is the relevant part of the billing section (irrelevant stuff removed)

Code: Select all

label label_billing:
    $ iap.purchase ("pro")
    if iap.has_purchased ("pro"):
        #animation stuff goes here
        return
Thanks so much!

cookienomnom
Newbie
Posts: 14
Joined: Thu Jan 11, 2018 10:02 am
Contact:

Re: Add in-game purchases to the Android game

#8 Post by cookienomnom » Mon Sep 03, 2018 12:46 am

Thanks for posting. This is really helpful!
By the way, did any of you successfully implement in-game purchases on iOS?
Please do share or ping me if anyone has done it successfully on iOS. Thanks!!!

User avatar
ComputerArt.Club
Veteran
Posts: 270
Joined: Mon May 22, 2017 8:12 am
Completed: Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: Add in-game purchases to the Android game

#9 Post by ComputerArt.Club » Wed Sep 05, 2018 6:43 pm

I haven't tried iOS yet, although I am interested, it's not yet the right time for me (it would cost me a a lot of time, money and effort and so I'd like to refine my games more first). I am trying to get another game with in app purchases on Play though, experiencing errors as usual though.

Post Reply

Who is online

Users browsing this forum: No registered users