支付宝支付

weigenzongderoot 2020-04-20

<?php
namespace alipay;
require_once ‘aop/AopClient.php‘;
require_once ‘aop/request/AlipayTradeAppPayRequest.php‘;
require_once ‘aop/request/AlipayTradeCreateRequest.php‘;
require_once ‘aop/request/AlipayTradePrecreateRequest.php‘;
/**
  • 支付宝支付支付
    */
    class alipay{
    private $pay_url = ‘https://openapi.alipay.com/gateway.do‘; // 请求网关
    /**

    • app支付
    • [app_pay description]
    • @Author 念天地之悠悠
    • @DateTime 2019-12-09
    • @param [type] $appId [description] appid
    • @param [type] $rsaPrivateKey [description] 请填写开发者私钥去头去尾去回车,一行字符串
    • @param [type] $alipayrsaPublicKey [description] 请填写支付宝公钥,一行字符串
    • @param [type] $body [description] 交易描述 例如 购买商品
    • @param [type] $subject [description] 主题
    • @param [type] $out_trade_no [description] 商户订单号
    • @param [type] $total_amount [description] 支付金额
    • @param [type] $notify_url [description] 异步通知接口
    • @return [type] [description]
      */
      public function app_pay($appId,$rsaPrivateKey,$alipayrsaPublicKey,$body,$subject,$out_trade_no,$total_amount,$notify_url){
      $total_amount = number_format($total_amount,2,".","");
      $aop = new \AopClient();
      $aop->gatewayUrl = $this->pay_url;
      $aop->appId = $appId;
      $aop->rsaPrivateKey = $rsaPrivateKey;
      $aop->alipayrsaPublicKey = $alipayrsaPublicKey;
      $aop->apiVersion = ‘1.0‘;
      $aop->signType = ‘RSA2‘;
      $aop->postCharset = ‘UTF-8‘;
      $aop->format = ‘json‘;
      $request = new \AlipayTradeAppPayRequest();
      // $biz_content = json_encode([
      // ‘body‘ => $body,
      // ‘subject‘ => $subject,
      // ‘out_trade_no‘ => $out_trade_no,
      // ‘timeout_express‘ => ‘30m‘,
      // ‘total_amount‘ => $total_amount,
      // ‘product_code‘ => ‘QUICK_MSECURITY_PAY‘
      // ]);
      $request->setNotifyUrl($notify_url);
      $biz_content = "{\"body\":\"$body\","
      . "\"subject\": \"$subject\","
      . "\"out_trade_no\": \"$out_trade_no\","
      . "\"timeout_express\": \"30m\","
      . "\"total_amount\": \"$total_amount\","
      . "\"product_code\":\"QUICK_MSECURITY_PAY\""
      . "}";
      $request->setBizContent($biz_content);
      $result = $aop->sdkExecute($request);
      // $result = $aop->execute ($request);
      return [‘code‘=>1,‘data‘=>[‘result‘=>$result],‘msg‘=>‘获取成功!‘];
      // $responseNode = strreplace(".", "", $request->getApiMethodName()) . "_response";
      // // dump($result);die;
      // $resultCode = $result->$responseNode->code;

      // if(!empty($resultCode)&&$resultCode == 10000){
      // $ret = [‘code‘=>1,‘data‘=>$result->$responseNode,‘msg‘=>$result->$responseNode->msg];
      // } else {
      // $ret = [‘code‘=>0,‘data‘=>$result,‘msg‘=>$result->$responseNode->msg];
      // }
      // return $ret;
      }
      // 面对面支付
      public function createTrade(){
      $aop = new \AopClient();
      $aop->gatewayUrl = $this->pay_url;
      $aop->appId = ‘2021001150665929‘;
      // 应用私钥
      $aop->rsaPrivateKey = ‘MIIEowIBAAKCAQEAnBvMW167zJrpiCNAT2Bv6BF/gQjQAVZeQEL7PmHHr6F/t680hQk24ORgS5xqBSbdqYCzABpCaSLkyx0Hiz2XcbhalL69KBxLVK1fWTIWddurlGQSb34Sp85HIvVRggy7HBrcegZDRv2ugYZMkw3yWxp8Ne+koKXhtNzc6gjk9ui4QA80GBFbJuD3Tumn3g1vCKByCw4I7TimUk0GE+YPjIjdZFmgAxNmPWpV6Fkg1r9ASp6PtoTiU3c3/GQQUZMgWyZxTAq9vxJ2w8GhLTDcAY9BwgIYLY8LdsYwGE/IAE5msLnfEZ3U870iv/yhBsSTeqpqNfRvqcH3nVpivIZozwIDAQABAoIBABIvvuAKKlDM31YH3U8iCgd8OmF6+Fifij1sUD7VP2E7rFhm5fR1vVn34SwN3eAZxgsgPjpN1NzQvtzW1fEnzMPn2U2o5vqMSU5lIE2Q40L5w4R3IBqzElNQWcM91zAScZ31lyf0INRda34VekhLgKeSb1QNRh2dFa1vaaEuRhZpmapL6MrOfjekKHEnNb8UvlaVjlQLuN99bUNQKYV5sn0u4oHUG6qbnlwyu0nUGJyx9fWoHmApY1gWCQiVvqaQuHrnzTxPOAx1uEq+i7q3mCDLZoVf5zouap+H0hH6IVM/KAkUres6HD96T224qPH9DQ7WWFieWWd+TAWBAWA3kyECgYEAztgxr4JQFx+IHPznnUUmQrB4ndVOVelDQWxV7FgBxLiwHo3jZMso53c3jKEjePd2bG+LLxcco8ZL0gGMuwKeHqTlrH8***4roks/2N6dgcuohZH6sZsZSQxalmiHzz+FRqri/USX58vdYVJm6v/bo4KFnjKDAE3vd3b9dmvZGZ8CgYEAwTT7OOrx0OqZdkHKjY4OdMH6qtBi9WW4rhGsgJT9aEVMiMOHyMpugMrJ9K4zQqcaVVO/LtrloPqhr/4rBAWU13oXUjmld/Rtj55Vgdodomuy68YgqXYSe/isIAOIJ4O2NG87rDCbDQ5wkuvST4ExdfVHupREvS/GLYoDPCRiwtECgYEAgjnRBkvH+tbqTj3X4MdBcGlEdzJtkF887bq0WgmKi30vvPzd9n6F8Ty4p3xoJee/EkQj3/DwIhhdcglxvefG/0oYI8MrTnAHLrWrq/F0Ptym6/ldWWZpw/jYK6wCHTRDx5sdMBF4hgJvDa/ghgGyGb66O0BYA+ANbSxoJZ9+3scCgYBjTWvh8T+gdTOzCFWnM+BSOnEv4jM6lbG6JfNXkzw7n4KauITNut3Y7GGolPBbza9HM+gLmQ51EtyPrD9Hrea2aVYbOO3HHaZCLsscFpMsq9YmQIsBDmKpvUIaojYUuSmpAZLWCZVnvIg3u2U439ogGpVnXFWIN0nQliLbgogQQQKBgH1VckrH3Gliq+E7AiGAIevrt5e3I5G5sEGJMD5SEBrKDbs0XeXFpoWf/Ye1S8htP8MReZwWdtMs2hFeCSyF/B0LI6dOjFhu6oKa7vriBBcKohHYcPQPo1RbsAmrlto5SAAeCIX+pYfWI8G4eEAfzslqyHautDoIF5N3/+VJ7WdH‘;
      $aop->format = "json";
      $aop->postCharset = "UTF-8";
      $aop->signType = "RSA2";
      // 支付宝公钥
      $aop->alipayrsaPublicKey = ‘MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlbkPEDkK4iBjUc88Jv9dS3C0oC3gwAT+WDXa1jt/8NHBSUoy9xM1BUdQh41YCAx883NxlMSe8bbYeEFCmqgFmDV9m0qUfAujnVdazaYEgoL6AVBIXKDGsfKEXVgvkd9I7z1+JWrA+XCKJha/cDDtVrzY9qq7mmkO+1pMJ34GhB+BVvNN0Gv89zmDVWcJDWyICQq7rncsR/XhRGtNusaKnV1GIZfR1Vr/9yBLuvsChVn4BLcxjs8wXJzf//0pWo4E5srQvZBrshxCm5IJwGzHAOfepgT9ZV2DD3vcQl8Ksdkm+xTU5HJJH59H/rpzUdV1nuyxyAXCRaggqaVHBIfY6QIDAQAB‘;
      $aop->apiVersion = ‘1.0‘;
      $request = new \AlipayTradePrecreateRequest();
      $money = 0.01;
      $biz_content = json_encode([
      ‘body‘ => ‘支付宝扫码支付‘,
      ‘subject‘ => ‘支付宝扫码支付‘,
      ‘out_trade_no‘ => ‘wait9731212‘.time(),
      ‘timeout_express‘ => ‘30m‘,
      ‘total_amount‘ => $money,
      ‘product_code‘ => ‘FACE_TO_FACE_PAYMENT‘
      ]);
      $request->setBizContent($biz_content);
      $response= $aop->execute($request);
      $responseNode = strreplace(".", "", $request->getApiMethodName()) . "_response";
      $resultCode = $response->$responseNode->code;
      if(!empty($resultCode)){
      //成功 获取二维码 ?这里是获取到二维码的内容,需要自己找工具把内容变为二维码
      return $response->$responseNode;
      } else {
      return false;
      }
      }
      /**

    • 退款
    • [order_back description]
    • @Author 念天地之悠悠
    • @DateTime 2020-01-14
    • @param [type] $appid [description] appid
    • @param [type] $rsaPrivateKey [description] 请填写开发者私钥去头去尾去回车,一行字符串
    • @param [type] $alipayrsaPublicKey [description] 请填写支付宝公钥,一行字符串
    • @param [type] $out_trade_no [description] 支付宝交易单号
    • @param [type] $refund_amount [description] 退款金额 单位 元
    • @param [type] $out_request_no [description] 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传。
    • @return [type] [description]
      */
      public function order_back($appid,$rsaPrivateKey,$alipayrsaPublicKey,$out_trade_no,$refund_amount,$out_request_no){
      $aop = new \AopClient ();
      $aop->gatewayUrl = $this->pay_url;
      $aop->appId = $appid;
      $aop->rsaPrivateKey = $rsaPrivateKey;
      $aop->alipayrsaPublicKey = $alipayrsaPublicKey;
      $aop->apiVersion = ‘1.0‘;
      $aop->signType = ‘RSA2‘;
      $aop->postCharset = ‘GBK‘;
      $aop->format = ‘json‘;
      $request = new \AlipayTradeRefundRequest();
      $request->setBizContent("{" .
      "\"out_trade_no\":\"$out_trade_no\"," . // 支付宝交易单号
      "\"refund_amount\":$refund_amount," . // 退款金额 单位 元
      "\"refund_currency\":\"USD\"," . // 人民币 标识
      "\"refund_reason\":\"正常退款\"," . // 退款说明
      "\"out_request_no\":\"$out_request_no\"," . // 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传。
      "}");
      $result = $aop->execute($request);

      $responseNode = strreplace(".", "", $request->getApiMethodName()) . "_response";
      $resultCode = $result->$responseNode->code;
      if(!empty($resultCode)&&$resultCode == 10000){
      $data[‘code‘] = 1;
      $data[‘data‘] = ‘‘;
      $data[‘msg‘] = $result->$responseNode->msg;
      }else{
      $data[‘code‘] = 0;
      $data[‘data‘] = ‘‘;
      $data[‘msg‘] = $result->$responseNode->msg;
      }
      return $data;
      }
      }

相关推荐