PHP接入支付宝当面付

本文介绍了如何使用PHP实现支付宝当面付功能,包括配置appid、私钥和公钥,以及发起支付和支付回调的代码实现。重点展示了`IndexService`和`NotifyService`两个类的使用,强调了支付成功后异步回调地址需使用HTTPS,并需在支付宝官方设置IP白名单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、文章介绍

两个文件实现PHP接入支付宝当面付功能,包括发起支付和支付回调应答

二、准备资料

  1. appid
  2. 私钥
  3. 公钥
  4. 在支付宝官方开启IP白名单

三、支付代码

1.index.php

<?php
//支付配置
$appid = '';         //APPID
$rsaPrivateKey= '';  //私钥

//默认配置
$notifyUrl = 'https://***/notify.php';     //付款成功后的异步回调地址
$outTradeNo = date("YmdHis").uniqid();     //商品订单号

//取商品名称和金额
$payAmount = '';          //付款金额,单位:元
$orderName = '';          //订单标题

//发起支付
$aliPay = new IndexService();
$aliPay->setAppid($appid);
$aliPay->setNotifyUrl($notifyUrl);
$aliPay->setRsaPrivateKey($rsaPrivateKey);
$aliPay->setTotalFee($payAmount);
$aliPay->setOutTradeNo($outTradeNo);
$aliPay->setOrderName($orderName);
$result = $aliPay->doPay();
$result = $result['alipay_trade_precreate_response'];
if($result['code']=='10000'){
    $url = 'https://wenhairu.com/static/api/qr/?size=300&text='.$result['qr_code'];
    echo "<img src='{$url}' style='width:300px;'><br>";
    echo '二维码内容:'.$result['qr_code'];
}


//IndexService类
class IndexService
{
    protected $appId;
    protected $notifyUrl;
    protected $charset;
    //私钥值
    protected $rsaPrivateKey;
    protected $totalFee;
    protected $outTradeNo;
    protected $orderName;

    public function __construct()
    {
        $this->charset = 'utf-8';
    }

    public function setAppid($appid)
    {
        $this->appId = $appid;
    }

    public function setNotifyUrl($notifyUrl)
    {
        $this->notifyUrl = $notifyUrl;
    }

    public function setRsaPrivateKey($saPrivateKey)
    {
        $this->rsaPrivateKey = $saPrivateKey;
    }

    public function setTotalFee($payAmount)
    {
        $this->totalFee = $payAmount;
    }

    public function setOutTradeNo($outTradeNo)
    {
        $this->outTradeNo = $outTradeNo;
    }

    public function setOrderName($orderName)
    {
        $this->orderName = $orderName;
    }

    /**
     * 发起订单
 
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值