Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server ACK for consumable #50

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
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
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
@@ -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
@@ -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
@@ -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.