hachchadi / cmi-payment
一个用于CMI支付集成的Laravel包
v1.0.2
2024-07-12 15:43 UTC
Requires
- php: >=8.0
- guzzlehttp/guzzle: ^7.0
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^7.42
- pestphp/pest: ^1.23
README
简介
此Laravel包将CMI(中心货币间)支付网关集成到您的Laravel应用程序中。它提供了方便的方法来启动支付、生成哈希和检查订单状态。
安装
您可以通过Composer安装此包。运行以下命令
composer require hachchadi/cmi-payment
然后,发布配置文件
php artisan vendor:publish --provider="Hachchadi\CmiPayment\CmiPaymentServiceProvider"
此命令将在您的config目录中发布cmi.php配置文件。
配置
在您的.env文件中更新必要的CMI配置值
CMI_CLIENT_ID=your_cmi_client_id CMI_STORE_KEY=your_cmi_store_key CMI_BASE_URI=https://testpayment.cmi.co.ma/fim/est3Dgate CMI_OK_URL=https://your-app.com/payment/success CMI_FAIL_URL=https://your-app.com/payment/failure CMI_CALLBACK_URL=https://your-app.com/payment/callback CMI_SHOP_URL=(https://your-app.com)
要检查订单状态,请通过CMI API添加以下内容到您的.env
CMI_BASE_URI_API=https://testpayment.cmi.co.ma/fim/api CMI_API_CREDENTIALS_NAME=your_cmi_name CMI_API_CREDENTIALS_PASSWORD=your_cmi_password CMI_API_CREDENTIALS_CLIENT_ID=your_cmi_client_id
如果您更喜欢硬编码值或需要调整默认值,可以直接修改config/cmi.php。
用法
1. 处理支付
您可以使用CmiClient类来启动支付。以下是在控制器中处理支付的示例
use Illuminate\Http\Request; use Hachchadi\CmiPayment\CmiClient; class PaymentController extends Controller { public function processPayment(Request $request, CmiClient $cmiClient) { try { $data = [ 'amount' => $request->input('amount'), 'currency' => $request->input('currency'), 'oid' => $request->input('orderid'), 'email' => $request->input('email'), 'billToName' => $request->input('billToName'), // Add other required fields as needed ]; // Process the payment $cmiClient->processPayment($data); } catch (\Exception $e) { // Handle any exceptions return response()->json(['error' => $e->getMessage()], 500); } } }
2. 检查订单状态
要使用CMI API检查订单状态,请在使用控制器的getCmiStatus方法
use Hachchadi\CmiPayment\CmiClient; class OrderController extends Controller { public function checkOrderStatus($orderId, CmiClient $cmiClient) { try { $status = $cmiClient->getCmiStatus($orderId); // Handle status response return response()->json(['status' => $status]); } catch (\Exception $e) { // Handle any exceptions return response()->json(['error' => $e->getMessage()], 500); } } }
3. 验证响应哈希
use Hachchadi\CmiPayment\CmiClient; use Hachchadi\CmiPayment\Exceptions\InvalidResponseHash; public function handleCallback(Request $request) { try { $responseData = $request->all(); if ($responseData) { // Validate the hash $isValid = $this->cmiClient->validateHash($responseData); if ($isValid && $_POST['ProcReturnCode'] == '00') { $response = 'ACTION=POSTAUTH'; } else { $response = 'FAILURE'; } } else { $response = 'No Data POST'; } return $response; } catch (InvalidResponseHash $e) { Log::error($e->getMessage()); } }