**基于`ThinkPHP5.0`扫码支付DEMO** -- ### **控制器源代码(仅供参考)** --- **通常的支付流程是:** 一、根据商户订单号生成预支付码(区分支付类型) 二、根据支付类型来处理预支付码 * 1. JSAPI支付,需要创建JS签名包,然后在微信网页上发起支付 * 2. 扫码支付,直接将预支付码生成二维码就可以了 三、在生成预支付码时,需要指定接收支付通知URL地址,然后在**通知处理时再来处理订单是否已经完成支付**。 --- ``` namespace app\index\controller; use think\Controller; class Index extends Controller { /** * 模板本显示 * @return type */ public function index() { return view(); } /** * 支付通知处理 * @return type */ public function notify() { header('Content-Type:text/xml; charset=utf-8'); $postStr = file_get_contents("php://input"); $notifyInfo = (array) simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); if ($notifyInfo['result_code'] == 'SUCCESS' && $notifyInfo['return_code'] == 'SUCCESS') { # 记录支付通知信息 file_put_contents(LOG_PATH . 'pay_notify.log', var_export($notifyInfo, TRUE)); # 所有操作成功,返回正常状态 return xml(['return_code' => 'SUCCESS', 'return_msg' => 'SAVE DATA SUCCESS']); } } /** * 生成预支付码 * @return type */ public function qrc() { $outer_no = input('outer_no'); $price = input('price'); if (empty($outer_no) || empty($price)) { return ['code' => 'ERROR', 'info' => '参数错误!']; } $pay = & load_wechat('Pay'); $prepay_id = $pay->getPrepayId(null, '扫码测试', $outer_no, $price * 100, url('index/index/notify', null, null, TRUE), 'NATIVE'); if ($prepay_id === FALSE) { return ['code' => 'ERROR', 'info' => '创建预支付码失败,' . $pay->errMsg]; } return ['code' => 'SUCCESS', 'info' => '创建预支付码成功!', 'data' => $prepay_id]; } } ``` ### 支付成功会收到微信的通知,下面的内容就是代码中的` $notifyInfo`变量,根据自己的项目实际情况去更新订单信息。 --- ``` array ( 'appid' => 'wx3581ccf368729be3', 'bank_type' => 'CFT', 'cash_fee' => '1', 'fee_type' => 'CNY', 'is_subscribe' => 'Y', 'mch_id' => '1307539701', 'nonce_str' => 'api268huedasmkfgdjofzwitpuaqjl0c', 'openid' => 'o9j5kw-B8ZCp_FWLBmnSkrr1qHRE', 'out_trade_no' => '959947360', 'result_code' => 'SUCCESS', 'return_code' => 'SUCCESS', 'sign' => '43C91961D389D0A9FC480BC5B13592E6', 'time_end' => '20161011103317', 'total_fee' => '1', 'trade_type' => 'NATIVE', 'transaction_id' => '4008082001201610116370274372', ) ```