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

Webhook, Signature verfication integration & refactored structure #5

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2390c72
refactor: modularize chargily.go into dedicated model files for enhan…
BigBr41n Oct 15, 2024
2000cad
update : the models path to use the params ,the responses types and t…
BigBr41n Oct 15, 2024
47e8b30
refactor: grouping the utils.go(requests_sender.go) and errors(custom…
BigBr41n Oct 15, 2024
500653a
update: using the new utils folder to call the request sender and the…
BigBr41n Oct 15, 2024
8f75237
update: using the new path of utils to use the request sender
BigBr41n Oct 15, 2024
a78c784
Merge pull request #22 from BigBr41n/refactor/utils
BigBr41n Oct 15, 2024
cd1d371
refactor: restructure SDK to centralize functionality
BigBr41n Oct 15, 2024
348a4cc
refactor: reorganize SDK functions for unified access
BigBr41n Oct 15, 2024
c708ec9
Merge pull request #23 from BigBr41n/refactor/client/services
BigBr41n Oct 15, 2024
47ce6e7
update & refactor : changing the folder struct for more clarity and f…
BigBr41n Oct 15, 2024
8d0ef86
update & refactor : changing the folder struct for more clarity and f…
BigBr41n Oct 15, 2024
f8141eb
new: create a client example code
BigBr41n Oct 15, 2024
4b7d184
update & refactor : changing the folder struct for each of the old ex…
BigBr41n Oct 15, 2024
3b18af3
Merge pull request #24 from BigBr41n/update/examples
BigBr41n Oct 15, 2024
cd4dca4
refactor & new: moving the types to types.go file and adding new even…
BigBr41n Oct 16, 2024
f7bb3f1
feat: webhook setup function to create a webhook handler, custome rou…
BigBr41n Oct 16, 2024
f3ee0e8
feat(webhook): implement webhook functionality for client integration
BigBr41n Oct 16, 2024
92dd032
new: new examples about the webhook setup process, usage and the sig…
BigBr41n Oct 16, 2024
bdd3cc0
Merge pull request #25 from BigBr41n/new/webhook/signature-verifier
BigBr41n Oct 16, 2024
138bed8
new: adding missed examples
BigBr41n Oct 16, 2024
aa17492
update: updating README.md file
BigBr41n Oct 16, 2024
8d38337
new: more detailed docs for each service (cusomters, checkouts, payme…
BigBr41n Oct 16, 2024
fe4baa1
Merge pull request #26 from BigBr41n/update/docs/examples
BigBr41n Oct 16, 2024
b2774e5
Merge pull request #27 from BigBr41n/refactor/improve-architecture
BigBr41n Oct 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
544 changes: 78 additions & 466 deletions README.md

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions docs/Balance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Balance Struct Documentation

The `Balance` struct in the `chargily` package is used to retrieve the balance from the Chargily API.

## Method: `Get`

This method retrieves the current balance from the Chargily API.

### Syntax

```go
func (b *Balance) Get() (*models.Balance, error)
```

### Description

The Get method sends a GET request to the Chargily API to retrieve the current balance.
It returns the balance data in the form of a models.Balance object, along with any error that occurred during the request.

### Parameters

No params passed

### Returns

\*models.Balance: The current balance as a Balance object.
error: If the request fails, an error is returned.

### Example Usage

```go
package main

import (
"fmt"
"log"

"github.com/Chargily/chargily-pay-go/pkg/chargily"
)

func main() {
// Initialize the API client
client := chargily.NewClient("API_KEY")


// Retrieve the balance
balance, err := client.Balance.Get()
if err != nil {
log.Fatalf("Error retrieving balance: %v", err)
}

// Print the balance
fmt.Printf("Current Balance: %+v\n", balance)
}
```

### Error Handling

If an error occurs during the request, the Get method will return an error object. You should always check the error and handle it appropriately.
189 changes: 189 additions & 0 deletions docs/Checkouts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
# Checkouts Module Documentation

## Overview

The `Checkouts` struct in the `chargily` package provides methods for managing checkout functionalities within the Chargily payment API. This module allows users to create, retrieve, and manage checkouts and their associated items.

## Checkouts Structure

### Fields

- **client**: A pointer to the `Client` structure, which holds the API key, endpoint, and other necessary configurations to make API requests.

## Methods

### Create

#### Parameters

- **checkout**: A pointer to a `models.CheckoutParams` structure containing the details needed to create a new checkout.

#### Returns

- **(\*models.Checkout, error)**: A pointer to the newly created `models.Checkout` object and an error, if any occurs during the request.

#### Description

The `Create` method sends a request to the Chargily API to create a new checkout with the provided details. It constructs the API request and parses the response into a `models.Checkout` object.

#### Example

```go
func CreateCheckout(client *chargily.Client , id string) {
// Create example items to be added to the checkout.
items := []models.CItems{
{
Price: id,
Quantity: 2,
},
}

// Initialize the CheckoutParams struct with required fields.
checkout := &models.CheckoutParams{
Items: items,
PaymentMethod: "edahabia",
SuccessURL: "https://your-site.com/success",
FailureURL: "https://your-site.com/failure",
WebhookEndpoint: "https://your-site.com/webhook",
Description: "Checkout for Order #12345",
Locale: "en",
PercentageDiscount: 10,
}

// Create the checkout using the client.
checkoutData, err := client.Checkouts.Create(checkout)
if err != nil {
// Handle creation error.
fmt.Printf("Error creating checkout: %v\n", err)
return
}

// Output the details of the created checkout.
fmt.Printf("Checkout created successfully: %+v\n", checkoutData)
fmt.Printf("Checkout Parameters: %+v\n", checkout)
}
```

---

### Get

#### Parameters

- **checkoutId**: A string representing the unique identifier of the checkout to be retrieved.

#### Returns

- **(\*models.Checkout, error)**: A pointer to the retrieved `models.Checkout` object and an error, if any occurs during the request.

#### Description

The `Get` method retrieves the details of a specific checkout identified by `checkoutId`. It sends a GET request to the API and parses the response into a `models.Checkout` object.

#### Example

```go
func RetrieveCheckout(client *chargily.Client, checkoutID string) {
// Retrieve the checkout using its ID.
checkoutData, err := client.Checkouts.Get(checkoutID)
if err != nil {
// Handle retrieval error.
fmt.Printf("Error retrieving checkout: %v\n", err)
return
}
// Output the details of the retrieved checkout.
fmt.Printf("Checkout retrieved successfully: %+v\n", checkoutData)
}
```

---

### GetAll

#### Returns

- **(\*models.RetrieveAll[models.Checkout], error)**: A pointer to a `models.RetrieveAll[models.Checkout]` structure containing an array of all checkouts and an error, if any occurs during the request.

#### Description

The `GetAll` method retrieves a list of all available checkouts stored in the Chargily system. It sends a GET request to the API and parses the response into a `models.RetrieveAll[models.Checkout]` structure, which includes an array of checkout objects.

#### Example

```go
func ListAllCheckouts(client *chargily.Client) {
// Call the GetAll method on the Checkouts service to retrieve all checkouts.
checkouts, err := client.Checkouts.GetAll()
if err != nil {
// Handle retrieval error.
fmt.Printf("Error retrieving all checkouts: %v\n", err)
return
}
// Output the list of successfully retrieved checkouts.
fmt.Printf("All checkouts retrieved successfully: %+v\n", checkouts)
}
```

---

### GetItems

#### Parameters

- **checkoutId**: A string representing the unique identifier of the checkout whose items are to be retrieved.

#### Returns

- **(\*models.RetrieveAll[models.CheckoutItems], error)**: A pointer to a `models.RetrieveAll[models.CheckoutItems]` structure containing an array of items associated with the checkout and an error, if any occurs during the request.

#### Description

The `GetItems` method retrieves the items associated with a specific checkout identified by `checkoutId`. It sends a GET request to the API and parses the response into a `models.RetrieveAll[models.CheckoutItems]` structure, which includes an array of item data.

#### Example

```go
func RetrieveCheckoutItems(client *chargily.Client, checkoutID string) {
// Retrieve the items from the checkout using its ID.
checkoutItems, err := client.Checkouts.GetItems(checkoutID)
if err != nil {
// Handle retrieval error.
fmt.Printf("Error retrieving checkout items: %v\n", err)
return
}
// Output the details of the retrieved checkout items.
fmt.Printf("Checkout items retrieved successfully: %+v\n", checkoutItems)
}
```

---

### Expire

#### Parameters

- **checkoutId**: A string representing the unique identifier of the checkout to be expired.

#### Returns

- **(\*models.Checkout, error)**: A pointer to the expired `models.Checkout` object and an error, if any occurs during the request.

#### Description

The `Expire` method sends a request to the Chargily API to expire a checkout identified by `checkoutId`. It constructs the API request and returns the corresponding checkout object if the expiration is successful.

#### Example

```go
func CancelCheckout(client *chargily.Client, checkoutID string) {
// Call the Expire method on the Checkouts service to cancel the checkout.
checkoutEX, err := client.Checkouts.Expire(checkoutID)
if err != nil {
// Handle cancellation error.
fmt.Printf("Error canceling checkout: %v\n", err)
return
}
// Output the details of the successfully canceled checkout.
fmt.Printf("Checkout canceled successfully: %+v\n", checkoutEX)
}
```
85 changes: 85 additions & 0 deletions docs/Client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Chargily Client Documentation

## Overview

The `chargily` package provides a `Client` structure that facilitates interaction with the Chargily payment API. This client serves as a gateway to access various functionalities offered by the Chargily platform, including balance inquiries, customer management, product pricing, payment link generation, checkout processes, and webhook handling.

## Client Structure

### Definition

```go
type Client struct {
apiKey string
endpoint string
rs utils.RequestSenderI // RequestSender for custom HTTP requests
mode Mode
Balance *Balance // Access balance-related functionalities
Customers *Customers // Access customer management functionalities
Prices *Prices // Access pricing-related functionalities
Products *Products // Access product management functionalities
PaymentLinks *PaymentLinks // Access payment link functionalities
Checkouts *Checkouts // Access checkout functionalities
Webhook *Webhook // Access webhook management functionalities
}
```

### Fields

- `apiKey`: A string representing the API key required to authenticate requests to the Chargily API.
- `endpoint`: A string representing the base URL of the Chargily API, which varies based on the operational mode (production or test).
- `rs`: An instance of RequestSenderI, used for sending custom HTTP requests to the API.
- `mode`: An enumeration indicating whether the client operates in "test" or "prod" mode, affecting the API endpoint.
- `Balance`: A pointer to the Balance structure, providing access to balance-related functionalities.
- `Customers`: A pointer to the Customers structure, enabling operations related to customer management.
- `Prices`: A pointer to the Prices structure, facilitating pricing-related actions.
- `Products`: A pointer to the Products structure, offering methods for product management.
- `PaymentLinks`: A pointer to the PaymentLinks structure, used for generating and managing payment links.
- `Checkouts`: A pointer to the Checkouts structure, providing functionality for handling checkout processes.
- `Webhook`: A pointer to the Webhook structure, allowing the client to manage webhook notifications.

### NewClient Function

```go
func NewClient(apiKey, mode string) (*Client, error)
```

### Parameters

- apiKey: A string representing the API key used for authenticating requests.
- mode: A string that determines the operating mode of the client. Acceptable values are:
- "prod": Indicates that the client operates in production mode.
- "test": Indicates that the client operates in test mode.

### Returns

(\*Client, error): A pointer to the initialized Client and an error, if any occurs during initialization.

### Description

The NewClient function initializes a new instance of the Client structure, setting up the necessary configurations based on the provided API key and operational mode.

- The function verifies the mode parameter to determine the appropriate API base URL:
- If the mode is "prod", it sets the API base URL to the production endpoint.
- If the mode is "test", it sets the API base URL to the test endpoint.
- If the mode is invalid, it returns an ErrInvalidMode error.
- It creates a new request sender to facilitate HTTP communications.
- The resulting Client provides access to all functionalities within the Chargily API ecosystem, enabling efficient management of payments and related services.

### Example Usage

```go
client, err := NewClient("your_api_key", "prod")
if err != nil {
// Handle error
}

// Accessing different functionalities
// client.Balance.Get()
// client.Customers.Crate(), .Update() ...
// client.Prices.Create() ...
// client.Products.Update() ...
// client.PaymentLinks.Create(), ...
// client.Checkouts.GetAll() ...
// client.Webhook.Setup()
```
Loading