search
HomeBackend DevelopmentPHP TutorialLaravel develops Alipay payment and cash transfer methods

这篇文章主要介绍了关于Laravel 开发支付宝支付与提现转账的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

由于项目需要,所以需要开发支付宝支付与微信支付,支付部分采用了

yansongda/pay   https://packagist.org/packages/yansongda/pay  https://yansongda.gitbooks.io/pay/(文档说明地址)

第三方扩展包 , 简单配置一下就可以使用,没有遇到什么问题  

但是由于业务需要, 需要用到提现业务,也就是支付宝支付的

alipay.fund.trans.toaccount.transfer(单笔转账到支付宝账户接口)

找了很久也没有找到很简单的集成支付宝单笔转账接口的扩展包,所以没办法只能自己来用支付宝给的SDK进行开发

首先下载了支付宝服务端SDK PHP版   下载地址https://docs.open.alipay.com/54/103419/ 

然后解压添加进项目目录  app\Libs\Alipay   Libs为自己建的目录,用来装第三方类库  

之后打开composer.json文件  在autoload里面的classmap里面添加入新添加的SDK

"autoload": {        "classmap": [            "database/seeds",
            "database/factories",
            "app/Libs/Alipay-php-sdk"
        ],
        "psr-4": {            "App\\": "app/"
        },
    。。。。。。

之后再执行composer dump-autoload 这样第三方的SDK就引入成功了

然后在控制器或者自己定义的业务层中就可以直接use这个SDK了   在new  类的时候会在前面出现 \ 属于正常

然后将给出的PHP示例粘贴进自己的业务代码中  点击查看官方请求示例https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer#s4

注意事项:

  1、粘贴到自己的业务逻辑中之后  所有new的类必须删除自己手打引入    比如: AopClient() 、 AlipayFundTransToaccountTransferRequest() 否则会爆出class not found 等错误

  2、按照说明配置所有参数之后,请求会爆出 “ 文件编码:[UTF-8] 与表单提交编码:[GBK]两者不一致!”错误,只要将复制的代码中 $aop->postCharset 值的GBK修改成为UTF-8即可

  3、支付宝SDK与Laravel都自定义了encrypt与decrypt函数,所以会产生冲突,只要将支付宝SDK中的 \aop\AopClient.php \aop\AopEncrypt.php  \lotusphp_runtime\Cookie\Cokkie.php 下的这两个函数全部重新命名即可,注意所有的  包括引用函数   直接在编辑器中查找替换也可以。

然后根据自己的业务来进行修改就可以了

如果爆出 “$sign”错误,请检查自己的公钥、私钥、AppID等是否正确,如果使用沙箱测试,请将网关修改为 

<em><strong><span style="color: #ff0000">$aop->gatewayUrl =</span></strong></em><span style="color: #ff0000"> 'https://openapi.alipaydev.com/gateway.do';</span><span style="color: #ff0000"><br></span><span style="color: #ff0000"><span style="color: #ffffff">最后贴上自己的代码,欢迎指教</span></span>
/*
     *支付宝提现接口     */
    public function toTransfer($orderNo, $account, $amount, $payName, $payeeRealName, $remark)
    {        $aop = new \AopClient();        $aop->gatewayUrl = &#39;https://openapi.alipaydev.com/gateway.do&#39;;        
    $aop->appId = config(&#39;Alipay.AppId&#39;);        $aop->rsaPrivateKey = config(&#39;Alipay.PriKey&#39;);        
    $aop->alipayrsaPublicKey = config(&#39;Alipay.PubKey&#39;);        $aop->apiVersion = &#39;1.0&#39;;        
    $aop->signType = &#39;RSA2&#39;;        $aop->postCharset = &#39;UTF-8&#39;;        $aop->format = &#39;json&#39;;        
    $request = new \AlipayFundTransToaccountTransferRequest ();        $request->setBizContent("{" .
            "\"out_biz_no\":\"$orderNo\"," .
            "\"payee_type\":\"ALIPAY_LOGONID\"," .
            "\"payee_account\":\"$account\"," .
            "\"amount\":\"$amount\"," .
            "\"payer_show_name\":\"$payName\"," .
            "\"payee_real_name\":\"$payeeRealName\"," .
            "\"remark\":\"$remark\"" .
            "}");        $result = $aop->execute($request);        
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;        
        if (!empty($resultCode) && $resultCode == 10000) {            echo "成功";
        } else {            echo "失败";
        }
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

 Laravel 学习的基础知识

php bug 调试助手 debug_print_backtrace()的介绍

The above is the detailed content of Laravel develops Alipay payment and cash transfer methods. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How can you check if a PHP session has already started?How can you check if a PHP session has already started?Apr 30, 2025 am 12:20 AM

In PHP, you can use session_status() or session_id() to check whether the session has started. 1) Use the session_status() function. If PHP_SESSION_ACTIVE is returned, the session has been started. 2) Use the session_id() function, if a non-empty string is returned, the session has been started. Both methods can effectively check the session state, and choosing which method to use depends on the PHP version and personal preferences.

Describe a scenario where using sessions is essential in a web application.Describe a scenario where using sessions is essential in a web application.Apr 30, 2025 am 12:16 AM

Sessionsarevitalinwebapplications,especiallyfore-commerceplatforms.Theymaintainuserdataacrossrequests,crucialforshoppingcarts,authentication,andpersonalization.InFlask,sessionscanbeimplementedusingsimplecodetomanageuserloginsanddatapersistence.

How can you manage concurrent session access in PHP?How can you manage concurrent session access in PHP?Apr 30, 2025 am 12:11 AM

Managing concurrent session access in PHP can be done by the following methods: 1. Use the database to store session data, 2. Use Redis or Memcached, 3. Implement a session locking strategy. These methods help ensure data consistency and improve concurrency performance.

What are the limitations of using PHP sessions?What are the limitations of using PHP sessions?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools