• payPal跨境支付


    三方支付前言

    • 一旦回调不成功支付宝不光用支付id可以查询支付状态,也可以用订单id查询
    • 微信支付为什么要有钱包
      • 为了解耦(减少耦合度,银行对接充值,购买逻辑和钱包结合,下方三个都改成充值业务,简化操作)
    • 与支付宝,微信相比,Paypal的优势是实时获取汇率

    1 payPal跨境支付平台

    • Python3+Django2集成PayPal(贝宝)跨境支付三方接口以及订单查询和退款业务

    1.1 前言

    ​ 如果您所在的公司涉及外贸或者跨境支付业务,那一定听说过大名鼎鼎的PayPal,总的来说,PayPal在跨国贸易里的优势还是比较大的,作为一种外贸支付方式,目前在国际贸易支付服务中倍受亿万用户追捧,是全球商户和消费者最受欢迎的电子支付方式之一,在跨境交易中有着超过90%的卖家和超过85%的买家认可并正在使用PayPal电子支付业务。当然,PayPal国际业务体量如此惊人,肯定不是毫无原因的。

    ​ PayPal支付的优势就是其业务网络遍布全球。目前PayPal的庞大网络覆盖了全球200多个国家,可提供20多种语言服务,并接受100多种货币付款和56种货币提现。同时,还允许在账户中持有25种货币余额。换句话说,只要付款人拥有一个PayPal账户,他就拥有了在200多个国家进行电子支付购物,并在需要服务的时候享受到母语支持的各种便捷服务。

    1.2 网址

    1.2.1 官网
    https://www.paypal.com
    
    1.2.2 开发者平台
    https://developer.paypal.com/developer/accounts/
    

    1.3 操作流程

    1.3.1 沙盒
    • 位置

    • 账户信息

    1.3.2 我的应用
    • 应用

    • 找到 secret key 以及 client ID

    • 账户详细信息(一定要改密码,不然虚拟支付支付不了)

    1.4 具体代码

    • django端代码
    1.4.1 utils.py
    import datetime
    import random
    
    import jwt
    import paypalrestsdk
    from django.conf import settings
    from django.http import HttpResponse
    from django.shortcuts import redirect
    
    # 生成订单号
    from MyAuthorization import decodeToken
    from walletapp.models import OrderModel
    
    
    def create_trade_no():
        d = datetime.datetime.now()
        base = 'Maple'
        time_str = '%04d%02d%02d%02d%02d%02d' % (d.year, d.month, d.day, d.hour, d.minute, d.second)
        rand_num = str(random.randint(10000, 99999))
        return base + time_str + rand_num
    
    
    def payment(request):
        total = int(request.GET.get('total'))
        token = request.GET.get('token')
        paypalrestsdk.configure({
            "mode": "sandbox",              # sandbox代表沙盒
            "client_id": "AfWM56tDHEiAIWWDAP_CjsK3CWuZnmBcjCZczaM3gpGuvhoTO2UiWgn4rCmgJdEmAEKjsIC6w2VCENuM",
            "client_secret": "EFVWfHa5fMiVT8TIqiJPn14O5y7Rp_yGmiRnTF3K_LYGqg4DtfF5AsLy83EWy_owdrU5SySROx2JI_41",
        })
    
        payment = paypalrestsdk.Payment({
            "intent": "sale",
            "payer": {
                "payment_method": "paypal"},
            "redirect_urls": {
                "return_url": "http://localhost:1594/wallet/execute/?user_token=" + token + '&total=' + str(total),  # 支付成功跳转页面
                "cancel_url": "http://localhost:8080/pay_for"},  # 取消支付页面
            "transactions": [{
                "amount": {
                    "total": total,
                    "currency": "USD"},
                "description": "可可爱订单支付"}]})
    
        if payment.create():
            print("Payment created successfully")
            for link in payment.links:
                if link.rel == "approval_url":
                    approval_url = str(link.href)
                    print("Redirect for approval: %s" % (approval_url))
                    return redirect(approval_url)
        else:
            print(payment.error)
            return HttpResponse("支付失败")
    
    
    
    def payment_execute(request):
    
        paymentid = request.GET.get("paymentId")    # 订单id
        payerid = request.GET.get("PayerID")        # 支付者id
    
        payment = paypalrestsdk.Payment.find(paymentid)
    
        if payment.execute({"payer_id": payerid}):
            print("Payment execute successfully")
            # 生成订单号
            number = create_trade_no()
            print(123, number)
            token = request.GET.get('user_token')
            user_info = jwt.decode(token, settings.SECRET_KEY)
            money = request.GET.get('total')
            OrderModel.objects.create(user_id=user_info.get('user_id'), order_id=number, pay_id=paymentid, money=int(money), method='3')
            return HttpResponse("支付成功")
        else:
            print(payment.error)        # Error Hash
            return HttpResponse("支付失败")
    
    1.4.2 urls.py
    from django.urls import path
    
    from walletapp.utils import payment, payment_execute
    
    urlpatterns = [
        path('paypal/', payment),            # 支付地址
        path('execute/', payment_execute),   # 回调
        # path('refund/', payment_execute),   # 退款
    ]
    
    1.4.3 前端测试代码
    <template>
        <div>
            支付金额:<a-input v-model="money"></a-input>
            <a-button @click="payPal">支付</a-button>
        </div>
    </template>
    
    <script>
    import axios from 'axios'
    // import { get_paypal } from '@/http/apis'
    export default {
        data() {
            return {
                money:'',
                token:localStorage.getItem('token')
            }
        },
        methods: {
            payPal(){
                window.location.href = 'http://127.0.0.1:1594/wallet/paypal/?total='+this.money+'&token='+this.token
            }
        },
        created() {
    
        }
    }
    </script>
    
    <style scoped>
    
    </style>
    
    

    1.5 效果

    1.5.1 支付页面

    1.5.2 回调

    1.5.3 获取信息
    id1: PAYID-L7UHOHQ2UP62622M5482824G 
    id2: 69KKTUUH2AXXE 
    id3: {
        'id': 'PAYID-L7UHOHQ2UP62622M5482824G', 
        'intent': 'sale', 
        'state': 'created', 
        'cart': '3YB50174X4033431P', 
        'payer': {
            'payment_method': 
            	'paypal', 
            	'status': 'VERIFIED', 
            		'payer_info': {
                        'email': 'sb-5ib4l4352663@personal.example.com', 
                        'first_name': 'John', 
                        'last_name': 'Doe', 
                        'payer_id': '69KKTUUH2AXXE', 
                        'shipping_address': {
                            'recipient_name': 'Doe John', 
                            'line1': 'NO 1 Nan Jin Road', 
                            'city': 'Shanghai', 
                            'state': 'Shanghai', 
                            'postal_code': '200000', 
                            'country_code': 'C2'
                        }, 
                 'country_code': 'C2'
                    }
        }, 
        	'transactions': [{
                'amount': {'total': '5.00', 'currency': 'USD'}, 
                'payee': {'merchant_id': 'DQ2MFCBJPSDHN', 'email': 'sb-wmmee4354779@business.example.com'}, 
                'description': '可可爱订单支付', 
                'item_list': {
                    'shipping_address': {
                        'recipient_name': 'Doe John', 'line1': 'NO 1 Nan Jin Road', 'city': 'Shanghai', 'state': 'Shanghai', 'postal_code': '200000', 'country_code': 'C2'}}, 'related_resources': []
            }], 
        	'redirect_urls': {
                'return_url': 'http://localhost:1594/wallet/execute/?paymentId=PAYID-L7UHOHQ2UP62622M5482824G', 
                'cancel_url': 'http://localhost:3000/paypal/cancel/'
            }, 
        	'create_time': '2020-12-27T11:59:25Z', 
        	'update_time': '2020-12-27T11:59:55Z', 
        	'links': [{
                'href': 'https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L7UHOHQ2UP62622M5482824G', 
                'rel': 'self', 'method': 'GET'}, 
                	  {'href': 'https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L7UHOHQ2UP62622M5482824G/execute', 'rel': 'execute', 'method': 'POST'}, 
                	  {'href': 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-3YB50174X4033431P', 'rel': 'approval_url', 'method': 'REDIRECT'}
            ]}
    
    1.5.4 有效信息
    • 订单状态

    image-20201227202556449

    • 订单号

      • 根据这个可以查询交易流水
    • 订单创建时间

    image-20201227203130429

    • 价格

    image-20201227202738719

    1.5.5 查询交易流水
    • 根据pay_id 进行退款

    image-20201227223102572

    1.6 总结

        总体而言,没有什么特别的难度,整个支付流程相对支付宝来说,更加的紧凑,但是做支付安全是第一要务,就个人体验(仅是个人体验)层面来说,支付宝在安全方面做的还是要比Paypal略强一些,起码在信用卡欺诈和盗刷方面风控做的更好,在风险保障和赔付方面都有提供保险,当然由于金融环境的差异较大,并不是说Paypal的风控做的不好,只是机制不同,在国外,如果持卡人的信用卡被盗刷,一般发卡组织会让商家去承担责任,而国内只能在交易环节设置更多的验证,本质上说是要持卡人承担责任。这也是为什么支付宝的风控看起来更好的原因。
    
        最后就是关于费率问题,Paypal官方给出的费率是每笔交易收取3.9%+$0.3(根据你的交易流水,比例可以优惠,具体下限看接入者的月营业额度),不过这可是美刀,不得不说这个费率是相当的高,但是国内做境外支付的电商,一般还是要接入Paypal作为支付方式。支付宝的费率基本上在1.2%左右,具体的费率也看交易流水,有实力的下限可以做到基本没有,单纯的看费率似乎支付宝更有优势,但是别忘记了,这样对比是不科学的,因为凡是接入Paypal的都是看中覆盖外币业务的地区,费率则是投资人该考虑的问题了。
    
  • 相关阅读:
    借了个屏幕来用
    生命开始的地方
    看了STLPort的安装方法,晕了
    程序员必备的10大健康装备!
    《代码整洁之道》读书笔记
    Mockito使用
    学习Emacs的理由
    shell 脚本编程的10 个最佳实践
    MongoDB入门
    用Orgmode实践《奇特的一生》
  • 原文地址:https://www.cnblogs.com/mapel1594184/p/14199874.html
Copyright © 2020-2023  润新知