Skip to content

Commit

Permalink
Merge pull request #6 from blessedjasonmwanza/php-library
Browse files Browse the repository at this point in the history
Feat: Add Verify payment feature, tests and documentation
  • Loading branch information
blessedjasonmwanza authored Oct 21, 2024
2 parents fee0e47 + 818da67 commit 69da504
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 25 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
],
"require": {
"php": ">=7.0"
"php": ">=7.4"
},
"license": "MIT",
"require-dev": {
Expand Down
75 changes: 54 additions & 21 deletions php_usage_documentation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# MoneyUnify Library PHP Documentation

## Introduction

The **MoneyUnify** library provides an easy interface for integrating with the Money Unify API to process payments. This documentation will guide you through the installation process and demonstrate how to use the library effectively.

Expand All @@ -25,9 +24,9 @@ To use the MoneyUnify library, follow these steps:

3. **Call the `requestPayment` Method**: This method takes the payer's phone number and the amount to pay as parameters.

### Example Code
### Example Code for Requesting a Payment

Here’s an example demonstrating how to use the MoneyUnify library:
Here’s an example demonstrating how to use the MoneyUnify library to request a payment:

```php
<?php
Expand Down Expand Up @@ -58,15 +57,6 @@ if ($response->isError) {
}
```

### Explanation of the Code

- **Line 1**: Loads Composer's autoload file, allowing you to use the MoneyUnify library.
- **Line 5**: Imports the `MoneyUnify` class from the `Blessedjasonmwanza\MoneyUnify` namespace.
- **Line 8**: Creates a new instance of the `MoneyUnify` class, passing in your unique MUID.
- **Line 12**: Calls the `requestPayment` method with the payer's phone number and the payment amount.
- **Lines 15-23**: Checks if there was an error in the response and handles it accordingly, or displays the success message.


### Example Successful Response

Here’s an example of a successful transaction response from the Money Unify API:
Expand All @@ -84,28 +74,71 @@ Here’s an example of a successful transaction response from the Money Unify AP
"isError": false
}
```
## Conclusion

The MoneyUnify library simplifies the process of integrating with the Money Unify API. By following the steps outlined in this documentation, you can easily set up and make payment requests. For further assistance, feel free to reach out or check the official documentation for more advanced features.
### Verifying a Payment

To verify a payment after requesting it, you can use the `verifyPayment` method. Here's how you can implement it:

# Author
1. **Call the `verifyPayment` Method**: This method takes the transaction reference as a parameter.

👤 **Blessed Jason Mwanza** - [Buy him a Coffee](https://www.buymeacoffee.com/mwanzabj)
### Example Code for Verifying a Payment

- LinkedIn: [Connect with Blessed on LinkedIn](https://www.linkedin.com/in/blessedjasonmwanza)
```php
// Assuming the previous code is already present...

// Step 4: Verify the payment
$transactionReference = 'your_transaction_reference'; // Replace with the transaction reference
$verificationResponse = $moneyUnify->verifyPayment($transactionReference);

