这是我的解决方案,它适用于 yii2 高级模板!
我已将此类 marciocamello/yii2-paypal/PayPal.php 从供应商文件夹移至 common/component/PayPal.php
您必须在组件部分下的 frontend\config\main.php 中“包含”common\components\paypal:
'components' => [
'paypal'=> [
'class' => 'common\components\Paypal',
'clientId' => 'your id you can find it over your paypal develpoer account. Ypu ah to create an application',
'clientSecret' => 'your id you can find it over your paypal develpoer account. Ypu ah to create an application',
'isProduction' => false,
// This is config file for the PayPal system
'config' => [
'http.ConnectionTimeOut' => 30,
'http.Retry' => 1,
'mode' => \common\components\Paypal::MODE_SANDBOX,
'log.LogEnabled' => YII_DEBUG ? 1 : 0,
'log.FileName' => '@runtime/logs/paypal.log',
'log.LogLevel' => \common\components\Paypal::LOG_LEVEL_INFO,
]
],
],
在第 11 行的 PPHttpConfig.php 中,我更改了以下行
//CURLOPT_SSLVERSION => 3,
CURLOPT_SSLVERSION => 'CURL_SSLVERSION_TLSv1',
在第 48-52 行的 PPLoggingManager.php 中,我对日志路径进行了硬编码
if($this->isLoggingEnabled) {
$this->loggerFile = $_SERVER['DOCUMENT_ROOT'].'/domain/frontend/runtime/logs/paypal.log';//($config['log.FileName']) ? $config['log.FileName'] : ini_get('error_log');
$loggingLevel = strtoupper($config['log.LogLevel']);
$this->loggingLevel = (isset($loggingLevel) && defined(__NAMESPACE__."\\PPLoggingLevel::$loggingLevel")) ? constant(__NAMESPACE__."\\PPLoggingLevel::$loggingLevel") : PPLoggingManager::DEFAULT_LOGGING_LEVEL;
}
在站点控制器中,我调用了组件函数
echo '<pre/>';
print_r(Yii::$app->paypal->payDemo());
exit();
我收到了带有重定向 url、transactionId 等的成功响应。
-------------- 400 ERROR Debuging ----------
我总是收到 400 错误,因为我的价格格式有问题,我使用 number_format 函数修复了它。
我稍微修改了 payDemo() 函数。
$amount = 16;
$formattedAmount = number_format($amount,2);
$payer = new Payer();
$payer->setPayment_method("paypal");
$item1 = new Item();
$item1->setName('Ground Coffee 40 oz')
->setCurrency('USD')
->setQuantity(1)
->setPrice($formattedAmount);
$item2 = new Item();
$item2->setName('Granola bars')
->setCurrency('USD')
->setQuantity(1)
->setPrice($formattedAmount);
$itemList = new ItemList();
$itemList->setItems(array($item1, $item2));
// $amountDetails = new Details();
// $amountDetails->setSubtotal('7');
// $amountDetails->setTax('0.00');
// $amountDetails->setShipping('0.00');
$amount = new Amount();
$amount->setCurrency('USD');
$amount->setTotal(number_format((2*$formattedAmount),2));
// $amount->setDetails($amountDetails);
$transaction = new Transaction();
$transaction->setDescription("creating a payment");
$transaction->setItemList($itemList);
$transaction->setAmount($amount);
//$baseUrl = getBaseUrl();
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturn_url("https://devtools-paypal.com/guide/pay_paypal/php?success=true");
$redirectUrls->setCancel_url("https://devtools-paypal.com/guide/pay_paypal/php?cancel=true");
$payment = new Payment();
$payment->setIntent("sale");
$payment->setPayer($payer);
$payment->setRedirect_urls($redirectUrls);
$payment->setTransactions(array($transaction));
return $payment->create($this->_apiContext);
如果您仍然收到 400 或 40x 错误,您可以在第 107 行的 PPHttpConnection.php 中打印带有错误消息等的整个 PayPal 响应。
$ex->setData($result);
// echo '<pre>';
// print_r($ex);exit;
throw $ex;