Skip to content

Commit

Permalink
Add server ACK for consumable (#50)
Browse files Browse the repository at this point in the history
* Add acknowledgement response

* Add product acknowledgement API call

* Allow tests contexts

* Add acknowledge_product_purchase public method

* Fix typo

* Add acknowledgement info to README

Co-authored-by: gerard-morera <[email protected]>
  • Loading branch information
gerard-morera and gerard-morera authored Jul 1, 2020
1 parent aa190df commit 1d7a7b7
Show file tree
Hide file tree
Showing 12 changed files with 645 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,34 @@ result = verifier.verify_subscription_purchase(

Please see documenation for [`CandyCheck::PlayStore::SubscriptionPurchases::SubscriptionPurchase`](http://www.rubydoc.info/github/jnbt/candy_check/master/CandyCheck/PlayStore/SubscriptionPurchases/SubscriptionPurchase) for further details.


##### Building an acknowledger

With the `authorization` object in place, we can build an acknowledger:

```ruby
acknowledger = CandyCheck::PlayStore::Acknowledger.new(authorization: authorization)
```

> **Note:** If you need to acknowledge against multiple Google Service Accounts, just instantiate a new acknowledger with another authorization object that got build with a different `.json` key file.
#### Acknowledging the purchase
Google Play purchases if not acknowledged are automatically refunded. The acknowledgement can be done client-side or server-side. If server-side validation is being used we recommend to proceed with the acknowledgement afterwards, since it is the only save way to ensure the users received what they have paid for.

```ruby
result = acknowledger.acknowledge_product_purchase(
package_name: "my-package-name",
subscription_id: "my-subscription-id",
token: "my-token"
)
# => ProductAcknowledgements::Response
```

The acknowledger response has 2 methods:

- `#acknowledged?` returns a boolean. It returns true only if it has been acknowledged at this moment (that's Google Play behaviour).
- `#error` returns a hash with `status_code` and `body` keys.

## CLI

This gem ships with an executable to verify in-app purchases directly from your terminal:
Expand Down
3 changes: 3 additions & 0 deletions lib/candy_check/play_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
require "candy_check/play_store/product_purchases/product_purchase"
require "candy_check/play_store/subscription_purchases/subscription_purchase"
require "candy_check/play_store/product_purchases/product_verification"
require "candy_check/play_store/product_acknowledgements/acknowledgement"
require "candy_check/play_store/product_acknowledgements/response"
require "candy_check/play_store/subscription_purchases/subscription_verification"
require "candy_check/play_store/verification_failure"
require "candy_check/play_store/verifier"
require "candy_check/play_store/acknowledger"

module CandyCheck
# Module to request and verify a AppStore receipt
Expand Down
19 changes: 19 additions & 0 deletions lib/candy_check/play_store/acknowledger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module CandyCheck
module PlayStore
class Acknowledger
def initialize(authorization:)
@authorization = authorization
end

def acknowledge_product_purchase(package_name:, product_id:, token:)
acknowledger = CandyCheck::PlayStore::ProductAcknowledgements::Acknowledgement.new(
package_name: package_name,
product_id: product_id,
token: token,
authorization: @authorization,
)
acknowledger.call!
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module CandyCheck
module PlayStore
module ProductAcknowledgements
# Verifies a purchase token against the PlayStore API

class Acknowledgement
# @return [String] the package_name which will be queried
attr_reader :package_name
# @return [String] the item id which will be queried
attr_reader :product_id
# @return [String] the token for authentication
attr_reader :token

# Initializes a new call to the API
# @param package_name [String]
# @param product_id [String]
# @param token [String]
def initialize(package_name:, product_id:, token:, authorization:)
@package_name = package_name
@product_id = product_id
@token = token
@authorization = authorization
end

def call!
acknowledge!

CandyCheck::PlayStore::ProductAcknowledgements::Response.new(
result: @response[:result], error_data: @response[:error_data])
end

private

def acknowledge!
service = CandyCheck::PlayStore::AndroidPublisherService.new

service.authorization = @authorization
service.acknowledge_purchase_product(package_name, product_id, token) do |result, error_data|
@response = { result: result, error_data: error_data }
end
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/candy_check/play_store/product_acknowledgements/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module CandyCheck
module PlayStore
module ProductAcknowledgements
class Response
def initialize(result:, error_data:)
@result = result
@error_data = error_data
end

def acknowledged?
!!result
end

def error
return unless error_data

{ status_code: error_data.status_code, body: error_data.body }
end

attr_reader :result, :error_data
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1d7a7b7

Please sign in to comment.