<?php
require dirname(__DIR__) . '/common.php';
use Yurun\Util\YurunHttp;
$GLOBALS['PAY_CONFIG'] = [
'appid' => '',
'mch_id' => '',
'key' => '',
'pay_notify_url' => 'http://yurun.test.com/test/Weixin/pay_notify.php',
'certPath' => __DIR__ . '/cert/apiclient_cert.pem',
'keyPath' => __DIR__ . '/cert/apiclient_key.pem',
];
YurunHttp::setDefaultHandler('Yurun\Util\YurunHttp\Handler\Swoole');
class weixinNative extends \Yurun\PaySDK\Weixin\Notify\Pay
{
protected function __exec()
{
file_put_contents(__DIR__ . '/notify_result.txt', date('Y-m-d H:i:s') . ':' . var_export($this->data, true));
$this->reply(true, 'OK');
}
}
$server = new swoole_http_server('0.0.0.0', 80);
$server->on('request', function ($request, $response) {
switch ($request->server['request_uri'])
{
case '/unifiedorder':
unifiedorder($request, $response);
break;
case '/pay_notify':
payNotify($request, $response);
break;
default:
$response->end('404');
break;
}
});
$server->start();
function unifiedorder($request, $response)
{
$params = new \Yurun\PaySDK\Weixin\Params\PublicParams();
$params->appID = $GLOBALS['PAY_CONFIG']['appid'];
$params->mch_id = $GLOBALS['PAY_CONFIG']['mch_id'];
$params->key = $GLOBALS['PAY_CONFIG']['key'];
$pay = new \Yurun\PaySDK\Weixin\SDK($params);
$pay->swooleRequest = $request;
$pay->swooleResponse = $response;
$request = new \Yurun\PaySDK\Weixin\Native\Params\Pay\Request();
$request->body = 'test'; $request->out_trade_no = 'test' . mt_rand(10000000, 99999999); $request->total_fee = 1; $request->spbill_create_ip = '127.0.0.1'; $request->notify_url = $GLOBALS['PAY_CONFIG']['pay_notify_url'];
$result = $pay->execute($request);
$shortUrl = $result['code_url'];
$response->end(json_encode([
'message' => 'weixin qr scan url:',
'url' => $shortUrl,
]));
}
function payNotify($request, $response)
{
$params = new \Yurun\PaySDK\Weixin\Params\PublicParams();
$params->appID = $GLOBALS['PAY_CONFIG']['appid'];
$params->mch_id = $GLOBALS['PAY_CONFIG']['mch_id'];
$params->key = $GLOBALS['PAY_CONFIG']['key'];
$sdk = new \Yurun\PaySDK\Weixin\SDK($params);
$payNotify = new PayNotify();
$payNotify->swooleRequest = $request;
$payNotify->swooleResponse = $response;
try
{
$sdk->notify($payNotify);
}
catch (Exception $e)
{
file_put_contents(__DIR__ . '/notify_result.txt', $e->getMessage() . ':' . var_export($payNotify->data, true));
}
}