支付宝支付(pc)

一、支付宝支付
1.搜索蚂蚁金服开发平台
2.免费入住
支付宝支付(pc)_第1张图片
3.登录
支付宝支付(pc)_第2张图片
4.使用沙箱环境
支付宝支付(pc)_第3张图片
5.APPID、支付宝网管、秘钥
支付宝支付(pc)_第4张图片

6、查看开发文档
支付宝支付(pc)_第5张图片

7、找电脑网站支付(也有其他支付方式)
支付宝支付(pc)_第6张图片
8、阅读内容
支付宝支付(pc)_第7张图片
9、使用别人写好的SDK 模块

https://github.com/fzlee/alipay/blob/master/README.zh-hans.md

10、下载沙箱钱包
支付宝支付(pc)_第8张图片

11、沙箱账号
支付宝支付(pc)_第9张图片

12、写测试代码

def test():
    # 应用的私钥
    app_private_key_string = """-----BEGIN RSA PRIVATE KEY-----
            MIIEpQIBAAKCAQEA2RytcQwVHDi5ra/xpHm2rQ0TjXEDBNZ
            YmUVmNbyd12hnbCBNUAP3m/6QgzeSe2Y7wPKKmp1mb+BVVKZQua8
            VoSohRlRE59GyK1f0IPCR1Tusjwkuw+E6dsarPRCSNJ1knJEsyjBwDCsG/tx/
            nUHbhrbWss0OoGb+h3TMjMWT82fXX3G0aJpA+uRe6n18iR/6q/1vgWA1Har
            cTZfGlTAOox3/fj7bDluOr9cFCTVNPLahN3P/9qvwCQGE7pouBB8qHkAhutT
            V3wG8qf8BW2tMag0F9Yb82yvQVnbt1AARh36VrUGV555vWWkOaSrGViJhKKpZc
            ftiQmgbH6VUw1iAtwIDAQABAoIBAQCj6ViwTlTpt5HHJVwjtphJ0eN/F2EWF9VP
            8esWis4HwJTENybnT/dyWCA2UJBQhYB7TSGDVW3u7Mi/Xcj4Rsznnez9McXL1q7
            StlZjP/x5pdHDM0WRMMt4vMivE4GuI13ALoJIK4WMSje/1iyqRvw7mnJBDzsJsnhb
            P7zmUow+aDTSx5uDHqwvZuGzAvpAvkexVqIouTOJ+gkcaJAcoQpiLuvuldV+/CIdh
            H55SFAM5FQH8MXhs4wFeX8+9keJ+DahF37Tl3Dk90lzdeOV/NBbHdhqjdh9WthS
            sO3S6MUxx2vIAFLZ9BXKsPE873JePAqY5vUO8XwDeUHCMV7wYX4RAoGBAPlNpKVn
            Wapwwh+Ri1kJz3NZBDAJ7ik0g5plUAKKBdzX3LZYcqTjilmgRIBOub0sDx76rTUd40
            cbiv9XOa6qXcyNHSer+yEMa0v5gRayEBffId9xoVandnJdGcDRWh2IovtVr7cQWwYt
            TJCCoG0N2qSShnwoiqnnE2ORbJ19SkZNAoGBAN7xqwsJr1tjzJSq43Aa8jwtvV3Pcs+Y
            qH+uzHdebfK8sEc0a7Hvk57Uc2w0a3mahOu79cX7FIXSX9MrC7vv4slbC/vfptKSM4VsS
            phn2eT6G20rZjp+UjwOPWTz4OfC5ewS5lOjP8+fycUdyPfjZ80I9C5NWVwUEVr0L2riw
            O0TAoGBAJ/g8D73CaTpjUTLhQ8D4Jt7sdjQ9YBNYjyozmfidiIL+UqjbFpcZkRWKLgURL
            +RutzGLEBNO41Fl51rJd3i+iaNNO5VN8cY/NKLXwLcDMASY9m1OJ+m46oyFKr4V49rKg
            J/ECIZhhZvBJQEgbeOU/KI2WBUa6DMRT3sbDiPugtdAoGAFaYqEdxS5vD+AMHHbC9xrl5S
            sTVf1MDiO+PhfwXwb03bYE661oCJOTcwLiW4+goDNlD+QMds6sHPBFZ9wTY+M4df1om
            VTYVc82vcoogTJKUU3LkGKE/z9Fj9pTv5bwgCA7aiHyUPIT5G/Zky1r+CaOqhq41fTA220b
            o8kIiMa3kCgYEAgDRVKzhz1ueqIOsA6XcdHlhpSoUKmzwHeE2sD00kWVyQkKGN2/TczZ+h0
            seEM20M3C7tRVJjaN5rgfNg7biEtCxqvQgiWKchb8wfPm6Tg7Zlw9njeKBbQzq7YkO7iCLI
            KZoygTqx65b/gYx8QjSeZ1EoS532ndDztx+RmITuJbE=
            -----END RSA PRIVATE KEY-----
        """
    # 支付宝的公钥
    alipay_public_key_string = """-----BEGIN PUBLIC KEY-----
            MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAubtn6v/fRxicLhOdTijJ0xqXLZflHjvq31Y13NId7Xjs5w7vHid
            dCrml0k69iD0COo/1Ke8AJLVajwh9Zj/ZtAjjRjiK9cRW4SgTRlTm3Bnbqm5aZucmunOwgJfeCohgLTkFIwOOPbSb93u
            +nwvMTWAayiblm3aHGmZuBxb0EsUP5IBg1XCs26Am5gV8nWZ87cxDrF1C36i94kbDpmhFubicEIw
            +NMRuyKmWObrTZmxugv2AcM4lgM1LVz+Qa4H1mj0KQl4CjRHMlImSrznsvWq+5HUbIxMA7VUStA
            +mpJHFG4azb1jWOlatK984vXXgfLqRSH/gu2lkRA9gEGc36wIDAQAB
            -----END PUBLIC KEY-----
        """
    alipay = AliPay(
        appid="2016092800613099",
        app_notify_url=None,  # 默认回调url
        app_private_key_string=app_private_key_string,
        # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
        alipay_public_key_string=alipay_public_key_string,
        sign_type="RSA2",  # RSA 或者 RSA2
        debug=True  # 默认False
    )

    # 电脑网站支付,需要跳转到https://openapi.alipay.com/gateway.do? + order_string
    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no="2019051512321",  # 订单id
        total_amount="100",  # 支付金额,注意是字符串类型的
        subject='全球水果生鲜商城',  # 主题
        return_url=None,
        notify_url=None  # 可选, 不填则使用默认notify url
    )

    # https://openapi.alipaydev.com/gateway.do
    # return redirect('https://openapi.alipaydev.com/gateway.do?' + order_string)

