PayController.class.php 3.39 KB
<?php
/**
 * 支付订单
 * User: tangxingguo
 * Date: 2018/3/6
 * Time: 2018年3月5日10:27:31
 */

namespace Apicp\Controller\Pay;

use Com\PackageValidate;
use VcySDK\CyPay;
use VcySDK\Service;

class PayController extends AbstractController
{
    /**
     * 支付渠道:微信
     */
    const PAY_CHANNEL_WXPAY = 1;

    /**
     * 支付渠道:支付宝
     */
    const PAY_CHANNEL_ALIPAY = 2;

    /**
     * 支付状态:待支付
     */
    const PAY_STATUS_WAIT_PAY = 1;


    /**
     * pay
     * @author tangxingguo
     * @desc 支付接口
     * @param String order_id:true 订单ID
     * @param Int channel:true 渠道(微信支付=1;支付宝支付=2)
     * @param String callback_url 支付宝支付完成后跳转前端的地址
     * @return array
                array(
                    'channel' => 1, // 支付渠道(微信支付=1;支付宝支付=2)
                    'qrcode' => "weixin://wxpay/bizpayurl?pr=q8OWw8h", // 微信支付时存在,支付链接(前端需生成二维码)
                    'url' => 'https://openapi.alipay.com/',// 支付宝支付时存在,请求地址
                    'biz_content' => array(// 支付宝支付时存在,将biz_content通过post方式提交到上面的url即可跳转到阿里付款页面;<br>提交数据实例:biz_content={"out_trade_no":"201838135645225","total_amount":"0.01","subject":"畅移在线支付","body":"","timeout_express":"3046m","product_code":"FAST_INSTANT_TRADE_PAY"};<br>支付demo地址:http://t-rst.vchangyi.com/demo/alipay.html
                        'out_trade_no' => 'LC2018030821535253800246',
                        'total_amount' => 0.01,
                        'subject' => '畅移在线支付',
                        'body' => '',
                        'timeout_express' => '4947m',
                        'product_code' => 'FAST_INSTANT_TRADE_PAY',
                    ),
                    'status' => 1, // 订单状态(1=待支付;2=已支付;3=已取消;4=已关闭)
                );
     */
    public function Index_post()
    {
        // 请求数据
        $postData = I('post.');

        // 验证规则
        $rules = [
            'order_id' => 'require',
            'channel' => 'require|in:1,2',
            'callback_url' => 'min:1',
        ];

        // 验证请求数据
        $validate = new PackageValidate();
        $validate->postData = $postData;
        $validate->validateParams($rules);

        $channel = $postData['channel'];
        $orderId = $postData['order_id'];

        // 支付
        $payData = [
            'orderId' => $orderId,
            'channel' => $channel
        ];

        // 支付宝支付,需要支付完成后回跳前端的地址
        if ($channel == self::PAY_CHANNEL_ALIPAY) {
            if (empty($postData['callback_url'])) {
                E('_ERR_PAY_CALLBACK_URL_EMPTY');
            }
            $payData['callBackUrl'] = $postData['callback_url'];
        }

        $sdk = new CyPay(Service::instance());
        $res = $sdk->pay($payData);

        // 支付宝支付,处理参数
        if ($channel == self::PAY_CHANNEL_ALIPAY &&
            isset($res['status']) && $res['status'] == self::PAY_STATUS_WAIT_PAY && isset($res['url'])) {
            $res['biz_content'] = json_decode($res['bizContent'], true);
            unset($res['bizContent']);
        }

        $this->_result = $res;
    }
}