Skip to content

Commit

Permalink
Paddle Billing Updates (#875)
Browse files Browse the repository at this point in the history
* update paddle billing charge code and docs

* update processor_subscription on paddle billable

* standardrb fix

* update paddle charge method to create transaction

* move to creating transactions

* update to use `grand_total` instead of `total`. fixes #874

* standardrb fix

* should return Pay::Error if there are no options provided

* standardrb fix
  • Loading branch information
deanpcmad authored Oct 24, 2023
1 parent 3bc8899 commit c2ef503
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
23 changes: 23 additions & 0 deletions docs/5_charges.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ On failure, a `Pay::Error` will be raised with details about the payment failure

##### Paddle Classic Charges

When creating charges with Paddle, they need to be approved by the customer. This is done by
passing the Paddle Transaction ID to a Paddle.js checkout.

To see the required fields, see the [Paddle API docs](https://developer.paddle.com/api-reference/transactions/create-transaction).

The amount can be set to 0 as this will be set by the Price set on Paddle, so will be ignored.

```ruby
@user.payment_processor.charge(0, {
items: [
{
quantity: 1,
price_id: "pri_abc123"
}
],
# include additional fields here
})
```

Then you can set the `transactionId` attribute for Paddle.js. For more info, see the [Paddle.js docs](https://developer.paddle.com/paddlejs/methods/paddle-checkout-open)

##### Paddle Classic Charges

Paddle Classic requires an active subscription on the customer in order to create a one-time charge. It also requires a `charge_name` for the charge.

```ruby
Expand Down
31 changes: 14 additions & 17 deletions lib/pay/paddle/billable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,23 @@ def update_customer!(**attributes)
end

def charge(amount, options = {})
subscription = pay_customer.subscription
return unless subscription.processor_id
raise Pay::Error, "A charge_name is required to create a one-time charge" if options[:charge_name].nil?
return Pay::Error unless options

response = PaddlePay::Subscription::Charge.create(subscription.processor_id, amount.to_f / 100, options[:charge_name], options)
items = options[:items]
opts = options.except(:items).merge(customer_id: processor_id)
transaction = ::Paddle::Transaction.create(items: items, **opts)

attributes = {
amount: (response[:amount].to_f * 100).to_i,
paddle_receipt_url: response[:receipt_url],
created_at: Time.zone.parse(response[:payment_date])
attrs = {
amount: transaction.details.totals.grand_total,
created_at: transaction.created_at,
currency: transaction.currency_code,
metadata: transaction.details.line_items&.first&.id
}

# Lookup subscription payment method details
attributes.merge! Pay::Paddle::PaymentMethod.payment_method_details_for(subscription_id: subscription.processor_id)

charge = pay_customer.charges.find_or_initialize_by(processor_id: response[:invoice_id])
charge.update(attributes)
charge = pay_customer.charges.find_or_initialize_by(processor_id: transaction.id)
charge.update(attrs)
charge
rescue ::PaddlePay::PaddlePayError => e
rescue ::Paddle::Error => e
raise Pay::Paddle::Error, e
end

Expand All @@ -83,9 +81,8 @@ def trial_end_date(subscription)
end

def processor_subscription(subscription_id, options = {})
hash = PaddlePay::Subscription::User.list({subscription_id: subscription_id}, options).try(:first)
OpenStruct.new(hash)
rescue ::PaddlePay::PaddlePayError => e
::Paddle::Subscription.retrieve(id: subscription_id)
rescue ::Paddle::Error => e
raise Pay::Paddle::Error, e
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pay/paddle/charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.sync(charge_id, object: nil, try: 0, retries: 1)
end

attrs = {
amount: object.details.totals.total,
amount: object.details.totals.grand_total,
created_at: object.created_at,
currency: object.currency_code,
metadata: object.details.line_items&.first&.id,
Expand Down

0 comments on commit c2ef503

Please sign in to comment.