微信企业号-卡券

/卡劵服务start**********************/
/

* 上传卡劵LOGO
* @param string $access_token
* @param array $file $_FILES数组中的单个元素
* @return string \(url 商户logo_url */ public function upload_card_logo(\)access_token, array $file){
log_write('开始上传卡劵LOGO');

	try {
		$url = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" . $access_token . "&type=card_logo";
		$curldata = array($file);
		$data = g('http') -> curl($url, $curldata);
		
		$data['httpcode'] != 0 && log_write('上传卡卷LOGO失败:重试 1') && $data = g('http') -> curl($url, $curldata);
		$data['httpcode'] != 0 && log_write('上传卡卷LOGO失败:重试 2') && $data = g('http') -> curl($url, $curldata);
		
		if ($data['httpcode'] == 0) {
			$tem_data = json_decode($data['content'], true);
			if($tem_data['errcode'] == 0 && $tem_data['errmsg'] == 'ok'){
				log_write('上传卡劵成功: ' . $file['name']);
				return $tem_data['url'];
			} else {
				log_write('上传卡劵失败: ' . $file['name']);
				throw new SCException($tem_data['errmsg']);
			}
			
		} else {
			throw new SCException($data['httpcode']);
			
		}
	} catch(SCException $e) {
		throw new SCException($e);
	}
}

