Thinkphp5 使用Paypal 支付

4 篇文章 0 订阅

1,首先前往官网https://www.paypal.com 创建一个账户,我创建的是一个企业账户

2,前往paypal开发者平台https://developer.paypal.com/ 进行创建应用(使用谷歌自带的翻译,把网页翻译过来....)

3,点击之后出现如下界面,沙盒开发人员帐户 是在创建账号的时候默认生成的两个账号其中一个企业账号,还有一个是个人账号,供测试使用的,还可以多创建几个。

3,前往 https://github.com/paypal/PayPal-PHP-SDK 下载Paypal SDK,我只用 lib 里面的 PayPal,把PayPal 文件夹移动到 tp5 根目录extend 里面,放到这里就不需要动了。

4,之后我创建了一个控制器,点击支付则调用

代码:

<?php

namespace app\home\controller;

use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Exception\PayPalConnectionException;
use PayPal\Rest\ApiContext;
use PayPal\Api\PaymentExecution;
use Think\DB;

class Paypal
{   
    // sandbox 开发环境
    // const clientId = 'xxxxxxxxxx';
    // const clientSecret = 'xxxxxxxx';

    // live 生产环境
    const clientId = 'xxxxxxxxxx';
    const clientSecret = 'xxxxxxxxxxxxxx';

    const accept_url = 'https://baidu.com';//返回地址
    const Currency = 'USD'; //币种 美元
    protected $PayPal;
    protected $order_number;

    public function __construct()
    {
        $this->PayPal = new ApiContext(
            new OAuthTokenCredential(
                self::clientId,
                self::clientSecret
            )
        );
        //如果是沙盒测试环境不设置,请注释掉
        // $this->PayPal->setConfig(
        //     array(
        //         'mode' => 'live',
        //     )
        // );
    }

    public function index()
    {

        // 这里可以把数据传过来,也可以查出来
        // code ....
            // 商品名称
            $product = '商品商品';
            if (empty($product)) {
                $this->error('Goods cannot be empty.');
            }
            // 总价
            $price = '100';
            if (empty($price)) {
                $this->error('Prices cannot be empty.');
            }
            // 运费 
            $shipping = input('shipping', 0);

            // 商品描述
            $description = '商品描述';
            if (empty($description)) {
                $this->error('Description cannot be empty.');
            }
            $this->pay($product, $price, $shipping, $description);

    }

    /**
     * @param
     * $product 商品
     * $price 价钱
     * $shipping 运费
     * $description 描述内容
     */
    public function pay($product, $price, $shipping = 0, $description)
    {
        $paypal = $this->PayPal;

        $total = $price + $shipping;//总价

        $payer = new Payer();
        $payer->setPaymentMethod('paypal');

        $item = new Item();
        $item->setName($product)->setCurrency(self::Currency)->setQuantity(1)->setPrice($price);

        $itemList = new ItemList();
        $itemList->setItems([$item]);

        $details = new Details();
        $details->setShipping($shipping)->setSubtotal($price);

        $amount = new Amount();
        $amount->setCurrency(self::Currency)->setTotal($total)->setDetails($details);

        $transaction = new Transaction();
        $transaction->setAmount($amount)->setItemList($itemList)->setDescription($description)->setInvoiceNumber(uniqid());

        $redirectUrls = new RedirectUrls();
        // $redirectUrls->setReturnUrl(self::accept_url . '?success=true')->setCancelUrl(self::accept_url . '/?success=false');
        $redirectUrls->setReturnUrl(self::accept_url .'?success=true')->setCancelUrl(self::accept_url .'?success=false');

        $payment = new Payment();
        $payment->setIntent('sale')->setPayer($payer)->setRedirectUrls($redirectUrls)->setTransactions([$transaction]);
        try {
            $payment->create($paypal);
        } catch (PayPalConnectionException $e) {
            echo $e->getData();
            die();
        }

        $approvalUrl = $payment->getApprovalLink();
        header("Location: {$approvalUrl}");
    }

    /**
     * 回调
     */
    public function Callback()
    {
        // 修改订单状态
        $success = trim($_GET['success']);

        if ($success == 'false' && !isset($_GET['paymentId']) && !isset($_GET['PayerID'])) {
            echo "<script>alert('取消支付。');
                history.go(-4);
            </script>";
            exit();
        }

        $paymentId = trim($_GET['paymentId']);
        $PayerID = trim($_GET['PayerID']);

        if (!isset($success, $paymentId, $PayerID)) {
            echo 'Failure to pay。';
            exit();
        }
 
        if ((bool)$_GET['success'] === 'false') {
            echo 'Failure to pay,payment ID【' . $paymentId . '】,Payer ID【' . $PayerID . '】';
            exit();
        }

        $payment = Payment::get($paymentId, $this->PayPal);

        $execute = new PaymentExecution();

        $execute->setPayerId($PayerID);

        try {
            $payment->execute($execute, $this->PayPal);
        } catch (Exception $e) {
            echo $e . ',支付失败,支付ID【' . $paymentId . '】,支付人ID【' . $PayerID . '】';
            exit();
        }

        // 到这里就支付成功了,可以修改订单状态,需要自己传参数,可以在成功回调地址后面加
        // code....
        

        // 可以跳转订单页面
        $url = 'http://www.baidu.com';
        header("Location:$url");die;
    }
}

代码end;

5,测试 id和key在应用里面,(不知道是我电脑原因还是其他原因,打开自动翻译之后点击显示秘钥没有反应。)

6,Paypal还是挺好用的,如果出现id key不正确的错误,可以尝试重新创建一个应用(一般不会错的),
如果一直调用不出来支付页面可能是网络原因

7,测试环境完成,接下来就到了切换到生产环境了,找了半小时没有找到生产环境怎么开启 ...
(1)点击下图live(生活) 则是生产环境的id和key,在代码里更换就可以了

(2)在代码中打开生产环境即可

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
thinkphp5 可以通过支付宝开放平台的 SDK 来对接支付支付功能。首先,我们需要在 thinkphp5 的项目中引入支付宝 SDK,可以通过 Composer 来安装 SDK,或者手动下载 SDK 的包来引入。接下来,我们需要在项目中配置支付支付相关的参数,如支付宝的AppID、App 私钥、支付宝公钥等。这些参数可以在支付宝开放平台开发者中心进行获取。 在配置好参数后,我们可以在 thinkphp5 的控制器中编写代码来实现支付支付的功能。一般来说,支付支付的流程包括生成支付订单、跳转到支付页面、支付成功回调等步骤。我们可以通过调用支付宝 SDK 提供的相关方法来完成这些步骤。 首先,我们可以使用 SDK 提供的方法来生成支付订单,并将订单信息保存在数据库或其他地方。然后,我们可以使用 SDK 提供的方法来生成支付表单,将用户重定向到支付宝的支付页面上。用户在支付页面上完成支付后,支付宝会将支付结果返回给我们的网站,我们可以通过配置支付宝回调地址,并编写回调方法来接收支付结果。 在接收到支付结果后,我们可以根据支付结果更新订单的状态,并做相应的业务逻辑处理。例如,如果支付成功,我们可以将订单状态改为已支付,并向用户展示支付成功的页面。 综上所述,通过引入支付宝 SDK,配置相关参数,编写相应的代码,我们就可以在 thinkphp5 中实现支付支付功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值