app_store_server_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 07df57effd89ae7c0c29815f4fbf1c0dc8e6aaefb9fd861b58a9f95fdde949b8
4
+ data.tar.gz: ca9299f9e20ab8de49433bde8973c940c06807640b87bb90f927627a01569960
5
+ SHA512:
6
+ metadata.gz: ba81d99883c98491d86a8d9f5d0b454de5abb8eaffc43b089c7fe8885665ab6bac6014e9a40bfaba420279892505d6cf6e3c87700f653b244d7303e08abb9f5c
7
+ data.tar.gz: 5a68dc8f2b3e1ef14b1da24457eb0682696750b393df0dc35de1b4b97089119d0fe48eb5a0e0e7e26438993679877fd1accf60431125bc79cf0267c750618aa5
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-05-12
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at csrutil@protonmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 tsaokoming
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # app-store-server-api
2
+ A Ruby client for the [App Store Server API](https://developer.apple.com/documentation/appstoreserverapi).
3
+
4
+ ## Features
5
+
6
+ - Transaction history, subscription status
7
+ - Notification test and history endpoints
8
+ - Manages authentication tokens for you
9
+ - Helpers to decode JWS items
10
+ - Performs certificate validation against Apple's CA.
11
+
12
+ ## Requirements
13
+
14
+ **Ruby 3.x**
15
+
16
+ ## Installation
17
+ ```bash
18
+ gem install app_store_server_api
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### Prerequisites
24
+ To get started, you must obtain the following:
25
+ - An [API key](https://developer.apple.com/documentation/appstoreserverapi/creating_api_keys_to_use_with_the_app_store_server_api)
26
+ - The ID of the key
27
+ - Your [issuer ID](https://developer.apple.com/documentation/appstoreserverapi/generating_tokens_for_api_requests)
28
+
29
+ ### Configure
30
+
31
+ **In your Rails application, create a client configure**
32
+
33
+ ```ruby
34
+ # my_app/config/app_store_server.yml
35
+ default: &default
36
+ private_key: |
37
+ -----BEGIN PRIVATE KEY-----
38
+ key_id: S4AZ693A4A
39
+ issuer_id: fd02853a-1290-4854-875e-918c86459b3e
40
+ bundle_id: com.myapp.app
41
+ environment: sandbox
42
+
43
+ development:
44
+ <<: *default
45
+
46
+ test:
47
+ <<: *default
48
+
49
+ production:
50
+ <<: *default
51
+ ```
52
+
53
+ **Load the configure**
54
+
55
+ ```ruby
56
+ # my_app/config/application.rb
57
+
58
+ config.app_store_server = config_for :app_store_server
59
+ ```
60
+
61
+ **Create a global client**
62
+
63
+ ```ruby
64
+ # my_app/config/initializers/app_store_server_api.rb
65
+
66
+ AppStoreServerApiClient = AppStoreServerApi::Client.new(**Rails.configuration.app_store_server)
67
+ ```
68
+
69
+ ### API
70
+
71
+ **Get Transaction History**
72
+
73
+ ```ruby
74
+ data = client.get_transaction_history(transaction_id,
75
+ params: {
76
+ sort: "DESCENDING"
77
+ })
78
+
79
+ transactions = AppStoreServerApi::Utils::Decoder.decode_transactions signed_transactions: data["signedTransactions"]
80
+ ```
81
+
82
+ **Get Transaction Info**
83
+
84
+ ```ruby
85
+ client.get_transaction_info transaction_id
86
+ ```
87
+
88
+ **Get All Subscription Statuses**
89
+ ```ruby
90
+ response = client.get_all_subscription_statuses transaction_id
91
+
92
+ signed = response["data"].first["lastTransactions"].first["signedTransactionInfo"]
93
+
94
+ transaction = AppStoreServerApi::Utils::Decoder.decode_transaction signed_transaction: signed
95
+ ```
96
+
97
+ **Request a Test Notification**
98
+
99
+ ```ruby
100
+ client.request_a_test_notification
101
+ ```
102
+
103
+
104
+ **Utils**
105
+
106
+ - AppStoreServerApi::Utils::Decoder.decode_transaction
107
+ - AppStoreServerApi::Utils::Decoder.decode_jws!
108
+
109
+
110
+ ## License
111
+
112
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
113
+
114
+ ## Code of Conduct
115
+
116
+ Everyone interacting in the AppStoreServerApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mnyify/app_store_server_api/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,144 @@
1
+ require "jwt"
2
+
3
+ require "net/http"
4
+ require "uri"
5
+ require "json"
6
+ require "openssl"
7
+
8
+ module AppStoreServerApi
9
+ class Client
10
+ def initialize(
11
+ private_key:, # p8 key
12
+ key_id:, # Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
13
+ issuer_id:, # Your issuer ID from the Keys page in App Store Connect
14
+ bundle_id:, # Your app’s bundle ID (Ex: “com.example.testbundleid”)
15
+ environment: :production
16
+ )
17
+
18
+ @private_key = private_key
19
+ @key_id = key_id
20
+ @issuer_id = issuer_id
21
+ @bundle_id = bundle_id
22
+ @base_url = app_store_base_url environment
23
+ end
24
+
25
+ def get_transaction_history(transaction_id, params: nil)
26
+ request_uri("#{@base_url}/inApps/v1/history/#{transaction_id}", params: params)
27
+ end
28
+
29
+ def get_transaction_info(transaction_id, params: nil)
30
+ request_uri("#{@base_url}/inApps/v1/transactions/#{transaction_id}", params: params)
31
+ end
32
+
33
+ # status
34
+ #
35
+ # 1, The auto-renewable subscription is active.
36
+ # 2, The auto-renewable subscription is expired.
37
+ # 3, The auto-renewable subscription is in a billing retry period.
38
+ # 4, The auto-renewable subscription is in a Billing Grace Period.
39
+ # 5, The auto-renewable subscription is revoked. The App Store refunded the transaction or revoked it from Family Sharing
40
+ def get_all_subscription_statuses(transaction_id, params: nil)
41
+ request_uri("#{@base_url}/inApps/v1/subscriptions/#{transaction_id}", params: params)
42
+ end
43
+
44
+ def request_a_test_notification
45
+ request_uri "#{@base_url}/inApps/v1/notifications/test", http_method: :post
46
+ end
47
+
48
+ private
49
+
50
+ def request_uri(uri, http_method: :get, headers: nil, params: nil, timeout: 5)
51
+ uri = URI(uri)
52
+ uri.query = URI.encode_www_form(params) if params && http_method == :get
53
+
54
+ # Create a new Net::HTTP object and set timeout
55
+ http = Net::HTTP.new(uri.host, 443)
56
+
57
+ http.open_timeout = timeout
58
+ http.read_timeout = timeout
59
+
60
+ http.use_ssl = true
61
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
62
+
63
+ # Make the HTTP request
64
+ begin
65
+ request = (http_method == :get) ? Net::HTTP::Get.new(uri.request_uri) : Net::HTTP::Post.new(uri.request_uri)
66
+
67
+ request.initialize_http_header headers || request_headers
68
+
69
+ if http_method == :post
70
+ request.body = params.to_json
71
+ end
72
+
73
+ response = http.request request
74
+
75
+ # Raise an error if the response is not successful
76
+ response.value
77
+
78
+ # Parse response body to JSON
79
+ JSON.parse response.body
80
+ rescue Net::OpenTimeout
81
+ raise "Timeout while opening the connection"
82
+ rescue Net::ReadTimeout
83
+ raise "Timeout while reading data"
84
+ rescue => e
85
+ raise "Error: #{e.message}"
86
+ end
87
+ end
88
+
89
+ def bearer_token
90
+ if @bearer_token
91
+ begin
92
+ JWT.decode @bearer_token, OpenSSL::PKey::EC.new(@private_key), true, {algorithm: "ES256"}
93
+ return @bearer_token
94
+ rescue JWT::ExpiredSignature
95
+ end
96
+ end
97
+
98
+ @bearer_token = generate_bearer_token(
99
+ key_id: @key_id,
100
+ issuer_id: @issuer_id,
101
+ private_key: @private_key,
102
+ bundle_id: @bundle_id
103
+ )
104
+ end
105
+
106
+ def request_headers
107
+ {
108
+ "Content-Type" => "application/json",
109
+ "Authorization" => "Bearer #{bearer_token}"
110
+ }
111
+ end
112
+
113
+ def generate_bearer_token(
114
+ key_id:,
115
+ issuer_id:,
116
+ bundle_id:,
117
+ private_key:,
118
+ audience: "appstoreconnect-v1", # appstoreconnect-v1,
119
+ issued_at: Time.now.to_i, # The time at which you issue the token, in UNIX time, in seconds (Ex: 1623085200)
120
+ expired_in: 3600,
121
+ alg: "ES256",
122
+ typ: "JWT"
123
+ )
124
+
125
+ payload = {
126
+ iss: issuer_id,
127
+ iat: issued_at,
128
+ exp: expired_in + issued_at,
129
+ aud: audience,
130
+ bid: bundle_id
131
+ }
132
+
133
+ JWT.encode payload, OpenSSL::PKey::EC.new(private_key), alg, {typ: typ, kid: key_id}
134
+ end
135
+
136
+ def app_store_base_url environment
137
+ if environment == :production
138
+ "https://api.storekit.itunes.apple.com"
139
+ else
140
+ "https://api.storekit-sandbox.itunes.apple.com"
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+ require "jwt"
5
+
6
+ module AppStoreServerApi
7
+ module Utils
8
+ module Decoder
9
+ module_function
10
+
11
+ def decode_jws! jws
12
+ payload, = JWT.decode(jws, nil, true, algorithm: "ES256") do |header|
13
+ certs = header["x5c"].map { |c| OpenSSL::X509::Certificate.new Base64.urlsafe_decode64(c) }
14
+ apple_root_cas.include? certs.last or raise JWT::DecodeError, "Missing root certificate"
15
+ certs.each_cons(2).all? { |a, b| a.verify(b.public_key) } or raise JWT::DecodeError, "Broken trust chain"
16
+ certs[0].public_key
17
+ end
18
+ payload
19
+ end
20
+
21
+ def decode_transaction(signed_transaction:)
22
+ decode_jws! signed_transaction
23
+ end
24
+
25
+ def decode_transactions(signed_transactions:)
26
+ signed_transactions.map do |signed_transaction|
27
+ decode_transaction signed_transaction: signed_transaction
28
+ end
29
+ end
30
+
31
+ def apple_root_cas
32
+ Dir.glob(File.join(__dir__, "certs", "*.cer")).map do |filename|
33
+ OpenSSL::X509::Certificate.new File.read(filename)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AppStoreServerApi
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "app_store_server_api/utils/decoder"
4
+ require_relative "app_store_server_api/client"
5
+ require_relative "app_store_server_api/version"
6
+
7
+ module AppStoreServerApi
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,4 @@
1
+ module AppStoreServerApi
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: app_store_server_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tsaokoming
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.8'
27
+ description: Manage your customers' App Store transactions from your server.
28
+ email:
29
+ - tsaokoming@mnyify.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".standard.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - lib/app_store_server_api.rb
42
+ - lib/app_store_server_api/client.rb
43
+ - lib/app_store_server_api/utils/certs/AppleRootCA-G3.cer
44
+ - lib/app_store_server_api/utils/certs/AppleWWDRCAG6.cer
45
+ - lib/app_store_server_api/utils/decoder.rb
46
+ - lib/app_store_server_api/version.rb
47
+ - sig/app_store_server_api.rbs
48
+ homepage: https://github.com/mnyify/app_store_server_api
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/mnyify/app_store_server_api
53
+ source_code_uri: https://github.com/mnyify/app_store_server_api
54
+ changelog_uri: https://github.com/mnyify/app_store_server_api/CHANGELOG.md
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 3.0.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.5.9
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: App Store Server API
74
+ test_files: []