tp5.1接入支付宝支付接口

第一步,下载dome

一些基础的东西就不多叙述了,比如申请开发者账号,然后密钥,公钥什么的,官方有说明,这边教程主要是干货.
下载完dome后,将它重命名为alipay,放在tp的vendor目录下,里面的东西基本不动,暂时

第二步,修改配置文件

修改配置文件中的app_id应用id,merchant_private_key用户私钥,alipay_public_key支付宝公钥,还有异步,同步地址

第二步,干货

在自己的控制器里面调用:

 "2016092900619970",

           //商户私钥
           'merchant_private_key' => "MIIEogIBAAKCAQEA2cO618CgcTyBVCHj11aZ3h1Viw+Kq/yZILw7rG+dOaOI/eQYAn2S9iUj0GriutN6QlTvDkRqYITW+6VtvBwLqmm/HpZBc77aE++AeQAi3vDnUO8mardk3JG5RKSl3zYFeleFNtGHr3DsDcwH3ND/vXIfBq5CuvGIpusOf16Stvo2dd7Xz0/RXJbuPAWcbS3de4qGeaCQqW72CE5PWOpe8wgHIGo7t/9aEL9xO+Vtni+E09g4JPQsmtPp07nXHnEaKRbyJlhuXA0ehAMzRQ/dUfQf7RRtXhOZBmvbhzsMOmu7YSA8zlSYodRkSzCA990bDvw/6kQW3eZbZXW4KRmImQIDAQABAoIBAHJrcIoasXKuKvoM0I19+F0HpsHpiaBymfbkUKp6/u6hKcLVjXQJjSV+QEIrVzR5F70wqWcitY4/XkJdGrqDNfH1nDEzG/L4LdfZuPRbCPeBNtmEvvfFot5uRx11MZ6F4Pz2bY1vLM8XE0DwmpUAz8833muBNpjJflbOHRD5nTc8ayo5ZienvQYmVEOiaHVNW9hNXNQBFT0sjZFlJcqGNo6rz34nlKC9PMJhR8GzcW6LWAy4BvlZT3eYCDZSW2E0w2ErE9RCOZyjw7QGsTuREydTGeVUxobjpxy3aXh0NLv1q5gUi6behDUnLKasG/c48cRN+7cl+/+uExS4WI46BjECgYEA71qST14oNnvb7491pnuBydXeCbeKkRIBL2s9L7hmzJkWokrwwwE+og8ohqBdzYptQ86E7mVUPbfvSHSmBxH0vGerZzBpTLIslBnA4NU5p4WBfPF99h1KCtjKVRGmKf6yM7ex9LZrFXf3/gsy0CLU/XGBR3pbmLSnWcf4t8tfoUUCgYEA6OjJQV6myxcQqiGreW/EQmlSE/B0TPc8c4GEeupI0V2EH5VriKpeRw1m/nMnEzoi5Tqkv3mqeArXXnewKH2Lp3tit+QdGyyokLiN3CfAZH92uU/q3GvlwO/XMw/27uo8AVGKn4Zr9h11KxERRn834ZEFt3IPFUm53B49XJ8ZXUUCgYA12+COYXaVA/vyhQqJQsNqwmgrMZTJng6MOApVoZ3oLg6MyGYyyyXn25dX6+APrzV1C5SX/E8fcDM0z6LIdn7mokFMf/VB39nuHXlsXi6SBpWjosegSOjvVeb5i3Yjfv9kadJuaVnhQ9SW4rg1Y5Ef0pmvltXjn8sEhd3upaHILQKBgGzvvHo7yC3DewLoPqjM8vHRCxfSZsM8/GRWFt3ayPZMFCklnSdN8EyxYxPuTj96+KlY1YfNY0NEZtmDh7xYhEJwvoqeQlLcf3c5KvjnCuC2kZQ5YfN47/xe8burYub6XdiMCBd3KvXxb0UWdYsQo9V4HEFiMtXVJv5iknbI33c1AoGAQs3VmNPMCZGi0fd882X0X8O5Ca++J2n+6YyApZAPwDXzlC/W92Mxni1T/gr/1LTJw4IsuWo8E23QeJtReedrWfEal+4ocLd1bHizOdPqaRHmTZfKC9OLjRIQCtiT/WFyo88KGTbM6OFSS+HTV7fII1U3Bt9evFRCPA+jDtXI9as=",

           //异步通知地址
           'notify_url' => "http://tp.test/api/index/notify",

           //同步跳转
           'return_url' => "http://tp.test/api/index/returnfy",

           //编码格式
           'charset' => "UTF-8",

           //签名方式
           'sign_type' => "RSA2",

           //支付宝网关
           'gatewayUrl' => "https://openapi.alipaydev.com/gateway.do",

           //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
           'alipay_public_key' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAndXlKv68inoOevZr2N70PIanm2A/7IkXVl7C/hnB8c5H+gLl/sIstZ8kfPSG3zuxuGgO66YVY5m4KrIMrzPqS/Aqi0Ypi4aL5ILI/7m2mwhvw6b95RSmvSFcHqEOJpgXLRXytw6w8UQGzZMKRQ2E1IP+iuFykTLjmWyjfShGs02aEk3zLA6hx7mkBZxQkVqMVzJDxBi9X7N73XW8wDgYVuk5hhpLeLH+IVU6k+AqCv48KZK//HF9f+oV1tIV56wU0TirZHpTOjaz13W67P+9uuq+Pn6CHE1StRDx3i0kRFTZXGZlwA23IrHehtluh1S970NvSnR2rUrb8lHUA9yUCwIDAQAB",
       );
       if ($_POST['WIDsubject']) {
           include_once VENDOR_PATH . '/alipay/config.php';
           include_once VENDOR_PATH . '/alipay/pagepay/service/AlipayTradeService.php';
           include_once VENDOR_PATH . '/alipay/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php';
           //商户订单号,商户网站订单系统中唯一订单号,必填
           $out_trade_no = trim($_POST['WIDout_trade_no']);

           //订单名称,必填
           $subject = trim($_POST['WIDsubject']);

           //付款金额,必填
           $total_amount = trim($_POST['WIDtotal_amount']);

           //商品描述,可空
           $body = trim($_POST['WIDbody']);
           //构造参数
           $payRequestBuilder = new \AlipayTradePagePayContentBuilder();
           $payRequestBuilder->setBody($body);
           $payRequestBuilder->setSubject($subject);
           $payRequestBuilder->setTotalAmount($total_amount);
           $payRequestBuilder->setOutTradeNo($out_trade_no);

           $aop = new \AlipayTradeService($config);

           /**
            * pagePay 电脑网站支付请求
            * @param $builder 业务参数,使用buildmodel中的对象生成。
            * @param $return_url 同步跳转地址,公网可以访问
            * @param $notify_url 异步通知地址,公网可以访问
            * @return $response 支付宝返回的信息
            */
           $response = $aop->pagePay($payRequestBuilder, $config['return_url'], $config['notify_url']);

           //输出表单
//            var_dump($response);
       }
   }

   public function notify()
   {
       $post = input();
       if ($post['trade_status'] == "TRADE_SUCCESS") {
           Db::name('order')->where('out_trade_no',$post['out_trade_no'])->update(array('pay_status'=>'success'));
           //操作数据库 修改状态
           echo "SUCCESS";
       }
       //写在文本里看一下参数
//        $data = json_encode($post);
//
//        fopen("testfile.txt", $data);

   }
   /**
    * 同步方法
    * @return [type] [description]
    */
   public function 

returnfy()
   {
       $post = input();
       //同步跳转地址
       var_dump($post);
   }
}

说明一下:

  1. 我用的是沙箱环境,改为正式环境需要将支付宝网关改为https://openapi.alipay.com/gateway.do
  2. tp中可以将同步,异步的地址写成域名/模块名/控制器/方法的形式
  3. 我的控制器地址为api/Payali,调用方法为index方法,需要参数有WIDout_trade_no订单号,WIDsubject订单名称,WIDtotal_amount付款金额,WIDbody其他信息,请求方式为post

你可能感兴趣的:(PHP杂项,php)