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

Api update #32

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -22,7 +22,79 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
### INIT:
```
PactasItero.client_id = "my_billwerk_client_id"
PactasItero.client_secret = "my_billwerk_client_secret"
@billwerkClient = PactasItero.client
@billwerkClient.bearer_token = @billwerkClient.try(:token).try(:access_token)
```

### Examples:

#### 1) Create-Plan Example

```
plangroup_id = "16dc8c7gba5c2202143de8b5"
plan_options={}
plan_options[:Name] = {"_c" => "Package XXL" }
plan_options[:PlanDescription] = {"_c"=>"This is the Plan-Description for XXL"}
plan_options[:PlanGroupId] = plangroup_id
plan_options[:SetupDescription] = {"_c"=>"one-time setup-fee"}
plan_options[:TrialEndNotificationPeriod] = {'Unit' => "Day", 'Quantity' => 3}
plan_options[:TrialEndPolicy] = "RequestPayment"
plan_options[:TrialPeriod] = {:Unit => "Day", :Quantity => 7}
plan_options[:IsQuantityBased] = false
plan_options[:Hidden] = false
plan_options[:IsDeletable] = false
@[email protected]_plan(plangroup_id,plan_options)
```

#### 2) Create-Order Example

##### Submitted via Order-Form (Example Data)

```
Parameters: {"Cart"=>{"PlanVariantId"=>"19dc8c7gba5c2202143de1s9"},
"Customer"=>{"CompanyName"=>"Mustermann Ltd.", "EmailAddress"=>"[email protected]", "FirstName"=>"Klaus", "LastName"=>"Mustermann", "VatId"=>"DE63244715",
"Address"=>{"Street"=>"Karl-Str.", "HouseNumber"=>"55", "PostalCode"=>"10421", "City"=>"Berlin", "Country"=>"DE"}},
"Bearer"=>{"holder"=>"Mustermann Ltd", "iban"=>"DE883299699663", "bic"=>"HAC556AXXX"}}
```

##### Usage in Order-Controller

```
orderOptions = {}
orderOptions[:ContractCustomFields] = {}
orderOptions[:AdditionalData] = {}
orderOptions[:AdditionalData][:Contract] = {}
orderOptions[:AdditionalData][:Contract][:CustomFields] = {}
orderOptions[:Cart] = params[:Cart].to_h
orderOptions[:Cart][:Quantity] = 1
orderOptions[:Cart][:EnableTrial] = true
orderOptions[:Cart][:ComponentSubscriptions] = []
orderOptions[:Cart][:MeteredUsages] = []
orderOptions[:Cart][:DiscountSubscriptions] = []
orderOptions[:Cart][:RatedItems] = []
orderOptions[:Customer] = params[:Customer].to_h
orderOptions[:Customer][:ExternalCustomerId] = "#{current_customer.id}"
orderOptions[:Customer][:Language] = 'de-DE';
orderOptions[:Customer][:Locale] = 'de-DE';
orderOptions[:Customer][:DebitorAccount] = "#{current_customer.debit_id}"
orderOptions[:Customer][:Hidden] = false
orderOptions[:StartDate] = Time.now.to_formatted_s(:iso8601)
@[email protected]_order(orderOptions)
```

##### On Success Commit-Order via Order-Controller

```
commitOptions = {}
commitOptions[:PaymentMethod] = "Debit:FakeProvider"
commitOptions[:Bearer] = params[:Bearer].to_h
commitOptions[:Bearer][:mandatereference] = "Z-#{current_customer.debit_id}-#{current_customer.id}"
@commit = @billwerkClient.commit_order(@order.id, commitOptions)
```

## Contributing

2 changes: 2 additions & 0 deletions lib/pactas_itero/api.rb
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
require "pactas_itero/api/plans"
require "pactas_itero/api/coupons"
require "pactas_itero/api/discounts"
require "pactas_itero/api/components"

