f75da78403c9ae512c6f4f92a0f9ca902c1004b2.svn-base 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

require_once( REST_API_TO_MINIPROGRAM_PLUGIN_DIR . 'includes/wxpay/WxPay.Api.php' );
require_once( REST_API_TO_MINIPROGRAM_PLUGIN_DIR . 'includes/wxpay/WxPay.JsApiPay.php' );
require_once( REST_API_TO_MINIPROGRAM_PLUGIN_DIR . 'includes/wxpay/WxPay.Notify.php' );


class RAW_REST_Payment_Controller  extends WP_REST_Controller{

    public function __construct() {
        $this->namespace     = 'watch-life-net/v1';
        $this->resource_name = 'payment';
    }


     // Register our routes.
    public function register_routes() {

        register_rest_route( $this->namespace, '/' . $this->resource_name, array(
            // Here we register the readable endpoint for collections.
            array(
                'methods'   => 'POST',
                'callback'  => array( $this, 'post_payment' ),
               'permission_callback' => array( $this, 'post_payment_permissions_check' ),
                'args'               => array(            
                    'openid' => array(
                        'required' => true
                    ),
                    'totalfee' => array(
                        'required' => true
                    )
                   
                )
            ),
            // Register our schema callback.
            'schema' => array( $this, 'post_public_item_schema' ),
        ) );

        register_rest_route( $this->namespace, '/' . $this->resource_name . '/notify', array(
            array(
                'methods'             => 'GET',
                'callback'            => array( $this, 'notify' ),
                'permission_callback' => array( $this, 'get_notify_permissions_check' ),
                'args'                => $this->get_collection_params()
            )
        ) );

    }

    public function  post_payment($request){
        
        date_default_timezone_set('Asia/Shanghai');
        $openId=isset($request['openid'])?$request['openid']:'';        
        $totalFee=isset($request['totalfee'])? $request['totalfee']:1;

        if(!is_numeric($totalFee))
        {

            return new WP_Error( 'error', "totalfee参数错误", array( 'status' => 400 ) );
        }
        
        
        $appId=RAM_WxPayConfig::get_appid();
        $mchId=RAM_WxPayConfig::get_mchid();
        $key=RAM_WxPayConfig::get_key();
        $body=RAM_WxPayConfig::get_body();

        if(empty($appId) || empty($mchId) || empty($key) || empty($body)) {
            
            return new WP_Error( 'error', "请填写AppID、商户号、商户支付密钥和支付描述", array( 'status' => 400 ) );
        }

        $tools = new RAM_JsApiPay();

        //②、统一下单
        $input = new RAM_WxPayUnifiedOrder();
        $input->SetBody($body);
        $orderId =RAM_WxPayConfig::get_mchid().date("YmdHis");
        $input->SetOut_trade_no($orderId);
        $input->SetTotal_fee(strval($totalFee*100));
        //$input->SetTotal_fee(strval($totalFee));
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 6000));
        $input->SetNotify_url(get_rest_url( null, $this->namespace . '/' . $this->resource_name . '/notify' ) );
        $input->SetTrade_type( 'JSAPI' );
        $input->SetOpenid($openId);

         $order = RAM_WxPayApi::unifiedOrder($input);

        $jsApiParameters = $tools->GetJsApiParameters($order);

        $jsApiParameters['success'] = 'success';
        return  $jsApiParameters;

    }

    // 支付通知
    public function notify( $request ) {        
        
        $notify = new RAW_PayNotifyCallBack();
        $notify->Handle( false );
    }
    public function post_payment_permissions_check($request) {
        $openId =isset($request['openid'])? $request['openid']:"";       
        if(empty($openId) || !username_exists($openId))
        {
            return new WP_Error( 'user_parameter_error', "用户参数错误", array( 'status' => 400 ) );
        }
        $totalFee=isset($request['totalfee'])? (int)$request['totalfee']:1;
        if(!is_int($totalFee))
        {
            return new WP_Error( 'error', 'totalfee参数错误', array( 'status' => 400 ) );
        }
        return true;
    }

    /**
     * Check whether a given request has permission to read order notes.
     *
     * @param  WP_REST_Request $request Full details about the request.
     * @return WP_Error|boolean
     */
    public function get_notify_permissions_check( $request ) {
        return true;
    }


}  

class RAW_PayNotifyCallBack extends RAM_WxPayNotify {
    
    // 重写回调处理函数
    public function NotifyProcess( $data, &$msg ) {
        
        if( ! array_key_exists( 'transaction_id' , $data ) ) {
            $msg = '输入参数不正确';
            return false;
        }
        if(!RAW_Util::check_notify_sign($data,get_option('raw_paykey'))){
            $msg = 'key错误';
            return false;
        }
        
        
        return true;
    }
}