/**
* 创建卡劵并返回卡劵ID
* @param string $access_token
* @param array $data 卡卷信息
* @return string $card_id 卡券ID
*/
public function get_card_id($access_token, $data){
	log_write('开始创建卡劵');

	try {
		$url = "https://qyapi.weixin.qq.com/cgi-bin/card/create?access_token=" . $access_token;
		$datacurl = array();
		switch($data['card_type']){
			case '1':{
				//优惠劵
				$datacurl['card'] = array(
					'card_type' => "GENERAL_COUPON",
					"general_coupon" => array(
						"base_info" => array(
							"logo_url" => $data['logo_url'], 
							"brand_name" => $data['brand_name'], 
							"code_type" => $data['code_type'], 
							"title" => $data['title'], 
							"color" => $data['color'], 
							"notice" => $data['notice'], 
							"service_phone" => $data['service_phone'], 
							"description" => $data['description'], 
							"date_info" => array(
								"type" => $data['type'],
								"begin_timestamp" => $data['begin_timestamp'], 
								"end_timestamp" => $data['end_timestamp']
							), 
							"sku" => array(
								'quantity' => $data['quantity']
							),
							"get_limit" => $data['get_limit'],
							"bind_openid" => $data['bind_openid'],
							"can_share" => $data['can_share'],
							"can_give_friend" => $data['can_give_friend'],
							"custom_url_name" => $data['custom_url_name'],
							"custom_url" => $data['custom_url'],
							"custom_url_sub_title" => $data['custom_url_sub_title'],
							"promotion_url_name" => $data['promotion_url_name'],
							"promotion_url" => $data['promotion_url'],
							"source" => $data['source']
						),
						'default_detail' => $data['default_detail']
					),
				);
				break;
			}
			case '2':{
				//团购券
				$datacurl['card'] = array(
					'card_type' => "GROUPON",
					"groupon" => array(
						"base_info" => array(
							"logo_url" => $data['logo_url'], 
							"brand_name" => $data['brand_name'], 
							"code_type" => $data['code_type'], 
							"title" => $data['title'], 
							"color" => $data['color'], 
							"notice" => $data['notice'], 
							"service_phone" => $data['service_phone'], 
							"description" => $data['description'], 
							"date_info" => array(
								"type" => $data['type'],
								"begin_timestamp" => $data['begin_timestamp'], 
								"end_timestamp" => $data['end_timestamp']
							), 
							"sku" => array(
								'quantity' => $data['quantity']
							),
							"get_limit" => $data['get_limit'],
							"bind_openid" => $data['bind_openid'],
							"can_share" => $data['can_share'],
							"can_give_friend" => $data['can_give_friend'],
							"custom_url_name" => $data['custom_url_name'],
							"custom_url" => $data['custom_url'],
							"custom_url_sub_title" => $data['custom_url_sub_title'],
							"promotion_url_name" => $data['promotion_url_name'],
							"promotion_url" => $data['promotion_url'],
							"source" => $data['source']
						),
						'deal_detail' => $data['deal_detail']
					),
				);
				break;
			}
			case '3':{
				//代金券
				$datacurl['card'] = array(
					'card_type' => "CASH",
					"cash" => array(
						"base_info" => array(
							"logo_url" => $data['logo_url'], 
							"brand_name" => $data['brand_name'], 
							"code_type" => $data['code_type'], 
							"title" => $data['title'], 
							"color" => $data['color'], 
							"notice" => $data['notice'], 
							"service_phone" => $data['service_phone'], 
							"description" => $data['description'], 
							"date_info" => array(
								"type" => $data['type'],
								"begin_timestamp" => $data['begin_timestamp'], 
								"end_timestamp" => $data['end_timestamp']
							), 
							"sku" => array(
								'quantity' => $data['quantity']
							),
							"get_limit" => $data['get_limit'],
							"bind_openid" => $data['bind_openid'],
							"can_share" => $data['can_share'],
							"can_give_friend" => $data['can_give_friend'],
							"custom_url_name" => $data['custom_url_name'],
							"custom_url" => $data['custom_url'],
							"custom_url_sub_title" => $data['custom_url_sub_title'],
							"promotion_url_name" => $data['promotion_url_name'],
							"promotion_url" => $data['promotion_url'],
							"source" => $data['source']
						),
						'least_cost' => $data['least_cost'],
						'reduce_cost' => $data['reduce_cost'],
					),
				);
			break;
			}
			case '4':{
				//折扣券
				$datacurl['card'] = array(
					'card_type' => "DISCOUNT",
					"discount" => array(
						"base_info" => array(
							"logo_url" => $data['logo_url'], 
							"brand_name" => $data['brand_name'], 
							"code_type" => $data['code_type'], 
							"title" => $data['title'], 
							"color" => $data['color'], 
							"notice" => $data['notice'], 
							"service_phone" => $data['service_phone'], 
							"description" => $data['description'], 
							"date_info" => array(
								"type" => $data['type'],
								"begin_timestamp" => $data['begin_timestamp'], 
								"end_timestamp" => $data['end_timestamp']
							), 
							"sku" => array(
								'quantity' => $data['quantity']
							),
							"get_limit" => $data['get_limit'],
							"bind_openid" => $data['bind_openid'],
							"can_share" => $data['can_share'],
							"can_give_friend" => $data['can_give_friend'],
							"custom_url_name" => $data['custom_url_name'],
							"custom_url" => $data['custom_url'],
							"custom_url_sub_title" => $data['custom_url_sub_title'],
							"promotion_url_name" => $data['promotion_url_name'],
							"promotion_url" => $data['promotion_url'],
							"source" => $data['source']
						),
						'discount' => $data['discount'],
					),
				);
			break;
			}
			case '5':{
				//礼品券
				$datacurl['card'] = array(
					'card_type' => "GIFT",
					"gift" => array(
						"base_info" => array(
							"logo_url" => $data['logo_url'], 
							"brand_name" => $data['brand_name'], 
							"code_type" => $data['code_type'], 
							"title" => $data['title'], 
							"color" => $data['color'], 
							"notice" => $data['notice'], 
							"service_phone" => $data['service_phone'], 
							"description" => $data['description'], 
							"date_info" => array(
								"type" => $data['type'],
								"begin_timestamp" => $data['begin_timestamp'], 
								"end_timestamp" => $data['end_timestamp']
							), 
							"sku" => array(
								'quantity' => $data['quantity']
							),
							"get_limit" => $data['get_limit'],
							"bind_openid" => $data['bind_openid'],
							"can_share" => $data['can_share'],
							"can_give_friend" => $data['can_give_friend'],
							"custom_url_name" => $data['custom_url_name'],
							"custom_url" => $data['custom_url'],
							"custom_url_sub_title" => $data['custom_url_sub_title'],
							"promotion_url_name" => $data['promotion_url_name'],
							"promotion_url" => $data['promotion_url'],
							"source" => $data['source']
						),
						'gift' => $data['gift'],
					),
				);
			break;
			}
		}
		$curldata = json_encode($datacurl);
		$res = g('http') -> curl($url, $curldata);
		
		$res['httpcode'] != 0 && log_write('创建卡劵失败:重试 1') && $res = g('http') -> curl($url, $curldata);
		$res['httpcode'] != 0 && log_write('创建卡劵失败:重试 2') && $res = g('http') -> curl($url, $curldata);
		
		if ($res['httpcode'] == 0) {
			$tem_res = json_decode($res['content'], true);
			if($tem_res['errcode'] == 0 && $tem_res['errmsg'] == 'ok'){
				log_write('创建卡劵成功: ' . $data['title']);
				return $tem_res['card_id'];
			} else {
				log_write('创建卡劵失败: ' . $data['title']);
				throw new SCException($tem_res['errmsg']);
			}
			
		} else {
			throw new SCException($res['httpcode']);
		}
	} catch(SCException $e) {
		throw new SCException($e);
	}
}

public function create_card()
{	
}

/**
* 获取卡券详情
* @param string $access_token
* @param string $card_id
* @return array 卡券详情
*/
public function get_card_detail($access_token, $card_id){
	log_write('开始获取卡券详情');

	try {
		$url = "https://qyapi.weixin.qq.com/cgi-bin/card/get?access_token=" . $access_token;
		$curldata = array('card_id' => $card_id);
		$data = g('http') -> curl($url, $curldata);
		
		$data['httpcode'] != 0 && log_write('获取卡券详情失败:重试 1') && $data = g('http') -> curl($url, $curldata);
		$data['httpcode'] != 0 && log_write('获取卡券详情失败:重试 2') && $data = g('http') -> curl($url, $curldata);
		
		if ($data['httpcode'] == 0) {
			$tem_data = json_decode($data['content'], true);
			if($tem_data['errcode'] == 0 && $tem_data['errmsg'] == 'ok'){
				log_write('获取卡券详情成功: ' . $card_id);
				return $tem_data['card'];
				
			} else {
				log_write('获取卡券详情失败: ' . $card_id);
				throw new SCException($tem_data['errmsg']);
			}

		} else {
			throw new SCException($data['httpcode']);
		}

	} catch(SCException $e) {
		throw new SCException($e);
	}
}

/**
* 获取卡券摘要列表
* @param string $access_token
* @param string $offset 查询卡列表的起始偏移量,从0开始,即offset: 5是指从从列表里的第六个开始读取。
* @param string $count 需要查询的卡片的数量(数量最大50)。
* @param string $status  支持开发者拉出指定状态的卡券列表。若不填默认拉取所有状态
* @return array 卡券摘要列表
*/
public function get_card_list($access_token, $offset=0, $count=20, $status = ''){
	log_write('开始获取卡券摘要列表');

	try {
		$url = "https://qyapi.weixin.qq.com/cgi-bin/card/batchget?access_token=" . $access_token;
		$curldata = array('offset' => $offset, 'count' => $count, 'status' => $status);
		$data = g('http') -> curl($url, $curldata);
		
		$data['httpcode'] != 0 && log_write('获取卡券摘要列表失败:重试 1') && $data = g('http') -> curl($url, $curldata);
		$data['httpcode'] != 0 && log_write('获取卡券摘要列表失败:重试 2') && $data = g('http') -> curl($url, $curldata);
		
		if ($data['httpcode'] == 0) {
			$tem_data = json_decode($data['content'], true);
			if($tem_data['errcode'] == 0 && $tem_data['errmsg'] == 'ok'){
				log_write('获取卡券摘要列表成功: 总共' . $tem_data['total_num']);
				return array('card_digest_list' => $tem_data['card_digest_list'], 'total_num' => $tem_data['total_num']) ;
				
			} else {
				log_write('获取卡券摘要列表失败: ' . $card_id);
				throw new SCException($tem_data['errmsg']);
			}

		} else {
			throw new SCException($data['httpcode']);
		}

	} catch(SCException $e) {
		throw new SCException($e);
	}
}

/**
* 修改卡劵库存
* @param string $access_token
* @param string $increase_stock_value 增加多少库存,支持不填或者填0
* @param string $reduce_stock_value 减少多少库存,支持不填或者填0(increase_stock_value跟reduce_stock_value必须填其中一个)
* @return boolean
*/
public function edit_card_stock($access_token, $increase_stock_value=0, $reduce_stock_value=0){
	log_write('开始修改卡劵库存');

	try {
		$url = "https://qyapi.weixin.qq.com/cgi-bin/card/modifystock?access_token=" . $access_token;
		$curldata = array('increase_stock_value' => $increase_stock_value, 'reduce_stock_value' => $reduce_stock_value);
		$data = g('http') -> curl($url, $curldata);
		
		$data['httpcode'] != 0 && log_write('修改卡劵库存失败:重试 1') && $data = g('http') -> curl($url, $curldata);
		$data['httpcode'] != 0 && log_write('修改卡劵库存失败:重试 2') && $data = g('http') -> curl($url, $curldata);
		
		if ($data['httpcode'] == 0) {
			$tem_data = json_decode($data['content'], true);
			if($tem_data['errcode'] == 0 && $tem_data['errmsg'] == 'ok'){
				log_write('修改卡劵库存成功');
				return true;
				
			} else {
				log_write('修改卡劵库存失败');
				throw new SCException($tem_data['errmsg']);
			}

		} else {
			throw new SCException($data['httpcode']);
		}

	} catch(SCException $e) {
		throw new SCException($e);
	}
}

/**
* 删除卡劵
* @param string $access_token
* @param string $card_id 	卡券id
* @return boolean
*/
public function delete_card($access_token, $card_id){
	log_write('开始删除卡劵');

	try {
		$url = "https://qyapi.weixin.qq.com/cgi-bin/card/delete?access_token=" . $access_token;
		$curldata = array('card_id' => $card_id);
		$data = g('http') -> curl($url, $curldata);
		
		$data['httpcode'] != 0 && log_write('删除卡劵失败:重试 1') && $data = g('http') -> curl($url, $curldata);
		$data['httpcode'] != 0 && log_write('删除卡劵失败:重试 2') && $data = g('http') -> curl($url, $curldata);
		
		if ($data['httpcode'] == 0) {
			$tem_data = json_decode($data['content'], true);
			if($tem_data['errcode'] == 0 && $tem_data['errmsg'] == 'ok'){
				log_write('删除卡劵成功');
				return true;
				
			} else {
				log_write('删除卡劵失败: ' . $card_id);
				throw new SCException($tem_data['errmsg']);
			}

		} else {
			throw new SCException($data['httpcode']);
		}

	} catch(SCException $e) {
		throw new SCException($e);
	}
}