module PactasItero
module Api
@@ -22,5 +23,6 @@ module Api
include PactasItero::Api::Plans
include PactasItero::Api::Coupons
include PactasItero::Api::Discounts
include PactasItero::Api::Components
end
end
5 changes: 5 additions & 0 deletions lib/pactas_itero/api/components.rb
Original file line number Diff line number Diff line change
@@ -10,6 +10,11 @@ def component(component_id, options = {})
options = options.camelize_keys
get "api/v1/components/#{component_id}", options
end

def create_component(plangroup_id, options = {})
options = options.camelize_keys
post "api/v1/planGroups/#{plangroup_id}/components", options
end

def update_component(component_id, options = {})
options = options.camelize_keys
5 changes: 5 additions & 0 deletions lib/pactas_itero/api/contracts.rb
Original file line number Diff line number Diff line change
@@ -35,6 +35,11 @@ def terminate_contract(contract_id, options = {})
options = options.camelize_keys
post "api/v1/contracts/#{contract_id}/end", options
end

def annulate_contract(contract_id, options = {})
options = options.camelize_keys
post "api/v1/contracts/#{contract_id}/annulate", options
end

def ledger_entries_for_contract(contract_id, options = {})
options = options.camelize_keys
11 changes: 8 additions & 3 deletions lib/pactas_itero/api/coupons.rb
Original file line number Diff line number Diff line change
@@ -10,12 +10,17 @@ def coupon(coupon_id, options = {})
options = options.camelize_keys
get "api/v1/coupons/#{coupon_id}", options
end

def coupon(coupon_id, options = {})
def create_coupon(plangroup_id, options = {})
options = options.camelize_keys
get "api/v1/coupons/#{coupon_id}", options
post "api/v1/planGroups/#{plangroup_id}/coupons", options
end

def update_coupon(coupon_id, options = {})
options = options.camelize_keys
patch "api/v1/coupons/#{coupon_id}", options
end

def coupon_by_code(coupon_code, options = {})
options = options.camelize_keys
get "api/v1/coupons/?couponCode=#{coupon_code}", options
10 changes: 10 additions & 0 deletions lib/pactas_itero/api/discounts.rb
Original file line number Diff line number Diff line change
@@ -10,6 +10,16 @@ def discount(discount_id, options = {})
options = options.camelize_keys
get "api/v1/Discounts/#{discount_id}", options
end

def create_discount(plangroup_id, options = {})
options = options.camelize_keys
post "api/v1/planGroups/#{plangroup_id}/discounts", options
end

def update_discount(discount_id, options = {})
options = options.camelize_keys
patch "api/v1/discount/#{discount_id}", options
end

end
end
8 changes: 4 additions & 4 deletions lib/pactas_itero/api/plans.rb
Original file line number Diff line number Diff line change
@@ -11,9 +11,9 @@ def plan(plan_id, options = {})
get "api/v1/plans/#{plan_id}", options
end

def create_plan(options = {})
def create_plan(plangroup_id, options = {})
options = options.camelize_keys
post "api/v1/plans", options
post "api/v1/planGroups/#{plangroup_id}/plans", options
end

def update_plan(plan_id, options = {})
@@ -31,9 +31,9 @@ def plan_variant(planvariant_id, options = {})
get "api/v1/planvariants/#{planvariant_id}", options
end

def create_plan_variant(options = {})
def create_plan_variant(plan_id, options = {})
options = options.camelize_keys
post "api/v1/planvariants", options
post "api/v1/plans/#{plan_id}/planvariants", options
end

def update_plan_variant(planvariant_id, options = {})
9 changes: 7 additions & 2 deletions lib/pactas_itero/ext/hash/camelize_keys.rb
Original file line number Diff line number Diff line change
@@ -24,9 +24,14 @@ def camelize_key(key)
end

# copied from ActiveSupport
# do not camelize exact "_c"
def camelize(term)
string = term.to_s
string = string.sub(/^[a-z\d]*/) { $&.capitalize }
string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
if (string.downcase == '_c')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to me like some really special behaviour. Could you please elaborate why this is necessary and give some examples?

string
else
string = string.sub(/^[a-z\d]*/) { $&.capitalize }
string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
end
end
end