Glench / ExtPay

The JavaScript library for ExtensionPay.com — payments for your browser extensions, no server needed.
https://extensionpay.com
Other
524 stars 66 forks source link

onPaid.addListener is not being triggered #117

Closed Nodirbek3d closed 1 year ago

Nodirbek3d commented 1 year ago

I have following code in my ext_pay_content.js

const extpay = ExtPay('gpt-assistant');
extpay.onPaid.addListener(() => {
    console.log("onPaid listener called")
});

and following in manifest:

...
"content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "css": [
        "style.css"
      ],
      "js": [
        "./lib/jquery.js",
        "./lib/ExtPay.js",
        "./js/content.js"
      ],
      "run_at": "document_end"
    },
    {
      "matches": [
        "https://extensionpay.com/*"
      ],
      "js": [
        "./lib/ExtPay.js",
        "./js/limits.js",
        "./js/ext_pay_content.js"
      ],
      "run_at": "document_start"
    }
  ],
...

So, when in a popup, user clicks on Subscribe button, it opens up login pay, when user makes a payment, listener is not being triggered, I have ExtPay available on background script as well:

const extpay = ExtPay('gpt-assistant')
extpay.startBackground();
Glench commented 1 year ago

I haven't dug into it yet, but what immediately jumps out is using onPaid in the content script. Instead, you might try putting the onPaid listener in your background script.

Nodirbek3d commented 1 year ago

I have tried to put it everywhere, background script, popup script, content script, it is not getting triggered at all

Glench commented 1 year ago

Again, I haven't had time to play around (I will when I have time to sit down) but I just noticed you referred to "./js/ext_pay_content.js" in your manifest but "content_expay.js" in your example. Are you sure the files are named correctly?

Nodirbek3d commented 1 year ago

yeah, the same script, I fixed here in post, in my project there is no content_expay.js

Glench commented 1 year ago

I just tested with the onPaid listener in a background service worker and it worked fine. I would not recommend putting the onpaid listener in a content script that loads on the extensionpay site as it will likely not be ready by the time the payment signal happens.

Nodirbek3d commented 1 year ago

can you share sample code where it worked? I removed it from content script, and left only on background one, but it is not getting triggered.

Glench commented 1 year ago

Here you go: https://gist.github.com/Glench/373a47bc64ffc404665290ef1beaeebe

Make sure to change the extension id in that example. You'll see a console message in the service worker after a user has paid.

Nodirbek3d commented 1 year ago

ok, I found the case when it did not work, when a user cancels the subscription in the subscription window, and then activates it back, in that case it is not getting triggered.

Glench commented 1 year ago

I’m not sure what you mean. Can you take screenshots of what you’re doing?

Nodirbek3d commented 1 year ago

can we setup a zoom meeting: :)

Glench commented 1 year ago

No sorry I’m not available for video meetings.

The onPaid listener is not currently meant to listen for when a user cancels or uncancels their subscription. That isn’t a payment event. However that could be another event. Can you please explain your use case?

Nodirbek3d commented 1 year ago

my use case is, I have to set request limit that a user can send using extension, by default there is let's say 10 requests per day, but premium users can send 100 requests per day, so I need to know if user is premium or not, or user canceled subscription, but later activated it again, I need to set limit based on the case.

Glench commented 1 year ago

And why can’t you use extpay. getUser()?

Nodirbek3d commented 1 year ago

extpay.getUser() does not provide info if user reactivated their account. so that it could trigger setting limit.

Glench commented 1 year ago

What does "reactivated" mean? When a user lets cancels their subscription and the cancel_at datetime has passed?

I would say that in your code where you do the check for request limits you just check if extPay.getUser() is paid or not.

Nodirbek3d commented 1 year ago

you should consider that limit is per day, and there is an expiration that has to be checked.

Nodirbek3d commented 1 year ago

would be nice to have listener when user cancels, activates, etc. subscription

Glench commented 1 year ago

It still seems the same to me, but I don't know how your extension is structured and I don't provide extension-specific support.

Since this issue has been resolved, I'll close it, but I see now that what you're actually reporting is a feature request to call the onPaid listener after a user lets their subscription cancel and then resubscribes. Since this is a rare case, it's not high on my priority list, but it does seem like a good idea.