应用场景:
上次介绍的是,对接连连第三方支付,详情请参考《连连支付第三方对接》。使用连连支付,由于第一次去支付,需要进行绑卡操作,这样会导致用户体验不好。因此,需要在个人中心增加绑卡功能。
实现:
// 匿名用户绑定银行卡public function postBindCard(){$user = $this->user(); // 获取当前登录用户$param = $this->request->post();// 获取表单提交参数// 表单验证if (!$this->validate->scene('bindCard')->check($param)) {return $this->renderError($this->validate->getError());}// 验证银行卡if ($this->checkBankCard($param['acctno']) != 'true') {return $this->renderError('银行卡参数有误!');}// 查询该卡是否已经绑定过$card_info = Db::name('user_card')->where(['user_id' => $user['user_id'],'acctno' => $param['acctno']])->value("acctno");if (!empty($card_info)) {return $this->renderError('您已绑定过该银行卡!');}$llPay = new LianLianPay();$result = $llPay->bindCard($param,$user);if ($result['status'] == 'succeeded') {// 绑定成功return $this->renderSuccess($result);}return $this->renderError($result['msg']);}// 匿名用户绑卡验证public function bindMyCardVerify(){$user = $this->user();// 获取当前登录用户$param = $this->request->post();// 表单验证if (!$this->validate->scene('bindCardVerify')->check($param)) {return $this->renderError($this->validate->getError());}$llPay = new LianLianPay();$result = $llPay->bindCardVerify($param,$user);if ($result['status'] == 'succeeded') {// 绑定成功return $this->renderSuccess($result);}return $this->renderError($result['msg']);}/*** 检查银行卡号是否正确* @param string $cardNumber* @return bool*/public function checkBankCard($card_number){$arr_no = str_split($card_number);$last_n = $arr_no[count($arr_no)-1];krsort($arr_no);$i = 1;$total = 0;foreach ($arr_no as $n){if($i%2==0){$ix = $n*2;if($ix>=10){$nx = 1 + ($ix % 10);$total += $nx;}else{$total += $ix;}}else{$total += $n;}$i++;}$total -= $last_n;$x = 10 - ($total % 10);if($x == $last_n){return 'true';}else{return 'false';}}
LianLianPay.php
<?php
class LianLianPay {// 01 用户绑定银行卡public function bindCard($param,$user){include_once VENDOR_PATH . '/lianlianpay/LLianPayClient.php';$time = date('YmdHis',time());$seqno = 'YHBK'.$time;$notify_url = 'https://test.com/notice.php';$parameter = new \stdClass();$parameter->timestamp = $time;$parameter->oid_partner = $this->payee_id; // 商户号$parameter->user_id = $user['user_id'];$parameter->txn_seqno = $seqno;$parameter->txn_time = $time;$parameter->notify_url = $notify_url;// 以下几个参数需要加密处理$parameter->linked_acctno = LLianPayAccpSignature::publicKeyEncrypt($param['acctno']);$parameter->linked_acctname = LLianPayAccpSignature::publicKeyEncrypt($param['acctname']);$parameter->linked_phone = LLianPayAccpSignature::publicKeyEncrypt($param['phone']);$parameter->id_type = "ID_CARD";$parameter->id_no = LLianPayAccpSignature::publicKeyEncrypt($addressInfo['ID_number']);$client = new LLianPayClient();$gateway_url = 'https://accpapi.lianlianpay.com/v1/acctmgr/bindcard-apply';$result = $client->sendRequest($gateway_url,json_encode($parameter));if($result['ret_code'] == '0000'){$result['status'] = 'succeeded';}else{$result = ['status'=>'failed','msg'=>$result['ret_msg']];}return $result;}// 02 用户绑定银行卡验证public function bindCardVerify($param,$user){include_once VENDOR_PATH . '/lianlianpay/LLianPayClient.php';$time = date('YmdHis',time());$notify_url = 'https://test.com/notice.php';$parameter = new \stdClass();$parameter->timestamp = $time;$parameter->oid_partner = $this->payee_id; // 商户号$parameter->user_id = $user['user_id'];$parameter->txn_seqno = $param['txn_seqno'];$parameter->token = $param['token'];$parameter->verify_code = $param['verify_code']; // 验证码$client = new LLianPayClient();$gateway_url = 'https://accpapi.lianlianpay.com/v1/acctmgr/bindcard-verify';$result = $client->sendRequest($gateway_url,json_encode($parameter));if($result['ret_code'] == '0000'){$result['status'] = 'succeeded';}else{$result = ['status'=>'failed','msg'=>$result['ret_msg']];}return $result;}
}