支付宝四(卡券的创建与核销)

    xiaoxiao2021-12-02  20

           支付宝的卡券使用,如果是与开发无关,那模板创建可以直接访问

    https://alipassprod.alipay.com/builder/myAlipassIndex.htm

    进行手动创建。当然今天我们要说的是,支付宝接口的卡券创建与使用。

    1. 添加卡券模板

    /* * @todo: 组装基础卡券信息 */ private function returnEvoucherInfo(){ $evoucherInfo = array(); $evoucherInfo["goodsId"] = '$goodsId$'; $evoucherInfo["title"] = '$discount_title$'; # 显示标题 $evoucherInfo["type"] = "coupon"; # 卡券类型 $evoucherInfo["product"] = "free"; # 产品类型 $evoucherInfo["startDate"] = '$startDate$'; # 有效期开始时间 $evoucherInfo["endDate"] = '$endDate$'; # 有效期结束时间 $evoucherInfo["operation"] = array( # 操作区域字段 轮播图区 array( "format"=> "qrcode", # qrcode img url wave 声波 text barcode "message"=> '$qrcode$', "messageEncoding"=> "utf-8", "altText"=> "核销码", ) ); $einfo = array(); $einfo['logoText'] ='$discount_title$'; # 优惠标题 $einfo['secondLogoText'] = "第二标题"; # 第二个 logo 文字 $einfo['headFields'] = array( # 头部区域字段 array( "key" => "discount_name", "label"=> "优惠内容:", "value"=> '$discount_name$', "type"=> "text" ) ); $einfo['primaryFields'] = array( array( "key" => "discount_name", "label"=> "首先区块:", "value"=> '$discount_name$', "type"=> "text" ) ); $einfo['secondaryFields'] = array( array( "key" => "discount_name", "label"=> "其次区块:", "value"=> '$discount_name$', "type"=> "text" ) ); $einfo['auxiliaryFields'] = array( array( "key" => "discount_desc", "label" => "优惠说明 :", "value" => '$discount_desc$', "type" => "text" ) ); # 这里是 详情 $einfo['backFields'] = array( array( "key"=> "description", "label"=> "详情描述", "value"=> "1.该优惠有效期:截止至2017年06月18日;\n2.凭此券可以享受以下优惠:\n享门市价优惠\n不与其他优惠同享。详询商家。", "type"=> "text" ), array( "key"=> "shops", "label"=> "可用门店", "value"=> "", "type"=> "text" ), array( "key"=> "disclaimer", "label"=> "负责声明", "value"=> "除特殊注明外,本优惠不能与其他优惠同时享受;本优惠最终解释权归商家所有,如有疑问请与商家联系。提示:为了使您得到更好的服务,请在进店时出示本券。", "type"=> "text" ) ); $evoucherInfo["einfo"] = $einfo; return $evoucherInfo; } private function addTemplateJson(){ $post_data = array(); $post_data['logo'] = '$logo$'; # logo 图片地址 $post_data['icon'] = '$icon$'; # icon 图片地址 放在卡券包的列表里 $post_data['strip'] = '$strip$'; # strip 图片地址 中间的banner 大图 $content = array(); $content['evoucherInfo'] = $this->returnEvoucherInfo(); $content['style'] = array("backgroundColor"=> '$backgroundColor$'); $content['fileInfo'] = array("formatVersion"=> "2","canShare"=> true,"canBuy"=> false,"canPresent"=> true, "serialNumber"=> '$serialNumber$',"supportTaxi"=> "true", "taxiSchemaUrl"=> "alipays://platformapi/startapp?appId=20000XXX&bizid=260&channel=713XX" ); $content['merchant'] = array("mname"=> "注册时的商户名称","mtel" => "","minfo"=> ""); $content['platform'] = array("channelID"=> "企业的appid或者partnerID", "webServiceUrl" => "https://alipass.alipay.com/builder/syncRecord.htm?tempId=".'$tempID$' ); $content['appInfo'] = array( "app"=> array("android_appid"=> "","ios_appid"=> "","android_launch"=> "","ios_launch"=> "", "android_download"=> "","ios_download"=> "" ), "label"=> "app名称", "message"=> "" ); $content['source'] = 'alipassprod'; # 卡券来源 $content['alipayVerify'] = array("qrcode"); # 核销方式 二维码 $post_data['content'] = $content; return $post_data; } /* * @todo: 第一步:创建卡券模板 */ public function addcardTemplete(){ $this->AopClient->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $this->AopClient->apiVersion = '1.0'; $request = new AlipayPassTemplateAddRequest (); $post_data['unique_id'] = date('YmdHis').mt_rand(1000,9999); $post_data['tpl_content'] = json_encode($this->addTemplateJson()); $request->setBizContent(json_encode($post_data)); $result = $this->AopClient->execute($request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if(!empty($resultCode)&&$resultCode == 10000){ echo "成功"; } else { echo "失败"; } }

    添加成功 返回基础信息

    { "alipay_pass_template_add_response": { "code": "10000", "msg": "Success", "result": "{\"errorCode\":\"KP.AE_SUCCESS\",\"errorMsg\":\"处理成功\", \"tpl_id\":\"2016111813234987057650XXX\",\"tpl_params\": [\"discount_title\",\"discount_name\",\"discount_desc\",\"startDate\",\"endDate\",\"qrcode\",\"serialNumber\",\"tempID\"]}", "success": true }

    2. 更新卡券模板

    /* * @todo: 第一点一步:更新卡券模板 */ public function updatecardTemplete(){ $this->AopClient->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $this->AopClient->apiVersion = '1.0'; $request = new AlipayPassTemplateUpdateRequest (); $post_data['tpl_id'] = '创建模板返回的tpl_id'; $post_data['tpl_content'] = json_encode($this->addTemplateJson()); $request->setBizContent(json_encode($post_data)); $result = $this->AopClient->execute($request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if(!empty($resultCode)&&$resultCode == 10000){ echo "成功"; } else { echo "失败"; } } 更新卡券模板返回基础信息

    { "alipay_pass_template_update_response": { "code": "10000", "msg": "Success", "result": "{\"errorCode\":\"KP.AE_SUCCESS\",\"errorMsg\":\"处理成功\",\"tpl_id\":\"2016111813234987057650XXX\",\"tpl_params\":[\"discount_title\",\"discount_name\",\"discount_desc\",\"startDate\",\"endDate\",\"qrcode\",\"serialNumber\",\"tempID\"]}", "success": true }, "sign": "I3XPfRLixEWou5iNHJQliqvKYpg" }

    3. 发送卡券

    /* * @todo: 第二步:发送卡券给消费客户 */ public function sendCard(){ $this->AopClient->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $this->AopClient->apiVersion = '1.0'; $request = new AlipayPassInstanceAddRequest(); $post_data['recognition_type'] = '1'; $post_data['tpl_id'] = '2016111813234987057650XXX'; $post_data['recognition_type'] = '1'; $post_data['recognition_info'] = array( 'partner_id' => '企业partnerID', 'out_trade_no' => '支付宝支付交易成功的外部订单号' #只支持 消费发放 ); $post_data['tpl_params'] = array( # 反正想变的变量都可以变 加上 $变量名$ 发券的时候直接发就好 "logo" => 'logo图片地址', "icon" => 'icon图片地址', "strip" => 'strip图片地址', "backgroundColor" => 'rgb(背景涂色)', "goodsId" => '商品ID', "startDate"=> "2016-11-10 20:00:00", #开始时间 "endDate"=> "2017-01-30 20:00:00", #结束时间 "discount_desc"=> "站是一个很牛逼的民营", #折扣描述 "discount_title"=> "凭此券即可打8.5折", # 折扣标题 "discount_name"=> "85折优惠券", # 折扣名称 "qrcode" => 'lsh'.date('YmdHis'), # 核销二维码参数 "serialNumber"=> date('YmdHis')."72663986010", # 每个人均不一样 的序列ID "tempID" => '2016111813234987057650XXX', #模板ID ); $request->setBizContent(json_encode($post_data)); $result = $this->AopClient->execute($request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if(!empty($resultCode)&&$resultCode == 10000){ echo "成功"; } else { echo "失败"; } } 发送卡券返回基础信息

    发送卡券 返回 { "alipay_pass_instance_add_response": { "code": "10000", "msg": "Success", "result": "{\"passId\":\"103487510XX\",\"operate\":\"ADD\",\"errorCode\":\"SUCCESS\",\"serialNumber\":\"2016111813XXX\",\"errorMsg\":\"处理成功\"}", "success": true }, "sign": "mFOD7QungT3uMnCNeTIoXJh" }

    注意serialNumber 是必须的参数 写死的话 会报错 KP.AE_ALIPASS_NOT_ALLOWED_ADD_TWICE 同一卡券不能添加两次

    4. 更新已发送的卡券

    /* * @todo: 第二点二步:更新已经发送给消费客户的卡券 */ public function updateCard(){ $this->AopClient->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $this->AopClient->apiVersion = '1.0'; $request = new AlipayPassInstanceUpdateRequest(); $post_data['serial_number'] = '上面sendCard生成的serialNumber'; $post_data['channel_id'] = '企业的Appid'; $post_data['tpl_params'] = array( # 这里同上 "startDate"=> "2016-11-10 20:00:00", "endDate"=> "2017-01-30 20:00:00", "discount_desc"=> "镇很牛逼的民营", "discount_title"=> "凭此券即可打8.5折", "discount_name"=> "95折优惠券", "qrcode" => 'lsh'.date('YmdHis'), "serialNumber"=> date('YmdHis'), "tempID" => '2016111813234987057650XXX' ); $request->setBizContent(json_encode($post_data)); $result = $this->AopClient->execute($request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if(!empty($resultCode)&&$resultCode == 10000){ echo "成功"; } else { echo "失败"; } }

    5. 核销卡券

    /* * @todo: 第三步:核销或关闭已经发送给消费客户的卡券 */ public function verifyCard(){ $this->AopClient->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $this->AopClient->apiVersion = '1.0'; $request = new AlipayPassInstanceUpdateRequest(); $post_data['serial_number'] = '上面sendCard生成的serialNumber'; $post_data['channel_id'] = '企业的Appid'; #status String 可选 99 券状态,支持更新为USED、CLOSED两种状态 $post_data['status'] = 'CLOSED'; $request->setBizContent(json_encode($post_data)); $result = $this->AopClient->execute($request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if(!empty($resultCode)&&$resultCode == 10000){ echo "成功"; } else { echo "失败"; } } 4 和 5 返回的基础信息 是同样的

    { "alipay_pass_instance_update_response": { "code": "10000", "msg": "Success", "result": "{\"operate\":\"UPDATE\",\"serialNumber\":\"2016111813XXX\"}", "success": true }, "sign": "Y4Mi1xPaDCWOo6M9VWGn0YHt" } 【附录】

     支付宝卡券详细介绍 地址

     https://doc.open.alipay.com/doc2/detail?treeId=199&docType=1&articleId=105249#s2

     支付宝卡券API地址

     https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.S5sNQG&docType=4&apiId=714

    转载请注明原文地址: https://ju.6miu.com/read-679815.html

    最新回复(0)