如何使用PayPal扩展为Yii2集成yii2中的支付网关

jat*_*iya 12 php paypal yii-extensions yii2

如何在yii2中使用paypal扩展.我正在使用此链接
https://github.com/marciocamello/yii2-paypal.
我安装了扩展,并在配置文件中添加了代码.

但是没有更多信息下一步该做什么.所以请帮助我.

谢谢

iGb*_*nam 16

没有必要使用扩展名.您只需安装paypal/rest-api-sdk-php软件包并执行以下步骤即可.

1.创建一个组件,将Paypal粘贴到Yii2

components@app目录中创建一个文件夹.如果您使用的是基本模板,则该文件夹与webroot; 在高级模板中,此文件夹位于您要启用付款的应用中.

CashMoney使用以下内容创建一个PHP类文件(例如)

use yii\base\Component;

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;

class CashMoney extends Component {
  public $client_id;
  public $client_secret;
  private $apiContext; // paypal's API context

  // override Yii's object init()
  function init() { 
    $this->apiContext = new ApiContext(
      new OAuthTokenCredential($this->client_id, $this->client_secret)
    );
  }

  public function getContext() {
    return $this->apiContext;
  }
}
Run Code Online (Sandbox Code Playgroud)

这足以开始.您可以选择稍后添加特定于PayPal的其他配置.

2.使用Yii2注册胶水组件

在您的app/config/main.php(或app/config/main-local.php)中,包括以下内容以注册CashMoney组件.

'components' => [
  ...
  'cm' => [ // bad abbreviation of "CashMoney"; not sustainable long-term
    'class' => 'app/components/CashMoney', // note: this has to correspond with the newly created folder, else you'd get a ReflectionError

     // Next up, we set the public parameters of the class
    'client_id' => 'YOUR-CLIENT-ID-FROM-PAYPAL',
    'client_secret' => 'YOUR-CLIENT-SECRET-FROM-PAYPAL',
    // You may choose to include other configuration options from PayPal
    // as they have specified in the documentation
  ],
  ...
]
Run Code Online (Sandbox Code Playgroud)

现在我们将我们的支付组件注册为CashMoney,我们可以使用它来访问它Yii::$app->cm.很酷,对吧?

3.进行API调用

要在Yii2中进行第一次API调用,

打开要处理付款的控制器操作,并包括以下内容

use Yii;
...
use PayPal\Api\CreditCard;
use PayPal\Exception\PaypalConnectionException;

class PaymentsController { // or whatever yours is called
  ...
  public function actionMakePayments { // or whatever yours is called
    ...
    $card = new PayPalCreditCard;
    $card->setType('visa')
      ->setNumber('4111111111111111')
      ->setExpireMonth('06')
      ->setExpireYear('2018')
      ->setCvv2('782')
      ->setFirstName('Richie')
      ->setLastName('Richardson');

    try {
      $card->create(Yii::$app->cm->getContext());
      // ...and for debugging purposes
      echo '<pre>';
      var_dump('Success scenario');
      echo $card;
    } catch (PayPalConnectionException) {
      echo '<pre>';
      var_dump('Failure scenario');
      echo $e;
    }
    ...
  }

  ...

}
Run Code Online (Sandbox Code Playgroud)

预期输出类似于PayPal文档中的输出.

一旦建立连接,您应该能够执行其他任务.


Nag*_*vin 4

这是我的解决方案,它适用于 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,
        ]
    ],
Run Code Online (Sandbox Code Playgroud)

],

在 PPHttpConfig.php 的第 11 行中,我更改了以下行

    //CURLOPT_SSLVERSION => 3,
    CURLOPT_SSLVERSION => 'CURL_SSLVERSION_TLSv1',
Run Code Online (Sandbox Code Playgroud)

在 PPLoggingManager.php 的第 48-52 行中,我对日志路径进行了硬编码

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;
    }
Run Code Online (Sandbox Code Playgroud)

在站点控制器中,我调用了组件函数

      echo '<pre/>';
  print_r(Yii::$app->paypal->payDemo());  
  exit();
Run Code Online (Sandbox Code Playgroud)

我收到了包含重定向 url、transactionId 等的成功响应。

--------------  400 ERROR Debuging ----------
Run Code Online (Sandbox Code Playgroud)

我总是收到 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);
Run Code Online (Sandbox Code Playgroud)

如果您仍然收到 400 或 40x 错误,您可以在 PPHttpConnection.php 第 107 行打印整个 PayPal 响应以及错误消息等。

    $ex->setData($result);
        // echo '<pre>';
        // print_r($ex);exit;
        throw $ex;
Run Code Online (Sandbox Code Playgroud)