支付宝app支付-php

闽念
2023-12-01
支付宝app支付-php

官方文档:https://docs.open.alipay.com/204/105297/

1、创建应用并获取APPID

2、添加功能并签约

3、配置密钥
	1、下载工具:http://p.tb.cn/rmsportal_6680_secret_key_tools_RSA_win.zip
	点击“RSA签名验签工具.bat”
	pkcs1(非java适用)
	2048位
	点击生成密钥
	于是就生成了一对公钥和私钥,都是单行的,保存到两个文件。
	2、将公钥上传到支付宝平台
	3、将支付宝的公钥保存到本地文件

4、下载php-sdk
	https://openhome.alipay.com/doc/sdkDownload.resource?sdkType=PHP
	解压保存在项目根目录下的 Extends\alisdk

5、将私钥文件命名为 rsaPrivateKey ,保存到 Extends\alisdk\key 下面
	将支付宝公钥文件命名为 alipayrsaPublicKey ,保存到 Extends\alisdk\key 下面

6、封装参数及签名
	function ali_order($log){
        //支付宝 app 支付
        require_once( EASYSWOOLE_ROOT . '/Extends/alisdk/aop/AopClient.php' );
        require_once( EASYSWOOLE_ROOT . '/Extends/alisdk/aop/request/AlipayTradeAppPayRequest.php');

        $aop = new \AopClient();

        $config = ConfigModel::getInstance()->config_list('aliapp_pay', 1);

        //**沙箱测试支付宝开始
        //$aop->gatewayUrl = "https://openapi.alipaydev.com/gateway.do";// 上线注释此行
        //实际上线app id需真实的
        $aop->appId = $config['config_config']['appid'];
        //'填写工具生成的商户应用私钥';
        $aop->rsaPrivateKey = file_get_contents( EASYSWOOLE_ROOT . '/' . \Yaconf::get('pay.alipay.rsaPrivateKeyPath') );
        $aop->format = "json";
        $aop->charset = "utf-8";
        $aop->signType = "RSA2";
        //'填写从支付宝开放后台查看的支付宝公钥';
        $aop->alipayrsaPublicKey = file_get_contents( EASYSWOOLE_ROOT . '/' . \Yaconf::get('pay.alipay.alipayrsaPublicKeyPath') );
        $body = (int)($log['get_coin'] / $this->scale) . '金币';
        $bizcontent = json_encode(array(
            'body'         => $body,
            'subject'      => $body,
            'out_trade_no' => $log['orderno'], //此订单号为商户唯一订单号
            'total_amount' => sprintf('%.2f', $log['order_price'] / 100), //保留两位小数
            'product_code' => 'QUICK_MSECURITY_PAY'
        ));
        //**沙箱测试支付宝结束
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
        $request = new \AlipayTradeAppPayRequest();
        //支付宝回调
        $request->setNotifyUrl($config['config_config']['notify_url']);
        $request->setBizContent($bizcontent);
        //这里和普通的接口调用不同,使用的是sdkExecute
        $order['orderno']  = $aop->sdkExecute($request);
        $order['total_fee'] = $log['order_price'];

        return $order;
    }

    $order 的信息为:
	[
		orderno=>"alipay_sdk=alipay-sdk-php-20180705&app_id=2019042664327019&biz_content=%7B%22body%22%3A%2210%5Cu91d1%5Cu5e01%22%2C%22subject%22%3A%2210%5Cu91d1%5Cu5e01%22%2C%22out_trade_no%22%3A%22385a40d2b179dd795271e16e%22%2C%22total_amount%22%3A%2210.00%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fl8pay.ali09.cn%2Fnotify%2FaliappNotify.php&sign_type=RSA2&timestamp=2019-05-13+10%3A46%3A26&version=1.0&sign=Vgc7YB%2BK77l2ARB%2FIYLnxbC09CIjqbtiglkv4ASdMj9zXfDGnalNzqrSR61Hk50Fp3EontHIWQq0dSJE04uCsdGpTFeHjDe6MDNa9FkGXroV9dWH6P%2BjPczPe8q2paxvIcD6gVb7GEfhID7aR0HNkChEB89KZCVAKiP0w6o6SjlWSEhX09H2Jc6ICVlvDBwyPq7wL6EwX2Ap0k8eAENYgzdhFihFX7RU%2BQ5qmP6%2F7BtUdzz2rwU2nRcySXZPIo0PONoMc21RbeG3xBITmfxHjBpPnjVyDHRD1roFy5odEnSo9G9fjYOBaZuhm6OTkAAFNxCcmVFuBud6AOSLGMdyTQ%3D%3D",
		total_fee=>1000
	]

	将 orderno 信息返回给客户端,客户端也需要集成相应的SDK,于是才能拉起支付宝客户端。

7、异步通知验签
	将 Extends\alisdk 复制过来

	protected function checkSign(){
        $this->aop->alipayrsaPublicKey = file_get_contents($this->alipayrsaPublicKeyPath);

        $flag = $this->aop->rsaCheckV1($this->postData, NULL, "RSA2");

        return $flag;
    }

    // 校验状态
    if($this->postData['trade_status'] != 'TRADE_SUCCESS'){
        $this->responseSucess();
    }

    public function responseError(){
        exit('fail');
    }

    public function responseSucess(){
        exit('success');
    }
 类似资料: