diff --git a/composer.json b/composer.json index b1db4b5..3034554 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ } ], "require": { - "php": ">=7.0" + "php": ">=7.4" }, "license": "MIT", "require-dev": { diff --git a/php_usage_documentation.md b/php_usage_documentation.md index bfd91bd..b461756 100644 --- a/php_usage_documentation.md +++ b/php_usage_documentation.md @@ -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. @@ -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 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: @@ -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) \ No newline at end of file +Give a ⭐️ if you like this project! or [Donate](https://www.buymeacoffee.com/mwanzabj) \ No newline at end of file diff --git a/src/MoneyUnify.php b/src/MoneyUnify.php index c80e4d2..8a15279 100644 --- a/src/MoneyUnify.php +++ b/src/MoneyUnify.php @@ -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 /** @@ -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([ @@ -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. * diff --git a/tests/Feature/requestPaymentTest.php b/tests/Feature/requestPaymentTest.php index c39f0eb..8b8859c 100644 --- a/tests/Feature/requestPaymentTest.php +++ b/tests/Feature/requestPaymentTest.php @@ -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'); diff --git a/tests/Feature/verifyPaymentTest.php b/tests/Feature/verifyPaymentTest.php new file mode 100644 index 0000000..bcf29a8 --- /dev/null +++ b/tests/Feature/verifyPaymentTest.php @@ -0,0 +1,32 @@ +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 + }); +});