123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- namespace huolib\withdraw\driver;
- use huolib\constant\SettleConst;
- use huolib\withdraw\Driver;
- use think\Loader;
- class Alipay extends Driver {
- private $config
- = [
- 'partner' => '',
- 'seller_email' => '',
- 'key' => '',
- 'sign_type' => '',
- 'input_charset' => 'utf-8',
- 'transport' => 'http',
- 'cacert' => '',
- 'notify_url' => '',
- 'return_url' => '',
- 'show_url' => '',
- 'private_key_path' => '',
- 'public_key_path' => '',
- ];
-
- public function __construct() {
- if (file_exists(GLOBAL_CONF_PATH."extra/pay/zfb/config.php")) {
- $_config = include GLOBAL_CONF_PATH."extra/pay/zfb/config.php";
- } else {
- $_config = array();
- }
- $this->config = array_merge($this->config, $_config);
- $this->config['sign_type'] = strtoupper(trim('RSA'));
- $this->config['cacert'] = GLOBAL_CONF_PATH.'extra/pay/zfb/cacert.pem';
- $this->config['private_key_path'] = GLOBAL_CONF_PATH.'extra/pay/zfb/key/rsa_private_key.pem';
- $this->config['public_key_path'] = GLOBAL_CONF_PATH.'extra/pay/zfb/key/rsa_public_key.pem';
- $this->notify_url = get_val($this->config, 'notify_url', '');
- }
-
- public function withDraw() {
- $_re_data = [];
- Loader::import('withdraw.alipay.aop.AopClient', EXTEND_PATH, '.php');
- Loader::import('withdraw.alipay.aop.request.AlipayFundTransToaccountTransferRequest', EXTEND_PATH, '.php');
- Loader::import('withdraw.alipay.aop.SignData', EXTEND_PATH, '.php');
- $_gateway_url = 'https://openapi.alipay.com/gateway.do';
- $_aop = new \AopClient();
- $_aop->gatewayUrl = $_gateway_url;
- $_aop->appId = $this->config['app_id'];
- $_aop->rsaPrivateKey = $this->getPrivateKey($this->config['private_key_path']);
- $_aop->alipayrsaPublicKey = $this->getPublicKey($this->config['public_key_path']);
- $_aop->apiVersion = '1.0';
- $_aop->signType = 'RSA';
- $_aop->postCharset = 'utf-8';
- $_aop->format = 'json';
- $_request = new \AlipayFundTransToaccountTransferRequest();
- $_request->setBizContent(
- "{".
- "\"out_biz_no\":\"$this->order_id\",".
- "\"payee_type\":\"ALIPAY_LOGONID\",".
- "\"payee_account\":\"$this->payee_account\",".
- "\"amount\":\"$this->real_amount\",".
- "\"payer_show_name\":\"$this->payer_show_name\",".
- "\"payee_real_name\":\"$this->payee_real_name\",".
- "\"remark\":\"$this->remark\"".
- "}"
- );
- $_result = $_aop->execute($_request);
- $_response_node = str_replace(".", "_", $_request->getApiMethodName())."_response";
- $_resultCode = $_result->$_response_node->code;
- $_re_data['result'] = json_encode($_result);
- $_re_data['code'] = $_resultCode;
- $_re_data['msg'] = $_result->$_response_node->msg;
- if (!empty($_resultCode) && $_resultCode == 10000) {
- $_re_data['code'] = SettleConst::SETTLE_SUCCESS;
-
- $_rs = $this->orderQuery('s1530961465310009494');
- $_re_data['query_result'] = $_rs['result'];
- } else {
- $_re_data['msg'] = $_result->$_response_node->sub_msg;
- }
- return $_re_data;
- }
-
- public function orderQuery($order_id, $ext = null) {
- $_re_data = [];
- Loader::import('withdraw.alipay.aop.AopClient', EXTEND_PATH, '.php');
- Loader::import('withdraw.alipay.aop.request.AlipayFundTransOrderQueryRequest', EXTEND_PATH, '.php');
- Loader::import('withdraw.alipay.aop.SignData', EXTEND_PATH, '.php');
- $_gateway_url = 'https://openapi.alipay.com/gateway.do';
- $_aop = new \AopClient();
- $_aop->gatewayUrl = $_gateway_url;
- $_aop->appId = $this->config['app_id'];
- $_aop->format = 'json';
- $_aop->postCharset = 'utf-8';
- $_aop->signType = 'RSA';
- $_aop->apiVersion = '1.0';
- $_aop->rsaPrivateKey = $this->getPrivateKey($this->config['private_key_path']);
- $_aop->alipayrsaPublicKey = $this->getPublicKey($this->config['public_key_path']);
- $_request = new \AlipayFundTransOrderQueryRequest();
- $_request->setBizContent(
- "{".
- "\"out_biz_no\":\"$order_id\",".
- "}"
- );
- $_result = $_aop->execute($_request);
- $_response_node = str_replace(".", "_", $_request->getApiMethodName())."_response";
- $_resultCode = $_result->$_response_node->code;
- $_re_data['result'] = json_encode($_result);
- $_re_data['code'] = $_result->$_response_node->code;
- $_re_data['msg'] = $_result->$_response_node->msg;
- if (!empty($_resultCode) && $_resultCode == 10000) {
- switch ($_result->$_response_node->status) {
- case 'SUCCESS':
- $_re_data['status'] = SettleConst::SETTLE_PAY_SUCCESS;
- break;
- case 'INIT':
- case 'DEALING':
- $_re_data['status'] = SettleConst::SETTLE_PAY_PROCESSING;
- break;
- default:
- $_re_data['status'] = SettleConst::SETTLE_PAY_FAILED;
- }
- } else {
- $_re_data['msg'] = $_result->$_response_node->sub_msg;
- }
- return $_re_data;
- }
-
- public function getPrivateKey($private_key_path) {
- if (!file_exists($private_key_path)) {
- return null;
- }
- $_private_key = file_get_contents($private_key_path);
- $_private_key = str_replace("-----BEGIN RSA PRIVATE KEY-----", "", $_private_key);
- $_private_key = str_replace("-----END RSA PRIVATE KEY-----", "", $_private_key);
- $_private_key = str_replace("\n", "", $_private_key);
- $private_key = wordwrap($_private_key, 64, "\n", true);
- return $private_key;
- }
-
- function getPublicKey($public_key_path) {
- if (!file_exists($public_key_path)) {
- return null;
- }
- $_public_key = file_get_contents($public_key_path);
-
- $_public_key = str_replace("-----BEGIN PUBLIC KEY-----", "", $_public_key);
- $_public_key = str_replace("-----END PUBLIC KEY-----", "", $_public_key);
- $_public_key = str_replace("\n", "", $_public_key);
- $_public_key = wordwrap($_public_key, 64, "\n", true);
- return $_public_key;
- }
- }
|