Last active
August 29, 2015 13:56
-
-
Save oreshinya/9304850 to your computer and use it in GitHub Desktop.
in app purchase、in app billingの検証用コードざっくりまとめ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'openssl' | |
| require 'base64' | |
| require 'net/http' | |
| module Verify | |
| module InAppPurchase | |
| PRODUCTION_VERIFICATION_END_POINT = "https://buy.itunes.apple.com/verifyReceipt" | |
| SANDBOX_VERIFICATION_END_POINT = "https://sandbox.itunes.apple.com/verifyReceipt" | |
| def verify!(data) | |
| response = request_verify(data) | |
| status = response["status"].to_i | |
| response = request_verify(data, is_production: false) if status == 21007 | |
| status = response["status"].to_i | |
| receipt = response["receipt"] | |
| is_valid = (receipt.present? && status == 0) | |
| raise "Verify Invalid" if !is_valid | |
| return receipt | |
| end | |
| private | |
| def request_verify(data, opts) | |
| is_production = true | |
| is_production = opts[:is_production] if opts && opts[:is_production] | |
| params = { | |
| receipt-data: data | |
| } | |
| uri = URI(is_production ? PRODUCTION_VERIFICATION_END_POINT : SANDBOX_VERIFICATION_END_POINT) | |
| http = Net::HTTP.new(uri.host, uri.port) | |
| http.use_ssl = true | |
| http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
| request = Net::HTTP::Post.new(uri.request_uri) | |
| request['Accept'] = "application/json" | |
| request['Content-Type'] = "application/json" | |
| request.body = params.to_json | |
| response = http.request(request) | |
| JSON.parse(response.body) | |
| end | |
| end | |
| module InAppBilling | |
| GOOGLE_LICENSE_KEY = "" | |
| def verify!(signed_data, signature) | |
| public_key = OpenSSL::PKey::RSA.new(Base64.decode64(GOOGLE_LICENSE_KEY)) | |
| is_valid = public_key.verify(OpenSSL::Digest::SHA1.new, Base64.decode64(signature), signed_data) | |
| raise "Verify Invalid" if !is_valid | |
| return JSON.parse(signed_data) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment