1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00

ongoing work on sca stripe

This commit is contained in:
Nicolas Florentin 2019-09-09 17:37:54 +02:00
parent 510533e080
commit e94cf46fa4
8 changed files with 101 additions and 205 deletions

View File

@ -482,16 +482,22 @@ Application.Directives.directive('cart', [ '$rootScope', '$uibModal', 'dialogs',
},
coupon () {
return $scope.coupon.applied;
},
cartItems () {
return mkRequestParams(reservation, $scope.coupon.applied);
}
},
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'cgv', 'Auth', 'Reservation', 'wallet', 'helpers', '$filter', 'coupon',
function ($scope, $uibModalInstance, $state, reservation, price, cgv, Auth, Reservation, wallet, helpers, $filter, coupon) {
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'cgv', 'Auth', 'Reservation', 'wallet', 'helpers', '$filter', 'coupon', 'cartItems',
function ($scope, $uibModalInstance, $state, reservation, price, cgv, Auth, Reservation, wallet, helpers, $filter, coupon, cartItems) {
// user wallet amount
$scope.walletAmount = wallet.amount;
// Price
$scope.amount = helpers.getAmountToPay(price.price, wallet.amount);
// cartItems
$scope.cartItems = cartItems;
// CGV
$scope.cgv = cgv.custom_asset;

View File

@ -4,6 +4,9 @@ Application.Directives.directive('stripeForm', ['Payment', 'growl',
function (Payment, growl) {
return ({
restrict: 'A',
scope: {
cartItems: '='
},
link: function($scope, element, attributes) {
const stripe = Stripe('<%= Rails.application.secrets.stripe_publishable_key %>');
const elements = stripe.elements();
@ -50,7 +53,7 @@ Application.Directives.directive('stripeForm', ['Payment', 'growl',
growl.error(error);
} else {
// Send paymentMethod.id to your server (see Step 2)
Payment.confirm({ payment_method_id: paymentMethod.id }, function (response) {
Payment.confirm({ payment_method_id: paymentMethod.id, cart_items: $scope.cartItems }, function (response) {
// Handle server response (see Step 3)
handleServerResponse(response);
});
@ -71,7 +74,7 @@ Application.Directives.directive('stripeForm', ['Payment', 'growl',
} else {
// The card action has been handled
// The PaymentIntent can be confirmed again on the server
Payment.confirm({ payment_intent_id: result.paymentIntent.id }, function(confirmResult) {
Payment.confirm({ payment_intent_id: result.paymentIntent.id, cart_items: $scope.cartItems }, function(confirmResult) {
paymentSuccess(confirmResult);
}).then(handleServerResponse);
}

View File

@ -7,7 +7,7 @@
<uib-alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</uib-alert>
<div class="panel panel-default bg-light m-n">
<form name="stripeForm" stripe:form="payment" class="form-horizontal">
<form name="stripeForm" stripe:form="payment" cart-items="cartItems" class="form-horizontal">
<div class="panel-body">
<h3 class="m-t-xs" ng-if="walletAmount" ng-bind-html="'you_have_amount_in_wallet' | translate:{ amount: numberFilter(walletAmount, 2), currency: currencySymbol }"></h3>

View File

@ -5,28 +5,60 @@ class API::PaymentsController < API::ApiController
before_action :authenticate_user!
def confirm_payment
data = JSON.parse(request.body.read.to_s)
begin
if data['payment_method_id']
if params[:payment_method_id].present?
# Create the PaymentIntent
# TODO the client has to provide the reservation details. Then, we use Price.compute - user.walletAmount to get the amount
# currency is set in Rails.secrets
reservable = cart_items_params[:reservable_type].constantize.find(cart_items_params[:reservable_id])
price_details = Price.compute(false,
current_user,
reservable,
cart_items_params[:slots_attributes] || [],
cart_items_params[:plan_id],
cart_items_params[:nb_reserve_places],
cart_items_params[:tickets_attributes],
coupon_params[:coupon_code])
intent = Stripe::PaymentIntent.create(
payment_method: data['payment_method_id'],
amount: 1099,
currency: 'usd',
payment_method: params[:payment_method_id],
amount: price_details[:total],
currency: 'eur',
confirmation_method: 'manual',
confirm: true
)
elsif data['payment_intent_id']
intent = Stripe::PaymentIntent.confirm(data['payment_intent_id'])
elsif params[:payment_intent_id].present?
intent = Stripe::PaymentIntent.confirm(params[:payment_intent_id])
end
rescue Stripe::CardError => e
# Display error on client
render status: 200, json: { error: e.message }
end
if intent.status == 'succeeded'
begin
user_id = params[:cart_items][:reservation][:user_id]
@reservation = Reservation.new(reservation_params)
is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id)
.pay_and_save(@reservation, :stripe, coupon_params[:coupon_code])
if is_reserve
SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible
render('api/reservations/show', status: :created, location: @reservation) and return
else
render(json: @reservation.errors, status: :unprocessable_entity) and return
end
rescue InvalidCouponError
render(json: { coupon_code: 'wrong coupon code or expired' }, status: :unprocessable_entity) and return
end
end
render generate_payment_response(intent)
end
@ -51,4 +83,20 @@ class API::PaymentsController < API::ApiController
{ status: 500, json: { error: 'Invalid PaymentIntent status' } }
end
end
def reservation_params
params[:cart_items].require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :nb_reserve_places,
tickets_attributes: %i[event_price_category_id booked],
slots_attributes: %i[id start_at end_at availability_id offered])
end
def cart_items_params
params[:cart_items].require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :user_id, :nb_reserve_places,
tickets_attributes: %i[event_price_category_id booked],
slots_attributes: %i[id start_at end_at availability_id offered])
end
def coupon_params
params.require(:cart_items).permit(:coupon_code)
end
end

View File

@ -22,12 +22,13 @@ class API::ReservationsController < API::ApiController
def show; end
def create
method = current_user.admin? ? :local : :stripe
user_id = current_user.admin? ? params[:reservation][:user_id] : current_user.id
authorize Reservation
user_id = params[:reservation][:user_id]
@reservation = Reservation.new(reservation_params)
is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id)
.pay_and_save(@reservation, method, coupon_params[:coupon_code])
.pay_and_save(@reservation, :local, coupon_params[:coupon_code])
if is_reserve
SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible

View File

@ -32,10 +32,6 @@ class Reservation < ActiveRecord::Base
# @param coupon_code {String} pass a valid code to appy a coupon
##
def generate_invoice_items(on_site = false, coupon_code = nil)
# returning array
invoice_items = []
# prepare the plan
plan = if user.subscribed_plan
user.subscribed_plan
@ -70,18 +66,8 @@ class Reservation < ActiveRecord::Base
ii_amount = 0 if slot.offered && on_site # if it's a local payment and slot is offered free
unless on_site # if it's local payment then do not create Stripe::InvoiceItem
ii = Stripe::InvoiceItem.create(
customer: user.stp_customer_id,
amount: ii_amount,
currency: Rails.application.secrets.stripe_currency,
description: description
)
invoice_items << ii
end
invoice.invoice_items.push InvoiceItem.new(
amount: ii_amount,
stp_invoice_item_id: (ii.id if ii),
description: description
)
end
@ -100,18 +86,8 @@ class Reservation < ActiveRecord::Base
" #{I18n.l slot.start_at, format: :long} - #{I18n.l slot.end_at, format: :hour_minute}"
ii_amount = base_amount
ii_amount = 0 if slot.offered && on_site
unless on_site
ii = Stripe::InvoiceItem.create(
customer: user.stp_customer_id,
amount: ii_amount,
currency: Rails.application.secrets.stripe_currency,
description: description
)
invoice_items << ii
end
invoice.invoice_items.push InvoiceItem.new(
amount: ii_amount,
stp_invoice_item_id: (ii.id if ii),
description: description
)
end
@ -131,18 +107,8 @@ class Reservation < ActiveRecord::Base
end
ii_amount = amount
ii_amount = 0 if slot.offered && on_site
unless on_site
ii = Stripe::InvoiceItem.create(
customer: user.stp_customer_id,
amount: ii_amount,
currency: Rails.application.secrets.stripe_currency,
description: description
)
invoice_items << ii
end
invoice.invoice_items.push InvoiceItem.new(
amount: ii_amount,
stp_invoice_item_id: (ii.id if ii),
description: description
)
end
@ -163,18 +129,8 @@ class Reservation < ActiveRecord::Base
ii_amount = 0 if slot.offered && on_site # if it's a local payment and slot is offered free
unless on_site # if it's local payment then do not create Stripe::InvoiceItem
ii = Stripe::InvoiceItem.create(
customer: user.stp_customer_id,
amount: ii_amount,
currency: Rails.application.secrets.stripe_currency,
description: description
)
invoice_items << ii
end
invoice.invoice_items.push InvoiceItem.new(
amount: ii_amount,
stp_invoice_item_id: (ii.id if ii),
description: description
)
end
@ -199,36 +155,25 @@ class Reservation < ActiveRecord::Base
else
raise InvalidCouponError
end
unless on_site
invoice_items << Stripe::InvoiceItem.create(
customer: user.stp_customer_id,
amount: -discount,
currency: Rails.application.secrets.stripe_currency,
description: "coupon #{@coupon.code}"
)
end
end
@wallet_amount_debit = wallet_amount_debit
if @wallet_amount_debit != 0 && !on_site
invoice_items << Stripe::InvoiceItem.create(
customer: user.stp_customer_id,
amount: -@wallet_amount_debit.to_i,
currency: Rails.application.secrets.stripe_currency,
description: "wallet -#{@wallet_amount_debit / 100.0}"
)
end
# if @wallet_amount_debit != 0 && !on_site
# invoice_items << Stripe::InvoiceItem.create(
# customer: user.stp_customer_id,
# amount: -@wallet_amount_debit.to_i,
# currency: Rails.application.secrets.stripe_currency,
# description: "wallet -#{@wallet_amount_debit / 100.0}"
# )
# end
# let's return the resulting array of items
invoice_items
true
end
def save_with_payment(operator_profile_id, coupon_code = nil)
begin
clean_pending_strip_invoice_items
build_invoice(invoicing_profile: user.invoicing_profile, statistic_profile: user.statistic_profile, operator_profile_id: operator_profile_id)
invoice_items = generate_invoice_items(false, coupon_code)
generate_invoice_items(false, coupon_code)
rescue StandardError => e
logger.error e
errors[:payment] << e.message
@ -241,13 +186,12 @@ class Reservation < ActiveRecord::Base
customer = Stripe::Customer.retrieve(user.stp_customer_id)
if plan_id
self.subscription = Subscription.find_or_initialize_by(statistic_profile_id: statistic_profile_id)
subscription.attributes = { plan_id: plan_id, statistic_profile_id: statistic_profile_id, card_token: card_token, expiration_date: nil }
subscription.attributes = { plan_id: plan_id, statistic_profile_id: statistic_profile_id, expiration_date: nil }
if subscription.save_with_payment(operator_profile_id, false)
self.stp_invoice_id = invoice_items.first.refresh.invoice
invoice.stp_invoice_id = invoice_items.first.refresh.invoice
# self.stp_invoice_id = invoice_items.first.refresh.invoice # save payment_intent_id instead of stp_invoice_id
# invoice.stp_invoice_id = invoice_items.first.refresh.invoice
invoice.invoice_items.push InvoiceItem.new(
amount: subscription.plan.amount,
stp_invoice_item_id: subscription.stp_subscription_id,
description: subscription.plan.name,
subscription_id: subscription.id
)
@ -259,38 +203,16 @@ class Reservation < ActiveRecord::Base
#
else
# error handling
invoice_items.each(&:delete)
errors[:card] << subscription.errors[:card].join
# errors[:card] << subscription.errors[:card].join
errors[:payment] << subscription.errors[:payment].join if subscription.errors[:payment]
# errors[:payment] << subscription.errors[:payment].join if subscription.errors[:payment]
return false
end
else
begin
if invoice_items.map(&:amount).map(&:to_i).reduce(:+).positive?
card = customer.sources.create(card: card_token)
if customer.default_source.present?
customer.default_source = card.id
customer.save
end
end
#
# IMPORTANT NOTE: here, we have to create an invoice manually and pay it to pay all waiting stripe invoice items
#
stp_invoice = Stripe::Invoice.create(
customer: user.stp_customer_id,
)
# cf: https://board.sleede.com/project/sleede-fab-manager/issue/77
# this function only check reservation total is equal strip invoice total when
# pay only reservation not reservation + subscription
# if !is_equal_reservation_total_and_stp_invoice_total(stp_invoice, coupon_code)
# #raise InvoiceTotalDifferentError
# end
stp_invoice.pay
card&.delete
self.stp_invoice_id = stp_invoice.id
invoice.stp_invoice_id = stp_invoice.id
set_total_and_coupon(coupon_code)
save!
rescue Stripe::CardError => card_error

View File

@ -24,7 +24,6 @@ class Subscription < ActiveRecord::Base
return unless valid?
begin
customer = Stripe::Customer.retrieve(user.stp_customer_id)
invoice_items = []
unless coupon_code.nil?
@ -32,50 +31,21 @@ class Subscription < ActiveRecord::Base
raise InvalidCouponError if @coupon.nil? || @coupon.status(user.id) != 'active'
total = plan.amount
discount = 0
if @coupon.type == 'percent_off'
discount = (total * @coupon.percent_off / 100).to_i
elsif @coupon.type == 'amount_off'
discount = @coupon.amount_off
else
raise InvalidCouponError
end
invoice_items << Stripe::InvoiceItem.create(
customer: user.stp_customer_id,
amount: -discount,
currency: Rails.application.secrets.stripe_currency,
description: "coupon #{@coupon.code} - subscription"
)
end
# only add a wallet invoice item if pay subscription
# dont add if pay subscription + reservation
if invoice
@wallet_amount_debit = get_wallet_amount_debit
if @wallet_amount_debit != 0
invoice_items << Stripe::InvoiceItem.create(
customer: user.stp_customer_id,
amount: -@wallet_amount_debit,
currency: Rails.application.secrets.stripe_currency,
description: "wallet -#{@wallet_amount_debit / 100.0}"
)
end
end
@wallet_amount_debit = get_wallet_amount_debit
new_subscription = customer.subscriptions.create(plan: plan.stp_plan_id, source: card_token)
self.stp_subscription_id = new_subscription.id
self.canceled_at = nil
self.expiration_date = Time.at(new_subscription.current_period_end)
set_expiration_date
save!
UsersCredits::Manager.new(user: user).reset_credits
# generate invoice
stp_invoice = Stripe::Invoice.all(customer: user.stp_customer_id, limit: 1).data.first
if invoice
db_invoice = generate_invoice(operator_profile_id, stp_invoice.id, coupon_code)
db_invoice = generate_invoice(operator_profile_id, coupon_code)
# debit wallet
wallet_transaction = debit_user_wallet
if wallet_transaction
@ -87,39 +57,7 @@ class Subscription < ActiveRecord::Base
# cancel subscription after create
cancel
return true
rescue Stripe::CardError => card_error
clear_wallet_and_goupon_invoice_items(invoice_items)
logger.error card_error
errors[:card] << card_error.message
return false
rescue Stripe::InvalidRequestError => e
clear_wallet_and_goupon_invoice_items(invoice_items)
# Invalid parameters were supplied to Stripe's API
logger.error e
errors[:payment] << e.message
return false
rescue Stripe::AuthenticationError => e
clear_wallet_and_goupon_invoice_items(invoice_items)
# Authentication with Stripe's API failed
# (maybe you changed API keys recently)
logger.error e
errors[:payment] << e.message
return false
rescue Stripe::APIConnectionError => e
clear_wallet_and_goupon_invoice_items(invoice_items)
# Network communication with Stripe failed
logger.error e
errors[:payment] << e.message
return false
rescue Stripe::StripeError => e
clear_wallet_and_goupon_invoice_items(invoice_items)
# Display a very generic error to the user, and maybe send
# yourself an email
logger.error e
errors[:payment] << e.message
return false
rescue StandardError => e
clear_wallet_and_goupon_invoice_items(invoice_items)
# Something else happened, completely unrelated to Stripe
logger.error e
errors[:payment] << e.message
@ -142,7 +80,7 @@ class Subscription < ActiveRecord::Base
# debit wallet
wallet_transaction = debit_user_wallet
invoc = generate_invoice(operator_profile_id, nil, coupon_code)
invoc = generate_invoice(operator_profile_id, coupon_code)
if wallet_transaction
invoc.wallet_amount = @wallet_amount_debit
invoc.wallet_transaction_id = wallet_transaction.id
@ -152,7 +90,7 @@ class Subscription < ActiveRecord::Base
true
end
def generate_invoice(operator_profile_id, stp_invoice_id = nil, coupon_code = nil)
def generate_invoice(operator_profile_id, coupon_code = nil)
coupon_id = nil
total = plan.amount
@ -171,35 +109,25 @@ class Subscription < ActiveRecord::Base
invoicing_profile: user.invoicing_profile,
statistic_profile: user.statistic_profile,
total: total,
stp_invoice_id: stp_invoice_id,
coupon_id: coupon_id,
operator_profile_id: operator_profile_id
)
invoice.invoice_items.push InvoiceItem.new(
amount: plan.amount,
stp_invoice_item_id: stp_subscription_id,
description: plan.name,
subscription_id: id
)
invoice
end
def generate_and_save_invoice(operator_profile_id, stp_invoice_id = nil)
generate_invoice(operator_profile_id, stp_invoice_id).save
def generate_and_save_invoice(operator_profile_id)
generate_invoice(operator_profile_id).save
end
def cancel
return unless stp_subscription_id.present?
stp_subscription = stripe_subscription
stp_subscription.delete(at_period_end: true)
update_columns(canceled_at: Time.now)
end
def stripe_subscription
user.stripe_customer.subscriptions.retrieve(stp_subscription_id) if stp_subscription_id.present?
end
def expire(time)
if !expired?
update_columns(expiration_date: time, canceled_at: time)
@ -316,20 +244,4 @@ class Subscription < ActiveRecord::Base
amount = @wallet_amount_debit / 100.0
WalletService.new(user: user, wallet: user.wallet).debit(amount, self)
end
def clear_wallet_and_goupon_invoice_items(invoice_items)
begin
invoice_items.each(&:delete)
rescue Stripe::InvalidRequestError => e
logger.error e
rescue Stripe::AuthenticationError => e
logger.error e
rescue Stripe::APIConnectionError => e
logger.error e
rescue Stripe::StripeError => e
logger.error e
rescue => e
logger.error e
end
end
end

View File

@ -1,4 +1,8 @@
class ReservationPolicy < ApplicationPolicy
def create?
user.admin?
end
def update?
user.admin? or record.user == user
end