Navigation Menu

Skip to content

Commit

Permalink
Successful payment updating
Browse files Browse the repository at this point in the history
  • Loading branch information
joebuhlig committed Jun 2, 2017
1 parent 9380406 commit f633dd3
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 71 deletions.
61 changes: 39 additions & 22 deletions app/controllers/discourse_league/checkout_controller.rb
Expand Up @@ -14,35 +14,52 @@ def submit_payment
products = PluginStore.get("discourse_league", "levels")
product = products.select{|level| level[:id] == params[:level_id]}

if product[0][:recurring]
response = gateway.subscribe(current_user.id, product[0], params[:nonce])
else
response = gateway.purchase(current_user.id, product[0], params[:nonce])
end
if params[:update]
subscriptions = PluginStore.get("discourse_league", "subscriptions") || []
user_subscription = subscriptions.select{|subscription| subscription[:product_id].to_i == params[:level_id].to_i && subscription[:user_id] == current_user.id} || []

if response.success?
group = Group.find(product[0][:group].to_i)
if !group.users.include?(current_user)
group.add(current_user)
GroupActionLogger.new(current_user, group).log_add_user_to_group(current_user)
else
return render_json_error I18n.t('groups.errors.member_already_exist', username: current_user.username)
if user_subscription.empty?
render_json_error("Subscription cannot be found.")
end

if group.save
PostCreator.create(
Discourse.system_user,
target_usernames: current_user.username,
archetype: Archetype.private_message,
title: I18n.t('league.private_messages.sign_up_success.title', {productName: product[0][:name]}),
raw: product[0][:welcome_message]
)
response = gateway.update_payment(current_user.id, product[0], user_subscription[0][:subscription_id], params[:nonce])

if response[:response].success?
render json: success_json
else
return render_json_error(group)
render_json_error(response[:message])
end
else
render_json_error(response.message)
if product[0][:recurring]
response = gateway.subscribe(current_user.id, product[0], params[:nonce])
else
response = gateway.purchase(current_user.id, product[0], params[:nonce])
end

if response[:response].success?
group = Group.find(product[0][:group].to_i)
if !group.users.include?(current_user)
group.add(current_user)
GroupActionLogger.new(current_user, group).log_add_user_to_group(current_user)
else
return render_json_error I18n.t('groups.errors.member_already_exist', username: current_user.username)
end

if group.save
PostCreator.create(
Discourse.system_user,
target_usernames: current_user.username,
archetype: Archetype.private_message,
title: I18n.t('league.private_messages.sign_up_success.title', {productName: product[0][:name]}),
raw: product[0][:welcome_message]
)
render json: success_json
else
return render_json_error(group)
end
else
render_json_error(response.message)
end
end
end

Expand Down
10 changes: 10 additions & 0 deletions app/controllers/discourse_league/levels_controller.rb
Expand Up @@ -6,6 +6,16 @@ def show
if params[:id]
levels = PluginStore.get("discourse_league", "levels")
level = levels.select{|level| level[:id] == params[:id].to_i}

subscriptions = PluginStore.get("discourse_league", "subscriptions") || []
user_subscription = subscriptions.select{|subscription| subscription[:product_id].to_i == level[0][:id].to_i && subscription[:user_id] == current_user.id} || []

if user_subscription.empty?
level[0][:user_subscribed] = false
else
level[0][:user_subscribed] = true
level[0][:subscription_id] = user_subscription[0][:id]
end
end

if !level.empty? && level[0][:enabled]
Expand Down
Expand Up @@ -244,7 +244,7 @@ export default Ember.Component.extend({
_submitNonce(nonce){
var self = this;
this.set("braintreeLoading", true);
var result = Payment.submitNonce(this.get('leagueLevel')[0].id, nonce);
var result = Payment.submitNonce(this.get('leagueLevel')[0].id, nonce, this.get("update"));
result.then(response => {
console.log(response);
if (response.success){
Expand Down
Expand Up @@ -45,6 +45,12 @@ export default Ember.Controller.extend({
this.set('checkoutState', 'billing-payment');
this.set('showBilling', true);
this.set('showDescription', true);
if (this.get('memberSubscription') || (!this.get('memberExists'))){
this.set('showPayment', true);
}
else{
this.set('showPayment', false);
};
this.set('memberDetails', this.get('initMemberDetails'));
if (Discourse.SiteSettings.league_gateway == "Braintree"){
this.set('showBraintree', true);
Expand All @@ -66,6 +72,7 @@ export default Ember.Controller.extend({
submitBillingPayment: function() {
this.set("loading",true);
var data = this.get("memberDetails");
data.user_subscribed = this.get("memberSubscription");
var success = true;
return ajax("/league/checkout/billing-payment.json", {
data: JSON.stringify(data),
Expand Down Expand Up @@ -98,6 +105,7 @@ export default Ember.Controller.extend({
this.set("loading",true);
var data = this.get("memberDetails");
data.product_id = product[0].id;
data.user_subscribed = product[0].user_subscribed;
var success = true;
return ajax("/league/checkout/verify.json", {
data: JSON.stringify(data),
Expand Down
Expand Up @@ -23,7 +23,7 @@ export default Ember.Controller.extend({
},

updateBilling: function(subscription){
window.location.href = "/league/subscriptions/" + this.currentUser.id + "/" + subscription.id;
window.location.href = "/league/l/" + subscription.product.id;
}
}
})
4 changes: 2 additions & 2 deletions assets/javascripts/discourse/models/payment.js.es6
@@ -1,9 +1,9 @@
import { ajax } from 'discourse/lib/ajax';

export default {
submitNonce(level_id, nonce) {
submitNonce(level_id, nonce, update) {
return ajax('/league/checkout/submit-payment', {
data: JSON.stringify({"level_id": level_id, "nonce": nonce}),
data: JSON.stringify({"level_id": level_id, "nonce": nonce, "update": update}),
type: 'POST',
dataType: 'json',
contentType: 'application/json'
Expand Down
9 changes: 8 additions & 1 deletion assets/javascripts/discourse/routes/league-level-show.js.es6
Expand Up @@ -13,6 +13,13 @@ export default Discourse.Route.extend({
else{
var memberExists = false;
}
controller.setProperties({ model, memberExists: memberExists });

if (model[0].user_subscribed){
var memberSubscription = true;
}
else{
var memberSubscription = false;
}
controller.setProperties({ model, memberExists: memberExists, memberSubscription: memberSubscription });
}
});
75 changes: 42 additions & 33 deletions assets/javascripts/discourse/templates/league/level/show.hbs
@@ -1,41 +1,50 @@
{{#conditional-loading-spinner condition=loading}}
<div class="league checkout">
{{#if memberExists}}
<div>{{i18n 'league.member_exists'}}</div>
{{else}}
{{checkout-progress state=checkoutState}}
<div class="billing-section">
{{#if showDescription}}
<p>{{{model.0.description_cooked}}}</p>
{{/if}}
</div>
{{#if currentUser }}
<div class="user-table">
<div class="wrapper">
{{#if showBraintree}}
{{billing-braintree leagueLevel=model showBilling=showBilling showVerify=showVerify showCompleted=showCompleted checkoutState=checkoutState}}
{{else}}
{{#if showBilling}}
{{billing}}
{{/if}}

{{#if showVerify}}
{{verify}}
{{/if}}
{{/if}} {{!-- showBraintree --}}
{{#if memberSubscription}}

{{#if showCompleted}}
<div>{{{i18n 'league.checkout.completed_message'}}}</div>
{{else}}
<div>{{i18n 'league.member_exists'}}</div>
{{/if}} {{!-- memberSubscription --}}
{{/if}} {{!-- memberExists --}}
{{#if showPayment}}
{{checkout-progress state=checkoutState}}
<div class="billing-section">
{{#if showDescription}}
{{#if memberSubscription}}
<p>{{{i18n 'legaue.update_billing.description' }}}</p>
{{else}}
<p>{{{model.0.description_cooked}}}</p>
{{/if}} {{!-- memberSubscription --}}
{{/if}}
</div>
</div>
{{else}} {{!-- currentUser --}}
<div>{{i18n 'league.checkout.sign_up_message'}}</div>
<div class="account-buttons">
<span>{{d-button action="showCreateAccount" class="btn-primary sign-up-button" label="sign_up"}}</span>
<span>{{#d-button class="btn-primary" action="showLogin"}}{{fa-icon 'user'}} {{i18n 'log_in'}}{{/d-button}}</span>
</div>
{{/if}} {{!-- currentUser --}}
{{/if}} {{!-- memberExists --}}
{{#if currentUser }}
<div class="user-table">
<div class="wrapper">
{{#if showBraintree}}
{{billing-braintree leagueLevel=model showBilling=showBilling showVerify=showVerify showCompleted=showCompleted checkoutState=checkoutState update=memberSubscription}}
{{else}}
{{#if showBilling}}
{{billing update=memberSubscription}}
{{/if}}

{{#if showVerify}}
{{verify}}
{{/if}}
{{/if}} {{!-- showBraintree --}}

{{#if showCompleted}}
<div>{{{i18n 'league.checkout.completed_message'}}}</div>
{{/if}}
</div>
</div>
{{else}} {{!-- currentUser --}}
<div>{{i18n 'league.checkout.sign_up_message'}}</div>
<div class="account-buttons">
<span>{{d-button action="showCreateAccount" class="btn-primary sign-up-button" label="sign_up"}}</span>
<span>{{#d-button class="btn-primary" action="showLogin"}}{{fa-icon 'user'}} {{i18n 'log_in'}}{{/d-button}}</span>
</div>
{{/if}} {{!-- currentUser --}}
{{/if}} {{!-- showPayment --}}
</div>
{{/conditional-loading-spinner}}
@@ -1,21 +1,18 @@
{{#conditional-loading-spinner condition=loading}}
<div class="league checkout update-billing">
{{#if memberExists}}
<div>{{i18n 'league.member_exists'}}</div>
{{else}}
{{checkout-progress state=checkoutState}}
<div class="billing-section">
{{#if showDescription}}
<p>{{{model.0.description_cooked}}}</p>
<p>Update your payment method for Discourse League.</p>
{{/if}}
</div>
<div class="user-table">
<div class="wrapper">
{{#if showBraintree}}
{{billing-braintree leagueLevel=model showBilling=showBilling showVerify=showVerify showCompleted=showCompleted checkoutState=checkoutState}}
{{billing-braintree leagueLevel=model showBilling=showBilling showVerify=showVerify showCompleted=showCompleted checkoutState=checkoutState update=true}}
{{else}}
{{#if showBilling}}
{{billing}}
{{update-billing}}
{{/if}}

{{#if showVerify}}
Expand All @@ -28,6 +25,5 @@
{{/if}}
</div>
</div>
{{/if}} {{!-- memberExists --}}
</div>
{{/conditional-loading-spinner}}
21 changes: 19 additions & 2 deletions lib/discourse_league/billing/gateways.rb
Expand Up @@ -37,14 +37,30 @@ def store_token
user_id: @options[:user_id],
product_id: @options[:product_id],
token: @options[:token],
created_at: time
created_at: time,
updated_at: time
}

tokens.push(new_token)

PluginStore.set("discourse_league", "user_payment_tokens", tokens)
end

def update_token
tokens = PluginStore.get("discourse_league", "user_payment_tokens") || []

token = tokens.select{|token| token[:user_id] == @options[:user_id] && token[:product_id] == @options[:product_id]}

time = Time.now

unless token.empty?
token[0][:token] = @options[:token]
token[0][:updated_at] = time
end

PluginStore.set("discourse_league", "user_payment_tokens", tokens)
end

def store_subscription(subscription_id, subscription_end_date)
subscriptions = PluginStore.get("discourse_league", "subscriptions")
subscriptions = [] if subscriptions.nil?
Expand All @@ -62,7 +78,8 @@ def store_subscription(subscription_id, subscription_end_date)
subscription_id: subscription_id,
subscription_end_date: subscription_end_date,
active: true,
created_at: time
created_at: time,
updated_at: time
}

subscriptions.push(new_subscription)
Expand Down
28 changes: 26 additions & 2 deletions lib/discourse_league/gateways/braintree.rb
Expand Up @@ -48,7 +48,7 @@ def purchase(user_id, product, nonce, options = {})
image: response.transaction.paypal_details.image_url
}
league_gateway.store_transaction(response.transaction.id, response.transaction.amount, Time.now(), credit_card, paypal)
response
return {:response => response}
else
return {:success => false, :message => response}
end
Expand Down Expand Up @@ -78,7 +78,7 @@ def subscribe(user_id, product, nonce, options = {})
}
league_gateway.store_transaction(transaction.id, transaction.amount, transaction.created_at, credit_card)
end
subscription
return {:response => subscription}
else
return {:success => false, :message => subscription.errors.first.message}
end
Expand All @@ -92,6 +92,30 @@ def unsubscribe(subscription_id, options = {})
response = Braintree::Subscription.cancel(subscription_id)
end

def update_payment(user_id, product, subscription_id, nonce, options = {})
customer = self.customer(user_id)
payment = Braintree::PaymentMethod.create(
:customer_id => customer.id,
:payment_method_nonce => nonce
)
if payment.success?
subscription = Braintree::Subscription.update(
subscription_id,
:payment_method_token => payment.payment_method.token
)

if subscription.success?
league_gateway = DiscourseLeague::Billing::Gateways.new(:user_id => user_id, :product_id => product[:id], :token => subscription.subscription.payment_method_token)
league_gateway.update_token
return {:response => subscription}
else
return {:response => subscription, :message => subscription.errors.first.message}
end
else
return {:success => false, :message => payment.errors.first.message}
end
end

def customer(user_id)
begin
customer = Braintree::Customer.find(user_id)
Expand Down

0 comments on commit f633dd3

Please sign in to comment.