/**
* 推送卡劵信息
* @param string $access_token
* @param int $agentid 	企业应用的id,整型
* @param string $card_id 卡券ID
* @param string $touser 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
* @param string $toparty 	部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
* @param string $totag 标签ID列表,多个接收者用‘|’分隔。当touser为@all时忽略本参数
* @return boolean
*/
public function push_card_message($access_token, $agentid, $card_id, $touser='', $toparty='', $totag=''){
	log_write('开始推送卡劵信息');

	try {
		$url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" . $access_token;
		$curldata = array(
			'touser' => $touser,
			'toparty' => $toparty,
			'totag' => $totag,
			'msgtype' => 'card',
			'agentid' => $agentid,
			'card' => array('card_id' => $card_id),
			);
		$data = g('http') -> curl($url, $curldata);
		
		$data['httpcode'] != 0 && log_write('推送卡劵信息失败:重试 1') && $data = g('http') -> curl($url, $curldata);
		$data['httpcode'] != 0 && log_write('推送卡劵信息失败:重试 2') && $data = g('http') -> curl($url, $curldata);
		
		if ($data['httpcode'] == 0) {
			$tem_data = json_decode($data['content'], true);
			if($tem_data['errcode'] == 0 && $tem_data['errmsg'] == 'ok'){
				log_write('推送卡劵信息成功');
				return true;
				
			} else {
				log_write('推送卡劵信息失败: ' . $card_id);
				throw new SCException($tem_data['errmsg']);
			}

		} else {
			throw new SCException($data['httpcode']);
		}

	} catch(SCException $e) {
		throw new SCException($e);
	}
}

/**
* 获取卡券图文消息内容
* @param string $access_token
* @param int $agentid 	企业应用的id,整型
* @param string $card_id 卡券ID
* @return string content 将返回代内容填入到图文消息中content字段,即可将卡券嵌入图文消息发送
*/
public function get_card_mpnews($access_token, $agentid, $card_id){
	log_write('开始获取卡劵图文信息内容');

	try {
		$url = "https://qyapi.weixin.qq.com/cgi-bin/card/mpnews/gethtml?access_token=" . $access_token;
		$curldata = array(
			'agentid' => $agentid,
			'card_id' => $card_id,
			);
		$data = g('http') -> curl($url, $curldata);
		
		$data['httpcode'] != 0 && log_write('获取卡劵图文信息内容失败:重试 1') && $data = g('http') -> curl($url, $curldata);
		$data['httpcode'] != 0 && log_write('获取卡劵图文信息内容失败:重试 2') && $data = g('http') -> curl($url, $curldata);
		
		if ($data['httpcode'] == 0) {
			$tem_data = json_decode($data['content'], true);
			if($tem_data['errcode'] == 0 && $tem_data['errmsg'] == 'ok'){
				log_write('获取卡劵图文信息内容成功');
				return $tem_data['content'];
				
			} else {
				log_write('获取卡劵图文信息内容失败: ' . $card_id);
				throw new SCException($tem_data['errmsg']);
			}

		} else {
			throw new SCException($data['httpcode']);
		}

	} catch(SCException $e) {
		throw new SCException($e);
	}
}

/**
* 创建卡劵二维码
* @param string $access_token
* @param string $card_id 	卡券ID
* @param string $expire_seconds 指定二维码的有效时间,范围是60 ~ 1800秒。不填默认为永久有效。
* @param string $openid 指定领取者的openid,只有该用户能领取。bind_openid字段为true的卡券必须填写,非指定openid不必填写。
* @param string $is_unique_code 指定下发二维码,生成的二维码随机分配一个code,领取后不可再次扫描。填写true或false。默认false。
* @param string $outer_id 	领取场景值,用于领取渠道的数据统计,默认值为0,字段类型为整型,用户领取卡券后触发的事件推送中会带上此自定义场景值。
* @return string ticket 可以通过https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET获取二维码
* @return string expire_seconds 二维码的有效时间
* @return string url 	二维码图片解析后的地址,开发者可根据该地址自行生成需要的二维码图片
* @return string show_qrcode_url 二维码显示地址,点击后跳转二维码页面
*/
public function create_card_qrcode($access_token, $card_id, $expire_seconds='', $openid='', $is_unique_code=false, $outer_id=0){
	log_write('开始创建卡劵二维码');

	try {
		$url = "https://qyapi.weixin.qq.com/cgi-bin/card/qrcode/create?access_token=" . $access_token;
		$curldata = array(
			'action_name' => 'QR_CARD',
			'expire_seconds' => $expire_seconds,
			'action_info' => array(
				'card' => array(
					'card_id' => $card_id,
					'openid' => $openid,
					'is_unique_code' => $is_unique_code,
					'outer_id' => $outer_id
				)
			)
			);
		$data = g('http') -> curl($url, $curldata);
		
		$data['httpcode'] != 0 && log_write('创建卡劵二维码失败:重试 1') && $data = g('http') -> curl($url, $curldata);
		$data['httpcode'] != 0 && log_write('创建卡劵二维码失败:重试 2') && $data = g('http') -> curl($url, $curldata);
		
		if ($data['httpcode'] == 0) {
			$tem_data = json_decode($data['content'], true);
			if($tem_data['errcode'] == 0 && $tem_data['errmsg'] == 'ok'){
				log_write('创建卡劵二维码成功');
				return array(
					'ticket' => $tem_data['ticket'], 
					'expire_seconds' => $tem_data['expire_seconds'], 
					'url' => $tem_data['url'],
					'show_qrcode_url' => $tem_data['show_qrcode_url']
					);
				
			} else {
				log_write('创建卡劵二维码失败: ' . $card_id);
				throw new SCException($tem_data['errmsg']);
			}

		} else {
			throw new SCException($data['httpcode']);
		}

	} catch(SCException $e) {
		throw new SCException($e);
	}
}

