Skip to content

Commit

Permalink
feat: add webhook api (#25)
Browse files Browse the repository at this point in the history
* feat: add endpoint to create webhook

* feat: add endpoint to find webhooks

* feat: add endpoint to delete webhook
  • Loading branch information
aripiprazole authored Jul 21, 2023
1 parent 84f33d6 commit 160a80f
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

278 changes: 278 additions & 0 deletions src/main/kotlin/Webhook.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
@file:JvmName("Webhooks")

package br.com.openpix.sdk

import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.*
import java.net.URI
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
public data class WebhookPayload(
public val name: Int?,
public val url: String?,
public val authorization: String?,
public val isActive: Boolean?,

/**
* Available events to register a webhook to listen to. If no one selected anyone the default event will be
* `OPENPIX:TRANSACTION_RECEIVED`.
*/
public val event: WebhookEvent = WebhookEvent.TransactionReceived,
)

@Serializable
public data class Webhook(
public val id: String,
public val name: String,
public val url: String? = null,
public val authorization: String? = null,
public val isActive: Boolean,
public val createdAt: String? = null,
public val updatedAt: String? = null,

/**
* Available events to register a webhook to listen to. If no one selected anyone the default event will be
* `OPENPIX:TRANSACTION_RECEIVED`.
*/
public val event: WebhookEvent = WebhookEvent.TransactionReceived,
)

@Serializable
public data class WebhookDeleteResponse(public val status: String)

@Serializable
public data class WebhookRequest(public val webhook: WebhookPayload)

@Serializable
public data class WebhookResponse(public val webhook: Webhook)

@Serializable
public data class WebhookListResponse(
public val webhooks: List<Webhook>,
public val pageInfo: PageInfo,
)

@Serializable
public enum class WebhookEvent {
/**
* New charge created
*/
@SerialName("OPENPIX:CHARGE_CREATED")
ChargeCreated,

/**
* Charge completed is when a charge is fully paid
*/
@SerialName("OPENPIX:CHARGE_COMPLETED")
ChargeCompleted,

/**
* Charge expired is when a charge is not fully paid and expired
*/
@SerialName("OPENPIX:CHARGE_EXPIRED")
ChargeExpired,

/**
* New PIX transaction received
*/
@SerialName("OPENPIX:TRANSACTION_RECEIVED")
TransactionReceived,

/**
* New PIX transaction refund received or refunded
*/
@SerialName("OPENPIX:TRANSACTION_REFUND_RECEIVED ")
TransactionRefundReceived,

/**
* Payment confirmed is when the pix transaction related to the payment gets confirmed
*/
@SerialName("OPENPIX:MOVEMENT_CONFIRMED")
MovementConfirmed,

/**
* Payment failed is when the payment gets approved and a error occurs
*/
@SerialName("OPENPIX:MOVEMENT_FAILED")
MovementFailed,

/**
* Payment was removed by user
*/
@SerialName("OPENPIX:MOVEMENT_REMOVED")
MovementRemoved,
}

public class WebhookBuilder internal constructor() {
public var number: Int? by Properties.nullable()
public var url: String? by Properties.nullable()
public var authorization: String? by Properties.nullable()
public var isActive: Boolean? by Properties.nullable()
public var event: WebhookEvent? by Properties.nullable()

/**
* The number of the webhook.
*
* @param number The number of the webhook.
*/
public fun number(number: Int): WebhookBuilder = apply {
this.number = number
}

/**
* The url of the webhook.
*
* @param url The url of the webhook.
*/
public fun url(url: String): WebhookBuilder = apply {
this.url = url
}

/**
* The url of the webhook.
*
* @param url The url of the webhook.
*/
public fun url(url: Url): WebhookBuilder = apply {
this.url = url.toString()
}

/**
* The url of the webhook.
*
* @param uri The url of the webhook.
*/
public fun uri(uri: URI): WebhookBuilder = apply {
this.url = uri.toASCIIString()
}

/**
* The authorization of the webhook.
*
* @param authorization The authorization of the webhook.
*/
public fun authorization(authorization: String): WebhookBuilder = apply {
this.authorization = authorization
}

/**
* The isActive of the webhook.
*
* @param isActive The isActive of the webhook.
*/
public fun isActive(isActive: Boolean): WebhookBuilder = apply {
this.isActive = isActive
}

/**
* Makes active
*/
public fun enableWebhook(): WebhookBuilder = apply {
this.isActive = true
}

/**
* The event of the webhook.
*
* @param event The event of the webhook.
*/
public fun event(event: WebhookEvent): WebhookBuilder = apply {
this.event = event
}

/**
* Changes the event to charge created.
*/
public fun onChargeCreated(): WebhookBuilder = apply {
this.event = WebhookEvent.ChargeCreated
}

/**
* Changes the event to charge completed.
*/
public fun onChargeCompleted(): WebhookBuilder = apply {
this.event = WebhookEvent.ChargeCompleted
}

/**
* Changes the event to charge expired.
*/
public fun onChargeExpired(): WebhookBuilder = apply {
this.event = WebhookEvent.ChargeExpired
}

/**
* Changes the event to transaction received.
*/
public fun onTransactionReceived(): WebhookBuilder = apply {
this.event = WebhookEvent.TransactionReceived
}

/**
* Changes the event to transaction refund received.
*/
public fun onTransactionRefundReceived(): WebhookBuilder = apply {
this.event = WebhookEvent.TransactionRefundReceived
}

/**
* Changes the event to movement confirmed.
*/
public fun onMovementConfirmed(): WebhookBuilder = apply {
this.event = WebhookEvent.MovementConfirmed
}

/**
* Changes the event to movement failed.
*/
public fun onMovementFailed(): WebhookBuilder = apply {
this.event = WebhookEvent.MovementFailed
}

/**
* Changes the event to movement removed.
*/
public fun onMovementRemoved(): WebhookBuilder = apply {
this.event = WebhookEvent.MovementRemoved
}

@JvmSynthetic
internal fun build(): WebhookRequest {
return WebhookRequest(
webhook = WebhookPayload(
name = number,
url = url,
authorization = authorization,
isActive = isActive,
event = event ?: WebhookEvent.TransactionReceived,
),
)
}
}

@JvmSynthetic
public suspend fun WooviSDK.allWebhooks(url: String? = null): WebhookListResponse {
return client
.get("/api/v1/webhook") {
url?.let { parameter("url", it) }
}
.body<WebhookListResponse>()
}

@JvmSynthetic
public suspend fun WooviSDK.deleteWebhook(id: String): WebhookDeleteResponse {
return client.delete("/api/v1/webhook/$id").body<WebhookDeleteResponse>()
}

@JvmSynthetic
public suspend fun WooviSDK.createWebhook(
value: WebhookBuilder = WebhookBuilder(),
builder: WebhookBuilder.() -> Unit,
): WebhookResponse {
return client
.post("/api/v1/webhook") { setBody(value.apply(builder).build()) }
.body<WebhookResponse>()
}
33 changes: 32 additions & 1 deletion src/main/kotlin/WooviSDK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import kotlinx.serialization.json.Json
@JvmSynthetic
public suspend fun main() {
val sdk = WooviSDK(appId = System.getenv("APP_ID"))
println(sdk.allPayments())
println(sdk.deleteWebhook("V2ViaG9vazo2NDg3NzNjY2JlYmNjZTI0NzQ3ZmIwZGM="))
}

/**
Expand Down Expand Up @@ -252,6 +252,37 @@ public class WooviSDK @JvmOverloads public constructor(
createRefund(value) {}
}

/**
* Deletes a webhook by id.
*
* @param id The webhook id.
* @return The webhook delete response.
*/
public fun deleteWebhookAsync(id: String): Future<WebhookDeleteResponse> = future {
deleteWebhook(id)
}

/**
* Returns all webhooks.
*
* @param url The webhook url.
* @return The webhook list response.
*/
@JvmOverloads
public fun allWebhooksAsync(url: String? = null): Future<WebhookListResponse> = future {
allWebhooks(url)
}

/**
* Creates a new webhook.
*
* @param builder The webhook builder.
* @return The webhook response.
*/
public fun createWebhookAsync(builder: WebhookBuilder): Future<WebhookResponse> = future {
createWebhook(builder) {}
}

/**
* Removes JSON specification restriction (RFC-4627) and makes parser
* more liberal to the malformed input. In lenient mode quoted boolean literals,
Expand Down

0 comments on commit 160a80f

Please sign in to comment.