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

Declarative SuccessfulPayment handler #84

Open
aaabramov opened this issue Jan 4, 2020 · 2 comments
Open

Declarative SuccessfulPayment handler #84

aaabramov opened this issue Jan 4, 2020 · 2 comments

Comments

@aaabramov
Copy link
Contributor

Docs: https://core.telegram.org/bots/api#successfulpayment

Add declarative handler for successful payments to com.bot4s.telegram.api.declarative.Payments like it is done for com.bot4s.telegram.api.declarative.Payments#onPreCheckoutQuery.

@mukel
Copy link
Member

mukel commented Jan 5, 2020

You can add this extension yourself, it's not there by default since successful payments come attached to a message and not in an Update which is the common pattern for these extension methods.
Here's how it would look like:

package com.bot4s.telegram.api.declarative

import cats.instances.list._
import cats.syntax.flatMap._
import cats.syntax.functor._
import cats.syntax.traverse._
import com.bot4s.telegram.api.BotBase
import com.bot4s.telegram.models.{Message, SuccessfulPayment}

import scala.collection.mutable

/**
  * Extension for processing (successful) payments.
  * See [[https://core.telegram.org/bots/payments]].
  */
trait MyPayments[F[_]] extends BotBase[F] {

  private val successfulPaymentActions = mutable.ArrayBuffer[Action[F, SuccessfulPayment]]()

  /**
    * Executes 'action' for every successful payment.
    */
  def onSuccessfulPayment(action: Action[F, SuccessfulPayment]): Unit = {
    successfulPaymentActions += action
  }

  override def receiveMessage(message: Message): F[Unit] =
    for {
      _ <- successfulPaymentActions.toList.traverse(action => message.successfulPayment.map(action).getOrElse(unit))
      _ <- super.receiveMessage(message)
    } yield ()
}

If you have any issues with payments, bugs or even feature requests (e.g. in the usability department) please file an issue; I'm glad to help.

@aaabramov
Copy link
Contributor Author

@mukel Thanks a lot. I have already done that. I filed this issue to pick up it later.
It seems reasonable to have such trait in core lib that having it implemented everywhere.

If we have onPreCheckoutQuery directive, what is the reason for not adding onSuccessfulPayment to core library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants