Paypal与Laravel 5.3

时间:2017-01-17 17:51:23

标签: php laravel paypal

你们知道如何用laravel 5.3实现paypal吗?我已经找了一些信息,但我不确定。如果您了解任何教程,请告诉我 谢谢!

1 个答案:

答案 0 :(得分:1)

根据我的个人经验,我使用了paypal php SDK link here

我还构建了一个包含我将在这里分享的集成的帮助器。这是一个完整的流程,我建议您阅读这些步骤。

class PaypalHelper {

  private $apiContext;
  protected $payer;
  protected $transaction;
  protected $payment;
  protected $redirectUrls;

  /**
   * Instantiate a new helper instance.
   *
   * @return void
   */
  public function __construct() {
    $this->payer = new Payer();
    $this->transaction = new Transaction();
    $this->redirectUrls = new RedirectUrls();
    $this->payment = new Payment();
    $this->redirectUrls = new RedirectUrls();

    $this->apiContext = new \PayPal\Rest\ApiContext(
      new \PayPal\Auth\OAuthTokenCredential(
        config('paypal')['client_id'],
        config('paypal')['secret']
        )
    );

    $this->apiContext->setConfig(
      config('paypal')['settings']
    );
  }

  /**
   * Get api context.
   *
   * @return void
   */
  public function getApiContext() {
    return $this->apiContext;
  }

  /**
   * Set payment method.
   *
   * @return void
   */
  public function setPaymentMethod($method) {
    $this->payer->setPaymentMethod($method);
  }

  /**
   * Set funding instrument.
   *
   * @return void
   */
  public function setFundingInstrument($fi) {
    $this->payer->setFundingInstruments([$fi]);
  }

  /**
   * Set redirect urls.
   *
   * @return void
   */
  public function setRedirectUrls($return_url, $cancel_url) {
    $this->redirectUrls
      ->setReturnUrl($return_url)
      ->setCancelUrl($cancel_url);
  }

  /**
   * Build product list.
   *
   * @return void
   */
  public function buildProductList($products) {
    $productList = new ItemList();

    $products->each(function($product, $key) use ($productList) {
      $item = new Item();
      $description = '';

      $product->attributes->each(function($attribute, $key) use (&$description) {
        if(!is_object($attribute)) {
          if($key !== 0) {
            $description = $description . ' | ' . $attribute;
          } else {
            $description = $attribute;
          }
        }
      });

      $item->setName($product->name)
        ->setDescription($description)
        ->setCurrency('EUR')
        ->setQuantity($product->quantity)
        ->setSku($product->reference)
        ->setPrice($product->price);

      $productList->addItem($item);
    });

    return $productList;
  }

  /**
   * Calculate details
   *
   * @return Response
   */
  public function calculateDetails($totals) {
    $details = new Details();

    $details->setShipping(0)
      ->setTax(0)
      ->setSubtotal($totals['total']);

    return $details;
  }

  /**
   * Calculate total amount
   *
   * @return Response
   */
  public function calculateTotalAmount($totals, $details) {
    $amount = new Amount();

    $total = $details->getTax() + $details->getShipping() + $totals['total'];

    $amount->setCurrency('EUR')
      ->setTotal($total)
      ->setDetails($details);

    return $amount;
  }

  /**
   * Build transaction.
   *
   * @return void
   */
  public function buildTransaction($amount, $productList) {
    $this->transaction
      ->setAmount($amount)
      ->setItemList($productList)
      ->setDescription('')
      ->setInvoiceNumber(uniqid());

    return $this->transaction;
  }

  /**
   * Build payment.
   *
   * @return void
   */
  public function buildPayment() {
    $this->payment
      ->setIntent('sale')
      ->setPayer($this->payer)
      ->setRedirectUrls($this->redirectUrls)
      ->setTransactions([$this->transaction]);

    return $this->payment;
  }
}

我会查看他们在文档中的示例。如果您想查看该项目,可以查看here。希望这会有所帮助。