I’m trying to add google play billing to my PWA website, which I’m wrapping into an android app with PWABuilder.
I’m tring to figure out how to test/debug locally.
This page on the docs says it can done like so:
- Ensure you are on Android 9 or greater with developer mode enabled (my phone has android 11)
- Install Chrome 101 or newer. (my phone has chrome v133.0.6946.121)
- Enable the following flags in Chrome by navigating to chrome://flags and searching for the flag by name: #enable-debug-for-store-billing (i have set this to enabled on my phone)
- Ensure that the site is hosted using a https protocol. Using http will cause the API to be undefined (this was hard but I created a self signed certificate using openssl (command below) and it gave me a key.pem and cert.pem files, which I added to my project root.
ssl key generation command: C:Program FilesOpenSSL-Win64bin>OpenSSL req -x509 -sha256 -newkey rsa:4096 -keyout u:key.pem -out u:cert.pem -days 365
I loaded my cert like this (both in my vite client and expressjs server):
const privateKey = fs.readFileSync('./key.pem', 'utf8');
const certificate = fs.readFileSync('./cert.pem', 'utf8');
const credentials = {key: privateKey, cert: certificate, passphrase: 'password'};
vite looks like this:
export default defineConfig({
plugins: [react(), svgr()],
server: {
port: 5173,
https: credentials
}
});
the server looks like this:
const privateKey = fs.readFileSync('./key.pem', 'utf8');
const certificate = fs.readFileSync('./cert.pem', 'utf8');
const credentials = {key: privateKey, cert: certificate, passphrase: 'password'};
import https from 'https';
const server = https.createServer(credentials, app);
server.listen(port, host, () => {
console.log(`nServer started at https://${host}:${port}...n`);
});
both the client and server seem to be able to connect via https from my phone, after I click the accept risks and continue button for each.
finally I attempt to connect to the digital goods service like so:
console.log('are we in a secure context? ', window.isSecureContext);
console.log('is the Digital Goods API supported? ', !!window.getDigitalGoodsService);
console.log('document.refferer: ', document.referrer);
if ('getDigitalGoodsService' in window) {
// Digital Goods API is supported!
try {
const service = await window.getDigitalGoodsService('https://play.google.com/billing');
console.log('billing supported');
} catch (error) {
console.error('Error connecting to Digital Goods API:', error);
return;
}
}
but it just keeps returning the following error:
Purchase.tsx:40 are we in a secure context? true
Purchase.tsx:41 is the Digital Goods API supported? true
Purchase.tsx:42 document.refferer: https://192.168.1.10:5173/shop
Purchase.tsx:52 Error connecting to Digital Goods API: OperationError: unsupported context
It seems to me like I met all the requirements described in the documentation for testing locally, yet I cannot access the API.
What am I doing wrong, or what can I try to help debug this?
