2019-08-10
<h1>微信支付</h1>
<p>使用easywechat,统一下单</p>
<pre><code class="language-php">$payconfig = Di::getDefault()->get('config')->payconfig;
$param = [
'body' => $title, //标题
'out_trade_no' => $o_number, //订单编号
'total_fee' => $price, //总价(单位:分)
'notify_url' => empty($notifyUrl) ? $payconfig->notify_url : $notifyUrl, //回调地址
'trade_type' => $tradeType, //支付方式
'openid' => $openid, //支付用户openid
];
$config = [
'app_id' => $payconfig->pay_appid, //支付小程序appid
'mch_id' => $payconfig->pay_mchid, //商户号
'key' => $payconfig->pay_key, //密钥
];
$app = Factory::payment($config);
$unify = $app->order->unify($param);
if (!isset($unify['result_code']) || $unify['result_code'] != 'SUCCESS') {
$payLog->error(json_encode($unify, JSON_UNESCAPED_UNICODE));
return false;
}
$bridgeConfig = $app->jssdk->bridgeConfig($unify['prepay_id']);
return json_decode($bridgeConfig, true);</code></pre>
<p>回调处理</p>
<pre><code class="language-php">$config = [
'app_id' => $payconfig->pay_appid, //支付小程序appid
'mch_id' => $payconfig->pay_mchid, //商户号
'key' => $payconfig->pay_key, //密钥
];
$app = Factory::payment($config);
$response = $app->handlePaidNotify(function ($message, $fail) {
$payLog = Di::getDefault()->getShared('pay_logger');
try {
if (!isset($message['return_code']) || $message['return_code'] !== 'SUCCESS') {
throw new \Exception('支付回调信息有误(param)' . json_encode($message, JSON_UNESCAPED_UNICODE));
}
if ($message['result_code'] == 'SUCCESS') {
} elseif ($message['result_code'] == 'FAIL') {
}
}
} catch (\Exception $e) {
return $fail('通信失败,请稍后再通知我');
}
return true;
});
$response->send();
exit;</code></pre>