小程序微信支付

wx.requestPayment(
{
'timeStamp': e.data.timeStamp.toString(),
'nonceStr': e.data.nonceStr.toString(),
'package': e.data.package,
'signType': e.data.signType,
'paySign': e.data.sign,
'success': function (res) {

//create order
wx.request({
url: o.d.ceshiUrl + "/leokim/create_order",
method: "post",
data: {
openid: openid,
id: goods_info.goods_id,
type: goods_info.type,
code: goods_info.code
},
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
'success': function (e) {
var pages = getCurrentPages();
var currPage = pages[1];

currPage.setData({
is_buy: 1
});
},
'fail': function (res) { }
})

wx.showToast({
image: '/images/success.png',
title: "购买成功.",
duration: 2e3
});

},
'fail': function (res) {
var msg = '购买失败';
if (res.errMsg == 'requestPayment:fail cancel'){
msg = '取消支付';
};

wx.showToast({
image: '/images/failed.png',
title: msg,
});
},
'complete': function (res) {
console.log('支付完成');
}
})

后端:

function wechat_pay()
    {
        $openid = 'ocLsr5OXtYZZe6dUrMLJxsm0CTEk';
        $key = 'a81bfe3a84df3ceb5cc54bd3e1605ba2';

        $post = $this->input->post();
        $goods_name = $post['goods_name'];

        //get prepay_id
        $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
        $data = array();
        $data['appid'] = $this->appid;
        $data['mch_id'] = $this->mch_id;
        $data['nonce_str'] = mt_rand(10000000,99999999);
        $data['body'] = $goods_name;
        $data['out_trade_no'] = 'wkt' . time(). mt_rand(10000000,99999999);
//        $data['total_fee'] = $price * 100;
        $data['total_fee'] = 1;
        $data['spbill_create_ip'] = '114.96.242.184';
        $data['notify_url'] = 'http://www.weixin.qq.com/wxpay/pay.php';
        $data['trade_type'] = 'JSAPI';
        $data['openid'] = $openid;
        $data['sign'] = $this->createSign($data,$key);
        $data = $this->array_to_xml($data);
        $result = $this->curlPost($url, $data);
        $result = $this->xml_to_array($result);
        $prepay_id = $result['prepay_id'];


        $return_data = array(
            'appId' => $this->appid,
            'timeStamp'=>time(),
            'nonceStr'=>mt_rand(10000000,99999999),
            'package'=>"prepay_id=$prepay_id",
            'signType'=>'MD5'
        );
        $return_data['sign'] = $this->createSign($return_data,$key);

        echo json_encode($return_data);
    }

    public function create_order(){
        $result = array('flag'=>'N');
        $post = $this->input->post();
        $type = $post['type'];
        $id = $post['id'];
        $openid = $post['openid'];
        $code = $post['code'];
        $user_id = $this->get_user_id_by_open_id($openid);
        $goods_table_info = $this->get_table_info_by_type($type);
        $table = $goods_table_info['table'];
        $key = $goods_table_info['key'];

        $sql = "INSERT INTO `t_aci_bought` (`bought_id`, `type`, `related_id`, `price`, `status`, `user_id`, `add_time`)
                SELECT NULL, '$type', $id, price, 0, $user_id, NOW() FROM `$table` WHERE `$key`= $id";
        $this->db->query($sql);

        $insert_id = $this->db->insert_id();
        if($insert_id > 0){
            $result['flag'] = 'Y';

            //TODO 三级分销
        }

        echo json_encode($result);
    }