// Step 5: Check the verification response
if ($verificationResponse->isError) {
// Handle the error
echo "Error: " . $verificationResponse->message . "\n";
echo "Console: " . ($verificationResponse->console ?? 'No console message to debug') . "\n";
} else {
// Verification was successful
echo "Verification Success: " . $verificationResponse->message . "\n";
echo "Data: " . json_encode($verificationResponse->data) . "\n";
}
```

- Github : [@blessedjasonmwanza](https://github.com/blessedjasonmwanza)
### Example Verification Response

- Twitter : [Follow Blessed Jason @mwanzabj](https://twitter.com/mwanzabj)
Here’s an example of a verification response from the Money Unify API:

- Youtube : [Youtube](https://www.youtube.com/@blessedjasonmwanza)
```json
{
"message": "Transaction pending OTP confirmation",
"data": {
"amount": "1.00",
"customer_name": "Dilip Okafor",
"customerMobileWallet": "260975555555",
"reference": "0975555555_1713447717",
"status": "otp-required"
},
"isError": false
}
```

## Conclusion

The MoneyUnify library simplifies the process of integrating with the Money Unify API. By following the steps outlined in this documentation, you can easily set up and make payment requests, as well as verify transactions. For further assistance, feel free to reach out or check the official documentation for more advanced features.

# Author

👤 **Blessed Jason Mwanza** - [Buy him a Coffee](https://www.buymeacoffee.com/mwanzabj)

- LinkedIn: [Connect with Blessed on LinkedIn](https://www.linkedin.com/in/blessedjasonmwanza)
- Github: [@blessedjasonmwanza](https://github.com/blessedjasonmwanza)
- Twitter: [Follow Blessed Jason @mwanzabj](https://twitter.com/mwanzabj)
- Youtube: [Youtube](https://www.youtube.com/@blessedjasonmwanza)

# 🤝 Contributing

Contributions, issues, and feature requests are welcome!

Feel free to check the [issues page](https://github.com/blessedjasonmwanza/MoneyUnify/issues).

# Show your support

Give a ⭐️ if you like this project! or [Donate](https://www.buymeacoffee.com/mwanzabj)
Give a ⭐️ if you like this project! or [Donate](https://www.buymeacoffee.com/mwanzabj)
21 changes: 19 additions & 2 deletions src/MoneyUnify.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
* This class provides methods to interact with the Money Unify API for processing payments.
*/
class MoneyUnify {
private const BASE_URL = 'https://api.moneyunify.com/v2';
private string $muid; // The unique identifier for the Money Unify account
private string $request_payment_url = "https://api.moneyunify.com/v2/request_payment"; // URL for payment requests
private string $request_payment_url = self::BASE_URL."/request_payment"; // URL for payment requests
private string $verify_payment_url = self::BASE_URL."/verify_transaction";
public \stdClass $response; // Response object for API calls

/**
Expand All @@ -27,7 +29,7 @@ public function __construct(string $muid) {
*
* @param string $payer_phone_number The phone number of the payer.
* @param string $amount_to_pay The amount to be paid.
* @return \stdClass The response from the API.
* @return \stdClass Transaction details of the response from the API.
*/
public function requestPayment(string $payer_phone_number, string $amount_to_pay): \stdClass {
$body_fields = http_build_query([
Expand All @@ -40,6 +42,21 @@ public function requestPayment(string $payer_phone_number, string $amount_to_pay
return $this->urlRequestResponse($requestResponse);
}

/**
* verify and get payment details of a transaction.
*
* @param string $transaction_reference id of the transaction.
* @return \stdClass transaction details the response from the API.
*/
public function verifyPayment(string $transaction_reference): \stdClass{
$body_fields = http_build_query([
'muid' => $this->muid,
'reference' => $transaction_reference,
]);

$requestResponse = $this->urlRequest($this->verify_payment_url, $body_fields);
return $this->urlRequestResponse($requestResponse);
}
/**
* Processes the response from the API request.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/requestPaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require 'vendor/autoload.php';

describe('Expect RequestPayment feature to work properly', function () {
describe('Expect requestPayment feature to work properly', function () {
$muid = '123'; // Presumably invalid muid for testing
$moneyUnify = new MoneyUnify ($muid);
$requestToPay = $moneyUnify->requestPayment('0971943638', '10');
Expand Down
32 changes: 32 additions & 0 deletions tests/Feature/verifyPaymentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Blessedjasonmwanza\MoneyUnify\MoneyUnify;

require 'vendor/autoload.php';

describe('Expect verifyPayment feature to work properly', function () {
$muid = '123'; // Presumably invalid muid for testing
$moneyUnify = new MoneyUnify ($muid);
$requestToPay = $moneyUnify->verifyPayment('0975555555_1713447717');

// Test if the result is a valid object
it('is a valid object', function () use ($requestToPay) {
expect($requestToPay)->toBeObject(); // Ensure the response is an object
});

// Test if the object has required properties
it('has message and isError properties', function () use ($requestToPay) {
expect($requestToPay)->toHaveProperty('message'); // Checks if 'message' exists
expect($requestToPay)->toHaveProperty('isError'); // Checks if 'isError' exists
});

// Test for invalid muid resulting in isError being true
it('isError property is true due to invalid muid', function () use ($requestToPay) {
expect($requestToPay->isError)->toBeTrue(); // As the muid is invalid, isError should be true
});

// Test if the 'console' property is present in case of an error
it('has console property due to invalid muid', function () use ($requestToPay) {
expect($requestToPay)->toHaveProperty('console'); // Error responses typically include console data
});
});

0 comments on commit 69da504

Please sign in to comment.