Skip to content

Commit

Permalink
Add acknowledge_product_purchase public method
Browse files Browse the repository at this point in the history
  • Loading branch information
gerard-morera committed Jun 22, 2020
1 parent dbc244c commit efa152b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/candy_check/play_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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
48 changes: 48 additions & 0 deletions spec/play_store/acknowledger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "spec_helper"

describe CandyCheck::PlayStore::Acknowledger do
let(:json_key_file) { File.expand_path("../fixtures/play_store/random_dummy_key.json", __dir__) }
subject { CandyCheck::PlayStore::Acknowledger.new(authorization: authorization) }

let(:package_name) { "fake_package_name" }
let(:product_id) { "fake_product_id" }
let(:token) { "fake_token" }

let(:authorization) { CandyCheck::PlayStore.authorization(json_key_file) }

describe "#acknowledge_product_purchase" do
it "when acknowlegement succeeds" do
VCR.use_cassette("play_store/product_acknowledgements/acknowledged") do
result = subject.acknowledge_product_purchase(package_name: package_name, product_id: product_id, token: token)

result.must_be_instance_of CandyCheck::PlayStore::ProductAcknowledgements::Response
result.acknowleged?.must_be_true
result.error.must_be_nil
end
end
it "when already acknowledged" do
error_body = "{\n \"error\": {\n \"code\": 400,\n \"message\": \"The purchase is not in a valid state to perform the desired operation.\",\n \"errors\": [\n {\n \"message\": \"The purchase is not in a valid state to perform the desired operation.\",\n \"domain\": \"androidpublisher\",\n \"reason\": \"invalidPurchaseState\",\n \"location\": \"token\",\n \"locationType\": \"parameter\"\n }\n ]\n }\n}\n"

VCR.use_cassette("play_store/product_acknowledgements/already_acknowledged") do
result = subject.acknowledge_product_purchase(package_name: package_name, product_id: product_id, token: token)

result.must_be_instance_of CandyCheck::PlayStore::ProductAcknowledgements::Response
result.acknowleged?.must_be_false
result.error[:body].must_equal(error_body)
result.error[:status_code].must_equal(400)
end
end
it "when it has been refunded" do
error_body = "{\n \"error\": {\n \"code\": 400,\n \"message\": \"The product purchase is not owned by the user.\",\n \"errors\": [\n {\n \"message\": \"The product purchase is not owned by the user.\",\n \"domain\": \"androidpublisher\",\n \"reason\": \"productNotOwnedByUser\"\n }\n ]\n }\n}\n"

VCR.use_cassette("play_store/product_acknowledgements/refunded") do
result = subject.acknowledge_product_purchase(package_name: package_name, product_id: product_id, token: token)

result.must_be_instance_of CandyCheck::PlayStore::ProductAcknowledgements::Response
result.acknowleged?.must_be_false
result.error[:body].must_equal(error_body)
result.error[:status_code].must_equal(400)
end
end
end
end

0 comments on commit efa152b

Please sign in to comment.