13、集成到项目中
(1)页面
支付宝支付(pc)_第10张图片
支付宝支付(pc)_第11张图片

(2)视图
①支付宝函数

#支付宝内容
def pay(order_num, total):
    # 应用的私钥
    app_private_key_string = """-----BEGIN RSA PRIVATE KEY-----
            MIIEpQIBAAKCAQEA2RytcQwVHDi5ra/xpHm2rQ0TjXEDBNZ
            YmUVmNbyd12hnbCBNUAP3m/6QgzeSe2Y7wPKKmp1mb+BVVKZQua8
            VoSohRlRE59GyK1f0IPCR1Tusjwkuw+E6dsarPRCSNJ1knJEsyjBwDCsG/tx/
            nUHbhrbWss0OoGb+h3TMjMWT82fXX3G0aJpA+uRe6n18iR/6q/1vgWA1Har
            cTZfGlTAOox3/fj7bDluOr9cFCTVNPLahN3P/9qvwCQGE7pouBB8qHkAhutT
            V3wG8qf8BW2tMag0F9Yb82yvQVnbt1AARh36VrUGV555vWWkOaSrGViJhKKpZc
            ftiQmgbH6VUw1iAtwIDAQABAoIBAQCj6ViwTlTpt5HHJVwjtphJ0eN/F2EWF9VP
            8esWis4HwJTENybnT/dyWCA2UJBQhYB7TSGDVW3u7Mi/Xcj4Rsznnez9McXL1q7
            StlZjP/x5pdHDM0WRMMt4vMivE4GuI13ALoJIK4WMSje/1iyqRvw7mnJBDzsJsnhb
            P7zmUow+aDTSx5uDHqwvZuGzAvpAvkexVqIouTOJ+gkcaJAcoQpiLuvuldV+/CIdh
            H55SFAM5FQH8MXhs4wFeX8+9keJ+DahF37Tl3Dk90lzdeOV/NBbHdhqjdh9WthS
            sO3S6MUxx2vIAFLZ9BXKsPE873JePAqY5vUO8XwDeUHCMV7wYX4RAoGBAPlNpKVn
            Wapwwh+Ri1kJz3NZBDAJ7ik0g5plUAKKBdzX3LZYcqTjilmgRIBOub0sDx76rTUd40
            cbiv9XOa6qXcyNHSer+yEMa0v5gRayEBffId9xoVandnJdGcDRWh2IovtVr7cQWwYt
            TJCCoG0N2qSShnwoiqnnE2ORbJ19SkZNAoGBAN7xqwsJr1tjzJSq43Aa8jwtvV3Pcs+Y
            qH+uzHdebfK8sEc0a7Hvk57Uc2w0a3mahOu79cX7FIXSX9MrC7vv4slbC/vfptKSM4VsS
            phn2eT6G20rZjp+UjwOPWTz4OfC5ewS5lOjP8+fycUdyPfjZ80I9C5NWVwUEVr0L2riw
            O0TAoGBAJ/g8D73CaTpjUTLhQ8D4Jt7sdjQ9YBNYjyozmfidiIL+UqjbFpcZkRWKLgURL
            +RutzGLEBNO41Fl51rJd3i+iaNNO5VN8cY/NKLXwLcDMASY9m1OJ+m46oyFKr4V49rKg
            J/ECIZhhZvBJQEgbeOU/KI2WBUa6DMRT3sbDiPugtdAoGAFaYqEdxS5vD+AMHHbC9xrl5S
            sTVf1MDiO+PhfwXwb03bYE661oCJOTcwLiW4+goDNlD+QMds6sHPBFZ9wTY+M4df1om
            VTYVc82vcoogTJKUU3LkGKE/z9Fj9pTv5bwgCA7aiHyUPIT5G/Zky1r+CaOqhq41fTA220b
            o8kIiMa3kCgYEAgDRVKzhz1ueqIOsA6XcdHlhpSoUKmzwHeE2sD00kWVyQkKGN2/TczZ+h0
            seEM20M3C7tRVJjaN5rgfNg7biEtCxqvQgiWKchb8wfPm6Tg7Zlw9njeKBbQzq7YkO7iCLI
            KZoygTqx65b/gYx8QjSeZ1EoS532ndDztx+RmITuJbE=
            -----END RSA PRIVATE KEY-----
        """
    # 支付宝的公钥
    alipay_public_key_string = """-----BEGIN PUBLIC KEY-----
            MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAubtn6v/fRxicLhOdTijJ0xqXLZflHjvq31Y13NId7Xjs5w7vHid
            dCrml0k69iD0COo/1Ke8AJLVajwh9Zj/ZtAjjRjiK9cRW4SgTRlTm3Bnbqm5aZucmunOwgJfeCohgLTkFIwOOPbSb93u
            +nwvMTWAayiblm3aHGmZuBxb0EsUP5IBg1XCs26Am5gV8nWZ87cxDrF1C36i94kbDpmhFubicEIw
            +NMRuyKmWObrTZmxugv2AcM4lgM1LVz+Qa4H1mj0KQl4CjRHMlImSrznsvWq+5HUbIxMA7VUStA
            +mpJHFG4azb1jWOlatK984vXXgfLqRSH/gu2lkRA9gEGc36wIDAQAB
            -----END PUBLIC KEY-----
        """
    alipay = AliPay(
        appid="2016092800613099",
        app_notify_url=None,  # 默认回调url
        app_private_key_string=app_private_key_string,
        # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
        alipay_public_key_string=alipay_public_key_string,
        sign_type="RSA2",  # RSA 或者 RSA2
        debug=True  # 默认False
    )

    # 电脑网站支付,需要跳转到https://openapi.alipay.com/gateway.do? + order_string
    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no=order_num,  # 订单id
        total_amount=total,  # 支付金额,注意是字符串类型的
        subject='全球水果生鲜商城',  # 主题
        return_url=None,
        notify_url=None  # 可选, 不填则使用默认notify url
    )

    # 返回支付页面网址
    return 'https://openapi.alipaydev.com/gateway.do?' + order_string

②视图函数

#支付宝支付
def alipay_method(request):
    # 1.获取提交过来的 订单号和总价钱
    order_num = request.GET.get('order_num')  # 订单号
    total = request.GET.get('total')  # 商品总价
    # 2.调用支付宝
    url = pay(order_num, total)
    # 3.返回页面
    return redirect(url)


(3)路由

path('alipay_method/', views.alipay_method),  # 支付宝支付

你可能感兴趣的:(django框架)