123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- /**
- * 支付宝立减金接口封装
- */
- namespace pay;
- if (!defined('ALIPAY_PATHEX')) define('ALIPAY_PATHEX', BASE_DATA_PATH . '/api/aop');
- require_once(ALIPAY_PATHEX . '/AopClient.php');
- require_once(ALIPAY_PATHEX . '/AopEncrypt.php');
- require_once(ALIPAY_PATHEX . '/request/AlipayUserDtbankcustChannelvoucherSendRequest.php');
- require_once(ALIPAY_PATHEX . '/request/AlipayUserDtbankcustActivityorderQueryRequest.php');
- require_once(ALIPAY_PATHEX . '/request/AlipayUserDtbankcustChannelvoucherconfigQueryRequest.php');
- require_once(ALIPAY_PATHEX . '/request/AlipayUserDtbankActivitybillQueryRequest.php');
- require_once(ALIPAY_PATHEX . '/request/AlipayUserDtbankcustAccountQueryRequest.php');
- use aop\AopClient;
- use aop\AlipayUserDtbankcustChannelvoucherSendRequest;
- use aop\AlipayUserDtbankcustActivityorderQueryRequest;
- use aop\AlipayUserDtbankActivitybillQueryRequest;
- use aop\AlipayUserDtbankcustAccountQueryRequest;
- use Exception;
- class AlipayCoupon {
- const ALIPAY_SERVER_URL = "https://openapi.alipay.com";
- const ALIPAY_GATEWAY = "/gateway.do";
- const ALIPAY_SIGN_TYPE = "RSA2";
- const ALIPAY_FORMAT = "json";
- const ALIPAY_POST_CHARSET = "UTF-8";
- const ALIPAY_API_VERSION = "1.0";
- private $aop_client = NULL;
- private static $_instance = NULL;
- private function __construct(AopClient $aop){
- $this->aop_client = $aop;
- }
- public static function getInstance(string $app_id, string $rsa_private_key, string $rsa_public_key): AlipayCoupon
- {
- if (self::$_instance == NULL){
- $aop = new AopClient ();
- $aop->gatewayUrl = self::ALIPAY_SERVER_URL.self::ALIPAY_GATEWAY;
- $aop->appId = $app_id;
- $aop->rsaPrivateKey = $rsa_private_key;
- $aop->alipayrsaPublicKey = $rsa_public_key;
- $aop->apiVersion = self::ALIPAY_API_VERSION;
- $aop->signType = self::ALIPAY_SIGN_TYPE;
- $aop->postCharset = self::ALIPAY_POST_CHARSET;
- $aop->format = self::ALIPAY_FORMAT;
- self::$_instance = new AlipayCoupon($aop);
- }
- return self::$_instance;
- }
- private function check_voucher_key(array $params): bool
- {
- if (empty($biz_content['open_id']) && empty($biz_content['logon_id']) && empty($biz_content['phone_id'])) {
- return false;
- }
- if (empty($biz_content['activity_id']) || empty($biz_content['out_biz_no'])) {
- return false;
- }
- return true;
- }
- /**
- * 发放立减金
- * @param string $activity_id
- * @param string $out_biz_no
- * @param string $open_id
- * @param string $logon_id
- * @param string $phone_id
- * @return array
- */
- public function send_voucher(string $activity_id, string $out_biz_no, string $open_id = '', string $logon_id = '', string $phone_id = ''): array
- {
- $biz_content = [];
- if (!empty($open_id)){
- $biz_content['open_id'] = $open_id;
- }
- if (!empty($logon_id)){
- $biz_content['logon_id'] = $logon_id;
- }
- if (!empty($phone_id)){
- $biz_content['phone_id'] = $phone_id;
- }
- $biz_content['activity_id'] = $activity_id;
- $biz_content['out_biz_no'] = $out_biz_no;
- if (!$this->check_voucher_key($biz_content)){
- return [false, '参数错误'];
- }
- $request = new AlipayUserDtbankcustChannelvoucherSendRequest();
- $request->setBizContent(json_encode($biz_content));
- try
- {
- $resp = $this->aop_client->execute($request);
- if ($resp === false){
- return [false, '网络错误'];
- }
- }
- catch (Exception $e)
- {
- return [false, $e->getMessage()];
- }
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- return [$resp->$responseNode, ''];
- }
- /**
- * 查询红包流水
- * @param string $activity_id
- * @param string $out_biz_no
- * @param string $open_id
- * @param string $logon_id
- * @param string $phone_id
- * @return array
- */
- public function query_order(string $activity_id, string $out_biz_no, string $open_id = '', string $logon_id = '', string $phone_id = ''): array
- {
- if (!empty($open_id)){
- $biz_content['open_id'] = $open_id;
- }
- if (!empty($logon_id)){
- $biz_content['logon_id'] = $logon_id;
- }
- if (!empty($phone_id)){
- $biz_content['phone_id'] = $phone_id;
- }
- $biz_content['activity_id'] = $activity_id;
- $biz_content['out_biz_no'] = $out_biz_no;
- if (!$this->check_voucher_key($biz_content)){
- return [false, '参数错误'];
- }
- $request = new AlipayUserDtbankcustActivityorderQueryRequest();
- $request->setBizContent(json_encode($biz_content));
- try
- {
- $resp = $this->aop_client->execute($request);
- if ($resp === false){
- return [false, '网络错误'];
- }
- }
- catch (Exception $e)
- {
- return [false, $e->getMessage()];
- }
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- return [$resp->$responseNode, ''];
- }
- /**
- * 验证签名
- * @param array $params
- * @return bool
- */
- public function verify_sign(array $params): bool
- {
- if (!$this->aop_client->rsaCheckV1($params, null, 'RSA2')){
- return false;
- }
- return true;
- }
- public function on_notify(array $params): array
- {
- return [];
- }
- /**
- * 查询活动账单
- * @param string $activity_id
- * @return array
- */
- public function query_activity_bill(string $activity_id): array
- {
- if (empty($activity_id)){
- return [false, '参数错误'];
- }
- $biz_content = [
- 'activity_id' => $activity_id,
- 'bill_type' => 'ALL'
- ];
- $request = new AlipayUserDtbankActivitybillQueryRequest();
- $request->setBizContent(json_encode($biz_content));
- try
- {
- $resp = $this->aop_client->execute($request);
- if ($resp === false) {
- return [false, '网络错误'];
- }
- }
- catch (Exception $e)
- {
- return [false, $e->getMessage()];
- }
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- return [$resp->$responseNode, ''];
- }
- /**
- * 数字分行用户账号查询接口
- * $logon_id和$phone_id二选一
- * @param $logon_id
- * @param $phone_id
- * @return array
- */
- public function query_account($logon_id,$phone_id): array
- {
- $biz_content = [];
- if (!empty($phone_id)) {
- $biz_content['phone_id'] = $phone_id;
- }
- if (!empty($logon_id)) {
- $biz_content['logon_id'] = $logon_id;
- }
- if (empty($biz_content)) {
- return [false, '参数错误'];
- }
- $request = new AlipayUserDtbankcustAccountQueryRequest();
- $request->setBizContent(json_encode($biz_content));
- try
- {
- $resp = $this->aop_client->execute($request);
- if ($resp === false) {
- return [false, '网络错误'];
- }
- }
- catch (Exception $e)
- {
- return [false, $e->getMessage()];
- }
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- return [$resp->$responseNode, ''];
- }
- }
- ?>
|