/**
* 查询code
* @param string $access_token
* @param string $code 卡券code
* @return string openid 	用户openid
* @return string card 	卡劵信息
*/
public function get_code_info($access_token, $code){
	log_write('开始查询code');

	try {
		$url = "https://qyapi.weixin.qq.com/cgi-bin/card/code/get?access_token=" . $access_token;
		$curldata = array(
			'code' => $code
			);
		$data = g('http') -> curl($url, $curldata);
		
		$data['httpcode'] != 0 && log_write('查询code失败:重试 1') && $data = g('http') -> curl($url, $curldata);
		$data['httpcode'] != 0 && log_write('查询code失败:重试 2') && $data = g('http') -> curl($url, $curldata);
		
		if ($data['httpcode'] == 0) {
			$tem_data = json_decode($data['content'], true);
			if($tem_data['errcode'] == 0 && $tem_data['errmsg'] == 'ok'){
				log_write('查询code成功');
				return array(
					'openid' => $tem_data['openid'], 
					'card' => $tem_data['card']
					);
				
			} else {
				log_write('查询code失败: ' . $card_id);
				throw new SCException($tem_data['errmsg']);
			}

		} else {
			throw new SCException($data['httpcode']);
		}

	} catch(SCException $e) {
		throw new SCException($e);
	}
}

/**
* 核销code
* @param string $code 卡券code
* @return string openid 	用户openid
* @return string card 	卡劵信息
*/
public function consume_code($access_token, $code){
	log_write('开始核销code');

	try {
		$url = "https://qyapi.weixin.qq.com/cgi-bin/card/code/consume?access_token=" . $access_token;
		$curldata = array(
			'code' => $code
			);
		$data = g('http') -> curl($url, $curldata);
		
		$data['httpcode'] != 0 && log_write('核销code失败:重试 1') && $data = g('http') -> curl($url, $curldata);
		$data['httpcode'] != 0 && log_write('核销code失败:重试 2') && $data = g('http') -> curl($url, $curldata);
		
		if ($data['httpcode'] == 0) {
			$tem_data = json_decode($data['content'], true);
			if($tem_data['errcode'] == 0 && $tem_data['errmsg'] == 'ok'){
				log_write('核销code成功');
				return array(
					'openid' => $tem_data['openid'], 
					'card' => $tem_data['card']
					);
				
			} else {
				log_write('核销code失败: ' . $card_id);
				throw new SCException($tem_data['errmsg']);
			}

		} else {
			throw new SCException($data['httpcode']);
		}

	} catch(SCException $e) {
		throw new SCException($e);
	}
}

/卡劵服务end************************/

posted on 2017-02-02 00:12  果然朝辉  阅读(407)  评论(0编辑  收藏  举报

导航