ThinkPHP5.0+APP+Alipay payment server development

In the working environment, the Alipay account is owned by the company, and the app application and key configuration are all applied for by myself, and the process is skipped for the time being.

1. Preparation

appid, application private key, application public key, Alipay public key

2. Configuration file

'alipay'=>[
        'appId'             => '20180300000000',
        'gatewayUrl'        => 'https://openapi.alipay.com/gateway.do',
        'rsaPrivateKey'     => '应用私钥',
        'rsaPublicKey'      => '应用公钥',
        'alipayrsaPublicKey'=> '支付宝公钥',
        'seller'            => '支付宝邮箱',//可不要
        'format'            => 'json',
        'charset'           => 'UTF-8',
        'signType'          => 'RSA2',
        'transport'         => 'http',
    ],

2. Download the official SDK package and put it under extend, as shown in the figure

3. Create a payment method

      (1) Create a payment class

<?php
namespace app\index\controller;
use think\Config;

class Alipay
{

    /*
     * 支付宝支付
     * $body            名称
     * $total_amount    价格
     * $product_code    订单号
     * $notify_url      异步回调地址
     */
    public function alipay($body, $total_amount, $product_code, $notify_url)
    {

        /**
         * 调用支付宝接口。
         */

        import('.Alipay.aop.AopClient', '', '.php');
        import('.Alipay.aop.request.AlipayTradeAppPayRequest', '', '.php');

        $aop = new \AopClient();

        $aop->gatewayUrl            = Config::get('alipay')['gatewayUrl'];
        $aop->appId                 = Config::get('alipay')['appId'];
        $aop->rsaPrivateKey         = Config::get('alipay')['rsaPrivateKey'];
        $aop->format                = Config::get('alipay')['format'];
        $aop->charset               = Config::get('alipay')['charset'];
        $aop->signType              = Config::get('alipay')['signType'];
        $aop->alipayrsaPublicKey    = Config::get('alipay')['alipayrsaPublicKey'];

        $request = new \AlipayTradeAppPayRequest();
        $arr['body']                = $body;
        $arr['subject']             = $body;
        $arr['out_trade_no']        = $product_code;
        $arr['timeout_express']     = '30m';
        $arr['total_amount']        = floatval($total_amount);
        $arr['product_code']        = 'QUICK_MSECURITY_PAY';

        $json = json_encode($arr);
        $request->setNotifyUrl($notify_url);
        $request->setBizContent($json);

        $response = $aop->sdkExecute($request);
        return $response;

    }

}

    (2) Create a payment method

namespace app\index\controller;

use think\Config;
use think\Request;

class Payment extends Common
{
    //测试服务器
    private     $domain = 'http://xxxx.com';
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public function payOrder()
    {

        //获取订单号
        $where['id'] = input('post.orderid');
        //查询订单信息
        $order_info = db('order')->where($where)->find();
        $reoderSn = $order_info['ordersn'];
        //获取支付方式
        $pay_type = input('post.paytype');//微信支付 或者支付宝支付
        //获取支付金额
        $money = 0.01;//$order_info['realprice'];
        //判断支付方式

        if ($pay_type == 'alipay') {

            $type['paytype'] = 1;

            db('order')->where($where)->update($type);


            $alipay = new Alipay();

            //异步回调地址
            $url = $this->url_translation_address('/index/payment/alipay_notify');

            $array = $alipay ->alipay(Config::get('company'), $money, $reoderSn, $url);


            if ($array) {
                return $this->response($array, 1, '成功');
            } else {

                return $this->response('', 0, '对不起请检查相关参数');
            }
        }


        if ($pay_type == 'wechat') {
            $type['paytype'] = 2;


        }
    }

    /*
         * 支付宝支付回调修改订单状态
         */
    public function alipay_notify()
    {
        //原始订单号
        $out_trade_no = input('out_trade_no');
        //支付宝交易号
        $trade_no = input('trade_no');
        //交易状态
        $trade_status = input('trade_status');


        if ($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {

            $condition['ordersn'] = $out_trade_no;
            $data['status'] = 2;
            $data['third_ordersn'] = $trade_no;

            $result=db('order')->where($condition)->update($data);//修改订单状态,支付宝单号到数据库

            if($result){
                echo 'success';
            }else{
                echo 'fail';
            }

        }else{
            echo "fail";
        }



    }

   //相对地址转绝对地址
    protected function url_translation_address($url)
    {
        return $this->domain . $url;
    }

}

4. Next, after the Android or iOS docking is successful, you can test it. If there is no response from the callback address or other problems occur, you can view the header response information of Alipay in the open platform joint debugging log troubleshooting.

Address https://openmonitor.alipay.com/acceptance/cloudparse.htm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324404824&siteId=291194637