aporat / store-receipt-validator

PHP receipt validator for Apple iTunes, Google Play and Amazon App Store
Apache License 2.0
633 stars 153 forks source link

[ADD] fix error on acknowledgement of an already acknowledged purchase #110

Closed Orkin closed 4 years ago

Orkin commented 4 years ago

On acknowledgement of purchase already acknowledge the google play api send message not really explicit : The product purchase not is owned by the user.

After many researches I found this and try and it fix the issue for me and it's a small optimisation because we don't have to acknowledge a purchase when it's already done

https://stackoverflow.com/questions/57971231/unable-to-use-purchase-subscriptionacknowledge-to-acknowledge-a-subscription

Stafox commented 4 years ago

@Orkin thanks for PR. It would be nice to have strategy for this behavior. At first someone already store the acknowledgement value. At second, get-request took time and do additional request to Google API, which has limits.

Who knows what kind of error Google will return in the future, right? So it might be useful to be able handle such error by youself. So, better to add two strategies:

// Do acknowledge only in case if it have not done
const ACKNOWLEDGE_STRATEGY_IMPLICIT ='strategy_implicit';
// Try to do acknowledge directly (exception will be returned in case when acknowledge already was done)
const ACKNOWLEDGE_STRATEGY_EXPLICIT ='strategy_explicit';

And public method to setStrategy(string $strategy);

ACKNOWLEDGE_STRATEGY_EXPLICIT should be default.

Orkin commented 4 years ago

Hello, yes of course it's a good idea. I'll try to find some time to do this. I'd just need a quick fix due to production error and share my solution 😉

Orkin commented 4 years ago

There is a BC break because without this it was not possible to catch AlreadyAcknowledgeException

Orkin commented 4 years ago

Hello @Stafox any new on validating this PR ?

Stafox commented 4 years ago

Hello @Stafox any new on validating this PR ?

Hi. Could you make the fixes from our latest discussion? After that it can be merged. Thanks.

Orkin commented 4 years ago

@Stafox I have resolved conflict. I can do modification but it's become impossible to test the case when acknowledge failed because we don't know which exception is thrown. It's ok for you ?

IE => I have to remove some tests

Orkin commented 4 years ago

And of course it's become impossible to catch on our code if it's a generic error on payment or just an acknowledge error 😢

Stafox commented 4 years ago

but it's become impossible to test the case when acknowledge failed because we don't know which exception is thrown. It's ok for you ?

Yes, it will work like it worked before. The main thing that it become possible to catch Exception to handle that (it provides transparency).

Orkin commented 4 years ago

@Stafox done ;)

Stafox commented 4 years ago

Reviewed. Looks fine.

Thanks

ipehimanshu commented 2 years ago

How to acknowledge for google play? any example?

Thanks

Stafox commented 2 years ago

How to acknowledge for google play? any example?

Thanks

$strategy = Acknowledger::ACKNOWLEDGE_STRATEGY_EXPLICIT;

$acknowledger = new Acknowledger(
        $googleServiceAndroidPublisher,
        $packageName,
        $productId,
        $purchaseToken,
        $strategy
);

$purchaseType = Acknowledger::SUBSCRIPTION; // or Acknowledger::PRODUCT
$developerPayload = 'dev_payload_here';

$acknowledger->acknowledge($purchaseType, $developerPayload);

Also purchase can be acknowledged on client in that case no needs to do that on backend.