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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何使用Yii2的PayPal扩展集成yii2中的支付网关脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
何在yii2中使用paypal扩展.我正在使用此链接 https://github.com/marciocamello/yii2-paypal.
我安装了扩展名,并在配置文件添加代码.

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

谢谢

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

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

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

创建一个PHP文件(例如CashMoney),具有以下内容

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;
  }
}

这足以开始.您可以选择稍后添加特定于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
  ],...
]

现在我们的付款组件注册为CashMoney,我们可以使用Yii :: $app-> cm访问它们.很酷啊

3.进行API调用

到07.01在Yii2,

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

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;
    }
    ...
  }

  ...

}

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

一旦你得到连接,你应该能够执行其他任务.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何使用Yii2的PayPal扩展集成yii2中的支付网关全部内容,希望文章能够帮你解决php – 如何使用Yii2的PayPal扩展集成yii2中的支付网关所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。