123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- class alipay {
- private $config;
- public function __construct($payment_info = array(), $order_info = array()) {
- if (!empty($payment_info)) {
- if(!isset($payment_info['payment_config']['alipay_appid'])){
- throw new \think\Exception('请配置支付宝支付', 10006);
- }
- $this->config = array(
-
- 'app_id' => $payment_info['payment_config']['alipay_appid'],
-
- 'merchant_private_key' => $payment_info['payment_config']['private_key'],
-
- 'notify_url' => str_replace('/index.php', '', HOME_SITE_URL) . '/payment/alipay_notify.html',
-
- 'return_url' => str_replace('/index.php', '', HOME_SITE_URL) . "/payment/alipay_return.html",
-
- 'charset' => "UTF-8",
-
- 'sign_type' => "RSA2",
-
- 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
-
- 'app_cert_path' => $payment_info['payment_config']['app_cert_path'],
- 'alipay_cert_path' => $payment_info['payment_config']['alipay_cert_path'],
- 'root_cert_path' => $payment_info['payment_config']['root_cert_path'],
- );
- }
- }
-
- public function get_payform($order_info) {
- require_once dirname(__FILE__) . '/aop/AopCertClient.php';
- require_once dirname(__FILE__) . '/aop/request/AlipayTradePagePayRequest.php';
- $aop = new AopCertClient ();
- $appCertPath = $this->config['app_cert_path'];
- $alipayCertPath = $this->config['alipay_cert_path'];
- $rootCertPath = $this->config['root_cert_path'];
- $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
- $aop->appId = $this->config['app_id'];
- $aop->rsaPrivateKey = $this->config['merchant_private_key'];
- $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA2';
- $aop->postCharset = 'utf-8';
- $aop->format = 'json';
- $aop->isCheckAlipayPublicCert = true;
- $aop->appCertSN = $aop->getCertSN($appCertPath);
- $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);
- $request = new AlipayTradePagePayRequest ();
- $request->setNotifyUrl($this->config['notify_url']);
- $request->setReturnUrl($this->config['return_url']);
- $request->setBizContent("{" .
- "\"out_trade_no\":\"" . $order_info['order_type'] . '-' . $order_info['pay_sn'] . "\"," .
- "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"," .
- "\"total_amount\":" . round($order_info['api_pay_amount'],2) . "," .
- "\"subject\":\"" . $order_info['subject'] . "\"," .
- "\"body\":\"" . $order_info['order_type'] . "\"" .
- " }");
- $result = $aop->pageExecute($request);
- echo $result;exit;
- }
- public function return_verify() {
- require_once dirname(__FILE__) . '/aop/AopCertClient.php';
- $arr = $_GET;
- $return_result = array(
- 'trade_status' => '0',
- );
- $temp = explode('-', input('param.out_trade_no'));
- $out_trade_no = $temp['1'];
- $order_type = $temp['0'];
- $aop = new AopCertClient ();
- $appCertPath = $this->config['app_cert_path'];
- $alipayCertPath = $this->config['alipay_cert_path'];
- $rootCertPath = $this->config['root_cert_path'];
- $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
- $aop->appId = $this->config['app_id'];
- $aop->rsaPrivateKey = $this->config['merchant_private_key'];
- $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA2';
- $aop->postCharset = 'utf-8';
- $aop->format = 'json';
- $aop->isCheckAlipayPublicCert = true;
- $aop->appCertSN = $aop->getCertSN($appCertPath);
- $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);
- $result = $aop->rsaCheckV1($arr, $aop->alipayrsaPublicKey, $aop->signType);
- if ($result) {
- $return_result = array(
- 'out_trade_no' => $out_trade_no,
- 'trade_no' => input('param.trade_no'),
- 'total_fee' => input('param.total_amount'),
- 'order_type' => $order_type,
- 'trade_status' => '1',
- );
- }
- return $return_result;
- }
- public function verify_notify() {
- require_once dirname(__FILE__) . '/aop/AopCertClient.php';
- $arr = $_POST;
- $notify_result = array(
- 'trade_status' => '0',
- );
- $aop = new AopCertClient ();
- $appCertPath = $this->config['app_cert_path'];
- $alipayCertPath = $this->config['alipay_cert_path'];
- $rootCertPath = $this->config['root_cert_path'];
- $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
- $aop->appId = $this->config['app_id'];
- $aop->rsaPrivateKey = $this->config['merchant_private_key'];
- $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA2';
- $aop->postCharset = 'utf-8';
- $aop->format = 'json';
- $aop->isCheckAlipayPublicCert = true;
- $aop->appCertSN = $aop->getCertSN($appCertPath);
- $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);
- $result = $aop->rsaCheckV1($arr, $aop->alipayrsaPublicKey, $aop->signType);
- if ($result) {
- if ($arr['trade_status'] == 'TRADE_SUCCESS') {
- $out_trade_no = explode('-', input('param.out_trade_no'));
- $out_trade_no = $out_trade_no['1'];
- $notify_result = array(
- 'out_trade_no' => $out_trade_no,
- 'trade_no' => input('param.trade_no'),
- 'total_fee' => input('param.total_amount'),
- 'order_type' => input('param.body'),
- 'trade_status' => '1',
- );
- }
- }
- return $notify_result;
- }
-
- public function trade_refund($order_info, $refund_amount) {
- require_once dirname(__FILE__) . '/aop/AopCertClient.php';
- require_once dirname(__FILE__) . '/aop/request/AlipayTradeRefundRequest.php';
- $aop = new AopCertClient ();
- $appCertPath = $this->config['app_cert_path'];
- $alipayCertPath = $this->config['alipay_cert_path'];
- $rootCertPath = $this->config['root_cert_path'];
- $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
- $aop->appId = $this->config['app_id'];
- $aop->rsaPrivateKey = $this->config['merchant_private_key'];
- $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA2';
- $aop->postCharset = 'utf-8';
- $aop->format = 'json';
- $aop->isCheckAlipayPublicCert = true;
- $aop->appCertSN = $aop->getCertSN($appCertPath);
- $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);
- $request = new AlipayTradeRefundRequest ();
- $request->setBizContent("{" .
- "\"out_request_no\":\"".$order_info['out_request_no']."\"," .
- "\"trade_no\":\"".$order_info['trade_no']."\"," .
- "\"refund_amount\":".$refund_amount."," .
- "\"refund_reason\":\"".'订单退款'."\"" .
- " }");
- $result = $aop->execute($request);
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- $resultCode = $result->$responseNode->code;
- if (!empty($resultCode) && $resultCode == 10000) {
- return ds_callback(TRUE, $result->$responseNode->msg);
- } else {
- return ds_callback(FALSE, $result->$responseNode->sub_msg);
- }
- }
-
- public function fund_transfer($withdraw_info) {
- require_once dirname(__FILE__) . '/aop/AopCertClient.php';
- require_once dirname(__FILE__) . '/aop/request/AlipayFundTransUniTransferRequest.php';
-
- $aop = new AopCertClient ();
- $appCertPath = $this->config['app_cert_path'];
- $alipayCertPath = $this->config['alipay_cert_path'];
- $rootCertPath = $this->config['root_cert_path'];
- $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
- $aop->appId = $this->config['app_id'];
- $aop->rsaPrivateKey = $this->config['merchant_private_key'];
- $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA2';
- $aop->postCharset = 'utf-8';
- $aop->format = 'json';
- $aop->isCheckAlipayPublicCert = true;
- $aop->appCertSN = $aop->getCertSN($appCertPath);
- $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);
- $request = new AlipayFundTransUniTransferRequest ();
- $request->setBizContent("{" .
- "\"out_biz_no\":\"" . $withdraw_info['pdc_sn'] . "\"," .
- "\"trans_amount\":" . $withdraw_info['pdc_amount'] . "," .
- "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," .
- "\"biz_scene\":\"DIRECT_TRANSFER\"," .
- "\"order_title\":\"" . config('ds_config.site_name') . "账户提现\"," .
- "\"original_order_id\":\"\"," .
- "\"payee_info\":{" .
- "\"identity\":\"" . $withdraw_info['pdc_bank_no'] . "\"," .
- "\"identity_type\":\"ALIPAY_LOGON_ID\"," .
- "\"name\":\"" . $withdraw_info['pdc_bank_user'] . "\"" .
- " }," .
- "\"remark\":\"\"" .
- " }");
- $result = $aop->execute($request);
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- $resultCode = $result->$responseNode->code;
- if (!empty($resultCode) && $resultCode == 10000) {
- return ds_callback(TRUE, $result->$responseNode->msg,array('pdc_trade_sn'=>$result->$responseNode->order_id));
- } else {
- return ds_callback(FALSE, $result->$responseNode->sub_msg);
- }
- }
- }
|