thinkphp6如何接入paypal 支付
thinkphp6如何接入paypal 支付。
·
thinkphp6如何接入paypal 支付
命令行下载:
composer require paypal/rest-api-sdk-php
这样thinkphp6就接入PayPal支付了
PHP源码实例:
public function zhifu()
{
//palpay的clientId值
$clientId = 'clientId值';
//palpay的clientSecret值
$clientSecret = 'clientSecret值';
//获取请求的价格数据
$nums = Request::param('num');
//获取提交金钱数
$nums = substr($nums,1);
//加载三方类配置
require __DIR__ . '/../../vendor/autoload.php';
//实例化PayPal三方类
$paypal = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
//上线
$clientId,//clientId
$clientSecret//clientSecret
)
);
// 给三方类添加配置
$paypal->setConfig(
array(
//这里很重要没上线用沙盒,上线用上线
// 'mode' => 'sandbox',沙盒
'mode' => 'live',//上线
//下面是日志配置
'log.LogEnabled' => true,
'log.FileName' => 'PayPal.log',
'log.LogLevel' => 'DEBUG'
)
);
//收款方式
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
//收款金额与货币类型
$amount = new \PayPal\Api\Amount();
$amount->setTotal($nums);
$amount->setCurrency('USD');
//货物描述,可填可不填
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount)->setDescription("支付描述内容");
//不知道有什么用,好像没什么用,但勿动。
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl("https://example.com/your_redirect_url.html")
->setCancelUrl("https://example.com/your_cancel_url.html");
$payment = new \PayPal\Api\Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions(array($transaction))
->setRedirectUrls($redirectUrls);
// After Step 3
try {
//支付并返回信息
$payment->create($paypal);
//打印返回信息
dump($payment)
//echo $payment->getApprovalLink();打印跳转地址
//跳转支付界面
return redirect($payment->getApprovalLink());
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
// This will print the detailed information on the exception.
//REALLY HELPFUL FOR DEBUGGING
//打印错误信息
dump($ex->getData());
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)