您好,登錄后才能下訂單哦!
本文章向大家介紹利用php怎么實現一個單筆轉賬到支付寶功能,主要包括利用php怎么實現一個單筆轉賬到支付寶功能的使用實例、應用技巧、基本知識點總結和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下。
php是一個嵌套的縮寫名稱,指的是英文超級文本預處理語言(php:Hypertext Preprocessor)的縮寫,它的語法混合了C、Java、Perl以及php自創新的語法,主要用來做網站開發,許多小型網站都用php開發,因為php是開源的,從而使得php經久不衰。
1.首先 去螞蟻金服簽約 單筆轉賬到支付寶
官方api文檔
2.需要的配置信息
1).應用appid
2).生成密鑰
文檔地址
根據文檔步驟生成
上傳這里的 應用公鑰
3.下載官方sdk 然后集成到自己項目
服務端SDK
官方實例
//實例化客戶端 AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"); //實例化具體API對應的request類,類名稱和接口名稱對應,當前調用接口名稱:alipay.open.public.template.message.industry.modify AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest(); //SDK已經封裝掉了公共參數,這里只需要傳入業務參數 //此次只是參數展示,未進行字符串轉義,實際情況下請轉義 request.setBizContent(" {" + " \"primary_industry_name\":\"IT科技/IT軟件與服務\"," + " \"primary_industry_code\":\"10001/20102\"," + " \"secondary_industry_code\":\"10001/20102\"," + " \"secondary_industry_name\":\"IT科技/IT軟件與服務\"" + " }"); AlipayOpenPublicTemplateMessageIndustryModifyResponse response = alipayClient.execute(request); //調用成功,則處理業務邏輯 if(response.isSuccess()){ //..... }
效果如下
我的代碼
<?php /** * create by 適可而止 * create time 2018/4/8 */ namespace Org\Util; class AlipayTransfer{ private $appId = 'appid'; private $rsaPrivateKey = '私鑰'; private $alipayrsaPublicKey = "支付寶公鑰"; private $payer_name = "xx科技"; private $aop; public function __construct() { $g_alipay = C('ALIPAY_CONFIG'); $this->appId = $g_alipay['APPID'];//appid $this->rsaPrivateKey = $g_alipay['rsaPrivateKey']; //私鑰 $this->alipayrsaPublicKey=$g_alipay['rsaPublicKey'];//支付寶公鑰 //引入單筆轉賬sdk Vendor('Alipayaop.AopSdk'); } public function init_aop_config() { $this->aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $this->aop->appId = $this->appId; $this->aop->rsaPrivateKey = $this->rsaPrivateKey; $this->aop->alipayrsaPublicKey=$this->alipayrsaPublicKey; $this->aop->apiVersion = '1.0'; $this->aop->signType = 'RSA2'; $this->aop->postCharset='UTF-8'; $this->aop->format='json'; } /** * 單筆轉賬接口 * @param $order_number 訂單號 * @param $pay_no 轉賬賬號 * @param $pay_name 轉賬用戶名 * @param $amount 轉賬金額 * @param $memo 備注 */ public function transfer($order_number,$pay_no,$pay_name,$amount,$memo) { //存入轉賬日志 $this->transferLog($order_number,$pay_no,$pay_name,$amount); $this->aop = new \AopClient (); //配置參數 $this->init_aop_config(); //導入請求 $request = new \AlipayFundTransToaccountTransferRequest (); $request->setBizContent("{" . "\"out_biz_no\":\"".$order_number."\"," .//商戶生成訂單號 "\"payee_type\":\"ALIPAY_LOGONID\"," .//收款方支付寶賬號類型 "\"payee_account\":\"".$pay_no."\"," .//收款方賬號 "\"amount\":\"".$amount."\"," .//總金額 "\"payer_show_name\":\"".$this->payer_name."\"," .//付款方賬戶 "\"payee_real_name\":\"".$pay_name."\"," .//收款方姓名 "\"remark\":\"".$memo."\"" .//轉賬備注 "}"); $result = $this->aop->execute ( $request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; $resultSubMsg = $result->$responseNode->sub_msg; //修改轉賬日志 $this->edit_transferLog($order_number,$resultCode,$resultSubMsg); if(!empty($resultCode)&&$resultCode == 10000){ return true; } else { return false; } } /** * 存取日志 */ private function transferLog($order_number,$pay_no,$pay_name,$amount) { $data['order_number'] = $order_number; $data['pay_no'] = $pay_no; $data['pay_name'] = $pay_name; $data['amount'] = $amount; $data['create_time'] = time(); M('AlipayTransferLog')->add($data); } /** * 修改日志 */ private function edit_transferLog($order_number,$result_code,$sub_msg) { $model = D("AlipayTransferLog"); $where['order_number'] = $order_number; $result = $model->where($where)->order('create_time desc')->find(); if ($result_code == 10000) { $result['status'] = 1; $sub_msg = 'success'; } else { $result['status'] = 2; } $result['memo'] = $sub_msg; $result['update_time'] = time(); M('AlipayTransferLog')->save($result); } /** * 查單接口 */ public function query($order_number) { $this->aop = new \AopClient (); //配置參數 $this->init_aop_config(); $request = new \AlipayFundTransOrderQueryRequest (); $request->setBizContent("{" . "\"out_biz_no\":\"".$order_number."\"" . " }"); $result = $this->aop->execute ( $request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if(!empty($resultCode)&&$resultCode == 10000){ $res_arr['code'] = '00'; $res_arr['data'] = $result; } else { $res_arr['code'] = '-1'; } return $res_arr; } } ?>
到此這篇關于利用php怎么實現一個單筆轉賬到支付寶功能的文章就介紹到這了,更多相關的內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。