1. 程式人生 > >支付寶七(商戶會員卡之發放卡券)

支付寶七(商戶會員卡之發放卡券)

  支付寶會員卡發放 介面實現 部分功能

1. 發放會員卡

# 會員卡 開卡
    public function openCard(){
        $this->load->helper('url');
        $access_token = $this->session->userdata('access_token');
        #如果快取已存在 直接返回 之前的跳轉地址
        if(!$access_token){
            $this->alipay_auth();
        }
        $this->AopClient->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $this->AopClient->apiVersion = '1.0';
        $request = new AlipayMarketingCardOpenRequest();
        $post_data['out_serial_no'] = 'E201611250222479XXX';
        $post_data['card_template_id'] = '20161207000000000103117000300XXX';
        $post_data['card_user_info'] = array(
            'user_uni_id'=> '2088502948697XXX',
            'user_uni_id_type'=>'UID'
        );
        $card_no = 'ALICARD00002';
        $post_data['card_ext_info'] = array(
            'biz_card_no' => 'hha',#支付寶業務卡號 說明: 1、開卡成功後返回該引數,需要儲存留用; 2、開卡/更新/刪卡/查詢卡介面請求中不需要傳該引數;
            'external_card_no'=>$card_no, # 商戶外部會員卡卡號 說明: 1、會員卡開卡介面,如果卡型別為外部會員卡,請求中則必須提供該引數; 2、更新、查詢、刪除等介面,請求中則不需要提供該引數值;
            'open_date' => date('Y-m-d H:i:s'),
            'valid_date' => date('2022-m-d H:i:s'),
            'level' =>'VIP1',
            'point' => '88',
            'balance' => '128.88'
        );
        $post_data = json_encode($post_data,JSON_UNESCAPED_UNICODE);
        $request->setBizContent($post_data);
        $result = $this->AopClient->execute($request,$access_token);
        #echo json_encode($result,JSON_UNESCAPED_UNICODE);
        #echo $access_token;exit;
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失敗";
        }
    }

返回結果

{"code":"10000","msg":"Success","card_info":{"balance":"128.88","biz_card_no":"lsh0000000xxx","external_card_no":"ALICARD000xx","level":"VIP1","open_date":"2016-12-12 17:19:05","point":"88","valid_date":"2022-12-12 17:19:05"}}


2. 更新會員卡

#卡券 更新
    #  請注意 $post_data['target_card_no'] 和$post_data['card_info']['external_card_no'] 必須和原來開卡時一致 否則會報會員卡不存在
    public function updateCard(){
        $this->AopClient->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $this->AopClient->apiVersion = '1.0';
        $request = new AlipayMarketingCardUpdateRequest();
        $post_data['target_card_no'] = 'lsh000000057x';
        $post_data['target_card_no_type'] = 'BIZ_CARD';
        $post_data['occur_time'] = date('Y-m-d H:i:s');
        $card_no  = 'ALICARD00XXX';
        $post_data['card_info'] = array(
            'biz_card_no' => 'hha',#支付寶業務卡號 說明: 1、開卡成功後返回該引數,需要儲存留用; 2、開卡/更新/刪卡/查詢卡介面請求中不需要傳該引數;
            'external_card_no'=> $card_no, # 商戶外部會員卡卡號 說明: 1、會員卡開卡介面,如果卡型別為外部會員卡,請求中則必須提供該引數; 2、更新、查詢、刪除等介面,請求中則不需要提供該引數值;
            'open_date' => date('Y-m-d H:i:s'),
            'valid_date' => date('2022-m-d H:i:s'),
            'level' =>'VIP2',
            'point' => '188',
            'balance' => '1128.88'
        );
        $post_data = json_encode($post_data,JSON_UNESCAPED_UNICODE);
        $request->setBizContent($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 "失敗";
        }
    }

返回結果

3. 查詢會員卡

 #卡券 查詢
    public function queryCard(){
        $this->AopClient->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $this->AopClient->apiVersion = '1.0';
        $request = new AlipayMarketingCardQueryRequest();
        $post_data['target_card_no'] = 'lsh00000005XX';
        $post_data['target_card_no_type'] = 'BIZ_CARD';
        $post_data['card_user_info'] = array( #√ 可選引數
            'user_uni_id'=> '20885029486XXXX',
            'user_uni_id_type'=>'UID'
        );
        $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 "失敗";
        }
    }

返回結果

array(1) { [0]=> object(stdClass)#27 (2) { ["alipay_marketing_card_query_response"]=> object(stdClass)#28 (4) { ["code"]=> string(5) "10000" ["msg"]=> string(7) "Success" ["card_info"]=> object(stdClass)#29 (7) { ["balance"]=> string(7) "1128.88" ["biz_card_no"]=> string(13) "lsh0000000xxx" ["external_card_no"]=> string(12) "ALICARD000xx" ["level"]=> string(4) "VIP2" ["open_date"]=> string(19) "2016-12-13 14:41:38" ["point"]=> string(3) "188" ["valid_date"]=> string(19) "2022-12-13 14:41:38" } ["schema_url"]=> string(63) "alipays://platformapi/startapp?appId=20000021&b=m&p=10606450079" } ["sign"]=> string(172) "LMyk18n2vKskbPRLyLvaxq0LJiM9OV4coFsgyLs6EPfhmMT2kU92e+xC1sW3o44EYsbJdN6J6BVz3Z/eiBAOYKQ01y4CdYs6idLlepiGTvZjkldXzNfe5Rc2VUsqPctI3rNgcWlZCeNUWgnRoQaBnH1xnYq6g72u/fKpEOYaXO0=" } }


4. 刪除會員卡

#卡券 刪除
    public function deleteCard(){
        $this->AopClient->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $this->AopClient->apiVersion = '1.0';
        $request = new AlipayMarketingCardDeleteRequest();
        $post_data['out_serial_no'] = date('YmdHis').mt_rand(100,999); #商戶端刪卡業務流水號(商戶確保流水號唯一性)
        $post_data['target_card_no'] = 'lsh0000000XXX';
        $post_data['target_card_no_type'] = 'BIZ_CARD';
        $post_data['reason_code'] = 'USER_UNBUND'; # 刪卡原因 USER_UNBUND:使用者解綁(可以重新繫結) CANCEL:銷戶(完成銷戶後,就不能再重新繫結) PRESENT:轉贈(可以重新繫結)
        #$post_data['ext_info'] = ''; #√ 可選引數 目前支援如下key: new_card_no:新卡號 donee_user_id:受贈人userId 如 {"new_card_no":"12345"," donee_user_id":"20881021306XX"}
        $request->setBizContent(json_encode($post_data));
        $result = $this->AopClient->execute($request);
        var_dump(json_encode($result,JSON_UNESCAPED_UNICODE));exit;
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失敗";
        }
    }

返回結果
{"alipay_marketing_card_delete_response":{"code":"10000","msg":"Success"},"sign":"PK9mGX8uy3xfwRKygPxPCuf62YQU5JrdY4yDSn5dRhRcjtcTlzm8Gzv1yhnNXOhyITDzY6O8+rwPpSUqrQXT3VvS6dkMvEnoXsT5uKefRKX9PuvwbttN5Vc7Me57aPX9dReiBji4HgBVMI22gcugZ8mBTuNIecYNAAxiVcZjUFQ="}



      在開發商戶會員卡發放卡券的時候,一直報 “無效的令牌”,很是鬱悶,最後與支付寶相關技術定位問題,在於他們的文件未寫完全,授權獲取 access_token 時 scope 必須填寫 auth_ecard

詳見商戶會員卡快速接入: