PHP code example of vagh / laravel-allinpay
1. Go to this page and download the library: Download vagh/laravel-allinpay library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vagh / laravel-allinpay example snippets
'tongLianPay' => [
'app_id' => env('TONGLIAN_APP_ID'),
'cus_id' => env('TONGLIAN_CUS_ID'),
'app_version' => env('TONGLIAN_API_VERSION', 11),
'is_test' => env('TONGLIAN_IS_TEST', true)
],
// 依赖注入
use Vagh\LaravelAllInPay\AllInPay as TongLianPay;
use Vagh\LaravelAllInPay\Exceptions\Exception;
use Vagh\LaravelAllInPay\Exceptions\HttpException;
use Vagh\LaravelAllInPay\Exceptions\InvalidArgumentException;
use Vagh\LaravelAllInPay\Exceptions\ServiceException;
protected $pay_sdk;
public function __construct(TongLianPay $pay_sdk)
{
$this->pay_sdk = $pay_sdk;
}
public function createOrder()
{
try {
$params = [];
$result = $this->pay_sdk->payJSApi($params);
var_dump($result);exit;
} catch (Exception $e) {
$message = '未知错误:'.$e->getMessage();
if ($e instanceof HttpException) {
$message = '请求接口失败:'.$e->getMessage();
}
if ($e instanceof InvalidArgumentException) {
$message = '接口参数错误:'.$e->getMessage();
}
if ($e instanceof ServiceException) {
$message = '业务处理错误:'.$e->getMessage();
}
throw new \Exception($message, $e->getCode());
}
}
// 服务名访问
public function edit()
{
$params = []; // 请参照文档填写参数
$response = app('tongLianPay')->payJSApi($params);
}
Vagh\LaravelAllInPay\AllInPay;
use Vagh\LaravelAllInPay\Exceptions\HttpException;
use Vagh\LaravelAllInPay\Exceptions\InvalidArgumentException;
use Vagh\LaravelAllInPay\Exceptions\ServiceException;
$config = [
'app_id' => '00000051',
'cus_id' => '990440148166000',
'app_version' => '11',
'is_test' => true
];
$pay = new AllInPay($config);
// 测试统一下单(JS SDK)
try {
$params = [
'amount' => '10',
'out_trade_no' => 'CJXEWIOJOIDUXOUWOEICXNUWEO',
'open_id' => 'oTod4wA_AgM40UV2uQ9KJ-sgGmgU',
'notify_url' => 'http://test.com',
'app_id' => '748923478923'
];
$result = $pay->payJSApi($params);
var_dump($result);
} catch (Exception $e) {
if ($e instanceof InvalidArgumentException) {
$messgae = '参数异常:' . $e->getMessage();
}
if ($e instanceof HttpException) {
$messgae = '通信异常:' . $e->getMessage();
}
if ($e instanceof ServiceException) {
$messgae = '业务逻辑异常:' . $e->getMessage();
}
var_dump($messgae);
}
// 测试退款
try {
$params = [
'amount' => '10',
'order_history_id' => 'CJXEWIOJOIDUXOUWOEICXNUWEO',
'out_trade_no' => 'oTod4wA_AgM40UV2uQ9KJ-sgGmgU',
];
$result = $pay->refundPay($params);
var_dump($result);
} catch (Exception $e) {
if ($e instanceof InvalidArgumentException) {
$messgae = '参数异常:' . $e->getMessage();
}
if ($e instanceof HttpException) {
$messgae = '通信异常:' . $e->getMessage();
}
if ($e instanceof ServiceException) {
$messgae = '业务逻辑异常:' . $e->getMessage();
}
var_dump($messgae);
}