ThinkPHP5.0+APP+支付寶支付 服務端開發

 

1、準備工做php

appid、應用私鑰、應用公鑰、支付寶公鑰數據庫

2、配置文件express

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

2、下載官方的SDK包,放在extend下,如圖json

3、建立支付方法api

      (一)建立支付類服務器

<?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;

    }

}

    (二)建立支付方法微信

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、接下來,安卓或者iOS 對接成功後,就能夠進行測試了。若是出現回調地址沒有響應或者出現其餘問題,可在開放  平臺聯調日誌排查  中查看支付寶的報頭響應信息app

相關文章
相關標籤/搜索