PHP支付宝支付

// 调用支付
public function doPay(Request $request)
{
    $payment_type = $this->request->param('type');
    $order_source_type = 1; // 来源
    $order_source_type_config = config('config.order_source_type');
    if (!$order_source_type || !$order_source_type_config[$order_source_type]) {
        return $this->apiReturn(ERROR, [], '参数错误');
    }
    if (!$payment_type || !config('config.PAY_TYPE')[$order_source_type_config[$order_source_type]][$payment_type]) {
        return $this->apiReturn(ERROR, [], '支付方式错误');
    }
    $order_id = $this->request->param('order_id');
    if (!$order_id) {
        return $this->apiReturn(ERROR, [], '参数错误');
    }
    $where = [
        ['id','=',$order_id],
    ];
    $info = Db::name('order')->where($where)->field('id,order_no,real_money,total_num,title,pay_status,order_status')->find();
    $order_data['source'] = $order_source_type;
    $order_data['order_no'] = createOrderNo();
    if (empty($info)) {
        return $this->apiReturn(ERROR, [], '订单信息不存在');
    }
    if ($info['order_status'] != 1) {
        return $this->apiReturn(ERROR, [], '订单已支付');
    }
    if ($info['pay_status'] == 1) {
        return $this->apiReturn(ERROR, [], '订单已支付');
    }
    $order_data['payment_type'] = 'ali_pay';
    $order_data['trade_type'] = 'pc';
    Db::name('order')->where('id', $order_id)->update($order_data);

    // 配置
    aliFactory::setOptions($this->getOptions());
    $result = aliFactory::payment()->faceToFace()->preCreate($info['title'], "2234567890", "99.00");
    if (!empty($result->code) && $result->code == 10000) {
        $result = json_decode($result->httpBody, true)['alipay_trade_precreate_response'];
        return $this->apiReturn(SUCCESS, ['qrcode_url' => $result['qr_code']], 'ok');
    } else {
        return $this->apiReturn(ERROR, [], '支付失败');
    }
}
/**
 * 支付宝支付配置
 * @return Config
 */
public function getOptions()
{
    $options = new Config();
    $options->protocol = 'https';
    $options->gatewayHost = 'openapi.alipay.com';
    $options->signType = 'RSA2';

    $config = config('config.payment.ali_pay');
    $options->appId = $config['app_id'];

    // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
    $options->merchantPrivateKey = $config['merchant_private_key'];

    //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
    $options->alipayPublicKey = $config['alipay_public_key'];

    //可设置异步通知接收服务地址(可选)
    $options->notifyUrl = $config['notify_url'];

    //可设置AES密钥,调用AES加解密相关接口时需要(可选)
    //$options->encryptKey = "<-- 请填写您的AES密钥,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";

    return $options;
}
/**
 * 支付宝异步通知
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\DbException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * @throws \think\exception\PDOException
 */
public function ali_notify()
{
    $params = $_POST;

    $order = Db::name('order')->where('order_no', $params['out_trade_no'])->find();

    if (!$order) {
        echo "fail";
        exit;
    }

    $pay = new AliPcPay(config('config.ali_pay'));
    $res = $pay->payNotify($params, $order['order_no'], $order['transaction_id'], $order['real_money']);
    if (!$res) {
        echo "fail";
        exit;
    } else {
        $code = 0;
        try {
            $update['status'] = 2;
            $update['update_time'] = time();
            $res = Db::name('order')->where("id", $order['id'])->update($update);
            if (!$res) {
                throw new Exception("更新订单信息失败");
            }

            $code = 1;
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }

        if ($code) {    // 支付成功
            echo "success";
            exit;
        } else {
            echo "fail";
            exit;
        }
    }
}

/**
 * 支付宝同步支付
 * @throws Exception
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\DbException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * @throws \think\exception\PDOException
 */
public function ali_return()
{
    $request = $this->request;
    require_once Env::get('root_path') . 'extend/' . 'payment/aliPay/sdk/pagepay/service/AlipayTradeService.php';
    $arr = [
        'charset' => $request->param('charset'),
        'out_trade_no' => $request->param('out_trade_no'),
        'method' => $request->param('method'),
        'total_amount' => $request->param('total_amount'),
        'sign' => $request->param('sign'),
        'trade_no' => $request->param('trade_no'),
        'auth_app_id' => $request->param('auth_app_id'),
        'version' => $request->param('version'),
        'app_id' => $request->param('app_id'),
        'sign_type' => $request->param('sign_type'),
        'seller_id' => $request->param('seller_id'),
        'timestamp' => $request->param('timestamp'),
    ];
    $ali_pay_config = config('config.ali_pay');
    $alipaySevice = new \AlipayTradeService($ali_pay_config);
    $result = $alipaySevice->check($arr);
    $jump_url = $this->request->domain() . '/pcmxp/paysuccess.html';
    if ($result) {//验证成功 签名
        if (htmlspecialchars($arr['app_id']) != $ali_pay_config['app_id']) {
            $this->redirect($jump_url . '?msg=参数错误');
        }
        //商户订单号
        $out_trade_no = htmlspecialchars($arr['out_trade_no']);
        $order = Db::name('order')->where("order_no", $out_trade_no)->field('real_money,uid,id,paytime')
            ->find();
        if (!$order) {
            $this->redirect($jump_url . '?msg=订单号不存在');
        }

        //支付宝交易号
        $trade_no = htmlspecialchars($arr['trade_no']);

        if ($arr['total_amount'] != $order['real_money']) {
            $this->redirect($jump_url . '?msg=支付金额错误');
        }

        // 修改订单信息
        $order_data = [
            'transaction_id' => $trade_no,
            'update_time' => time(),
        ];
        $res = Db::name('order')->where('id', $order['id'])->update($order_data);
        if (!$res) {
            $this->redirect($jump_url . 'msg=支付失败');
        } else {
            $this->redirect($jump_url . '?msg=支付成功');
        }
    } else {
        $this->redirect($jump_url . '?msg=验证失败');
    }
}

标签

发表评论