ThinkPHP5.0+APP+ Alipay payment server development

Keywords: JSON PHP SDK Database

Working environment, Alipay account is company, app application and secret key configuration are my own application, and the process is temporarily skipped.

I. preparations

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

II. Configuration file

'alipay'=>[
        'appId'             => '20180300000000',
        'gatewayUrl'        => 'https://openapi.alipay.com/gateway.do',
        'rsaPrivateKey'     => 'Application of private key',
        'rsaPublicKey'      => 'Application of public key',
        'alipayrsaPublicKey'=> 'Alipay public key',
        'seller'            => 'Alipay mail box',//Do not
        '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 below

III. create payment method

Create payment class

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

class Alipay
{

    /*
     * Alipay payment
     * $body            Name
     * $total_amount    Price
     * $product_code    Order number
     * $notify_url      Asynchronous callback address
     */
    public function alipay($body, $total_amount, $product_code, $notify_url)
    {

        /**
         * Call the Alipay interface.
         */

        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;

    }

}

(II) create payment method

namespace app\index\controller;

use think\Config;
use think\Request;

class Payment extends Common
{
    //Test server
    private     $domain = 'http://xxxx.com';
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public function payOrder()
    {

        //Get order number
        $where['id'] = input('post.orderid');
        //Query order information
        $order_info = db('order')->where($where)->find();
        $reoderSn = $order_info['ordersn'];
        //Get payment method
        $pay_type = input('post.paytype');//WeChat payment or Alipay payment
        //Get payment amount
        $money = 0.01;//$order_info['realprice'];
        //Judge payment method

        if ($pay_type == 'alipay') {

            $type['paytype'] = 1;

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


            $alipay = new Alipay();

            //Asynchronous callback address
            $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, 'Success');
            } else {

                return $this->response('', 0, 'Sorry, please check the relevant parameters');
            }
        }


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


        }
    }

    /*
         * Alipay pays callback to modify order status
         */
    public function alipay_notify()
    {
        //Original order number
        $out_trade_no = input('out_trade_no');
        //Alipay transaction number
        $trade_no = input('trade_no');
        //Trading status
        $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);//Modify order status, Alipay odd number to database

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

        }else{
            echo "fail";
        }



    }

   //Relative address to absolute address
    protected function url_translation_address($url)
    {
        return $this->domain . $url;
    }

}

IV. next, after Android or iOS are successfully docked, they can be tested. If there is no response to the callback address or other problems, you can view the header response information of Alipay in the open platform mediation log check.

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

Posted by penguin_powered on Thu, 14 Nov 2019 12:02:56 -0800