tp5实现paypal支付

1.申请账号

https://www.paypal.com,申请商家账号
然后进入开发者网址

2.使用sanbox测试




sanbox账号详情密码修改,点击对应email ->profile

2.2创建app,获取clientId,clientSecret,https://developer.paypal.com/developer/applications


3.支付代码

copy官网的文档
<?php
namespace app\index\controller;

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;


class Index
{
    
    public function index()
    {
        
        $clientId = 'xxxx';
        $clientSecret = 'xxxx';
        $apiContext = new ApiContext(
            new OAuthTokenCredential(
                $clientId,
                $clientSecret
            )
        );
        $apiContext->setConfig(
            array(
                'mode' => 'sandbox',
                'log.LogEnabled' => true,
                'log.FileName' => '../PayPal.log',
                'log.LogLevel' => 'DEBUG', // PLEASE USE `INFO` LEVEL FOR LOGGING IN LIVE ENVIRONMENTS
                'cache.enabled' => true,
                // 'http.CURLOPT_CONNECTTIMEOUT' => 30
                // 'http.headers.PayPal-Partner-Attribution-Id' => '123123123'
                //'log.AdapterFactory' => '\PayPal\Log\DefaultLogFactory' // Factory class implementing \PayPal\Log\PayPalLogFactory
            )
        );
        

        // ### Payer
        // A resource representing a Payer that funds a payment
        // For paypal account payments, set payment method
        // to 'paypal'.
        $payer = new Payer();
        $payer->setPaymentMethod("paypal");
        
        // ### Itemized information
        // (Optional) Lets you specify item wise
        // information
        $item1 = new Item();
        $item1->setName('商品1')
            ->setCurrency('USD')
            ->setQuantity(1)
            ->setSku("123123") // Similar to `item_number` in Classic API
            ->setPrice(7.5);
        $item2 = new Item();
        $item2->setName('商品2')
            ->setCurrency('USD')
            ->setQuantity(5)
            ->setSku("321321") // Similar to `item_number` in Classic API
            ->setPrice(2);
        
        $itemList = new ItemList();
        $itemList->setItems(array($item1, $item2));
        
        // ### Additional payment details
        // Use this optional field to set additional
        // payment information such as tax, shipping
        // charges etc.
        $details = new Details();
        $details->setShipping(1.2)  
            ->setTax(1.3) 
            ->setSubtotal(17.50);
        
        // ### Amount
        // Lets you specify a payment amount.
        // You can also specify additional details
        // such as shipping, tax.
        $amount = new Amount();
        $amount->setCurrency("USD")
            ->setTotal(20)
            ->setDetails($details);
        
        // ### Transaction
        // A transaction defines the contract of a
        // payment - what is the payment for and who
        // is fulfilling it. 
        $transaction = new Transaction();
        $transaction->setAmount($amount)
            ->setItemList($itemList)
            ->setDescription("商品描述")
            ->setInvoiceNumber(uniqid());
        
        // ### Redirect urls
        // Set the urls that the buyer must be redirected to after 
        // payment approval/ cancellation.
        $baseUrl = 'http://'.$_SERVER["HTTP_HOST"];
        $redirectUrls = new RedirectUrls();
        $redirectUrls->setReturnUrl("$baseUrl/public/index.php/index/Index/success")
            ->setCancelUrl("$baseUrl/public/index.php/index/Index/cancel");
        
        // ### Payment
        // A Payment Resource; create one using
        // the above types and intent set to 'sale'
        $payment = new Payment();
        $payment->setIntent("sale")
            ->setPayer($payer)
            ->setRedirectUrls($redirectUrls)
            ->setTransactions(array($transaction));
        
        
        // For Sample Purposes Only.
        $request = clone $payment;
      
        // ### Create Payment
        // Create a payment by calling the 'create' method
        // passing it a valid apiContext.
        // (See bootstrap.php for more on `ApiContext`)
        // The return object contains the state and the
        // url to which the buyer must be redirected to
        // for payment approval
        try {
            $payment->create($apiContext);
        } catch (Exception $ex) {
            // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
            ResultPrinter::printError("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $request, $ex);
            exit(1);
        }
        
        // ### Get redirect url
        // The API response provides the url that you must redirect
        // the buyer to. Retrieve the url from the $payment->getApprovalLink()
        // method
        $approvalUrl = $payment->getApprovalLink();
        
        
        
        
    }
    
    
    public function success()
    {
        echo 'success';
    }
    
    public function cancel()
    {
        echo 'cancel';
    }
}

show



4.通知(页面级,异步级)

页面级的上面代码已有,异步 webhook 后面贴上,其实文档写的相当详细

5.相关资料


ps:另外发现php packagist.org也是好东西啊


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
TP5中使用PayPal,你需要下载PayPal PHP SDK并将其移动到TP5根目录的extend文件夹中。然后,你可以使用PayPal SDK提供的功能来处理支付事务。 以下是使用TP5PayPal SDK进行支付的示例代码: ```php // 引入PayPal SDK use PayPal\Rest\ApiContext; use PayPal\Auth\OAuthTokenCredential; use PayPal\Api\Amount; use PayPal\Api\Payer; use PayPal\Api\Payment; use PayPal\Api\RedirectUrls; use PayPal\Api\Transaction; // 创建API上下文 $apiContext = new ApiContext( new OAuthTokenCredential( 'YOUR_CLIENT_ID', // 你的PayPal客户端ID 'YOUR_CLIENT_SECRET' // 你的PayPal客户端密钥 ) ); // 设置支付金额 $amount = new Amount(); $amount->setTotal('10.00'); // 设置支付总额 $amount->setCurrency('USD'); // 设置货币类型 // 设置支付者 $payer = new Payer(); $payer->setPaymentMethod('paypal'); // 设置支付方式为PayPal // 创建支付 $payment = new Payment(); $payment->setIntent('sale'); // 设置支付意图为销售 $payment->setPayer($payer); $payment->setTransactions([ (new Transaction())->setAmount($amount) ]); // 设置重定向URL $redirectUrls = new RedirectUrls(); $redirectUrls->setReturnUrl('http://example.com/success'); // 设置支付成功后的重定向URL $redirectUrls->setCancelUrl('http://example.com/cancel'); // 设置支付取消后的重定向URL $payment->setRedirectUrls($redirectUrls); // 创建支付并获取支付链接 $payment->create($apiContext); $approvalUrl = $payment->getApprovalLink(); // 重定向用户到支付链接 header("Location: $approvalUrl"); exit; ``` 请注意,上述代码中的`YOUR_CLIENT_ID`和`YOUR_CLIENT_SECRET`需要替换为你在PayPal开发者网站上创建的应用程序的客户端ID和密钥。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值