The way to change the payment gateway after the duration

I am using eCommerce. After the payment is made, it must be taken, and after a few days it must be processed or deducted from the client's account.

Example. If the client pays using the Paypal homepage, it will be “Logged In” and after (Max. 21 days) that a particular transaction is converted to “Sale”, it will be processed.

Customers can pay by credit card. Can we use Paypal or use another payment method?

As I know: in the Paypal SDK API you need to create this payment again and then process it. But I already have a Transaction ID. So you need to create a payment again?

Paypal: how to collect an authorized payment?

http://paypal.imtqy.com/PayPal-PHP-SDK/sample/doc/payments/AuthorizationCapture.html

+4
source share
1 answer

Add to composer.json

"require": {
        "paypal/rest-api-sdk-php": "*"
    }

See sample code in yii

use Yii;
use yii\base\ErrorException;
use yii\helpers\ArrayHelper;
use yii\base\Component;
use yii\helpers\Url;

use PayPal\Api\Address;
use PayPal\Api\CreditCard;
use PayPal\Api\Amount;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\Transaction;
use PayPal\Api\FundingInstrument;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\RedirectUrls;
use PayPal\Rest\ApiContext;


class Paypal extends Component
{
    public $clientId;
    public $clientSecret;
    public $currency;
    public $returnUrl;
    public $cancelUrl;
    public $intentType;
    public $config;

    public function pay($total, $shipping, $tax, $productName, $transactionDescription)
    {

        $apiContext = new ApiContext(
            new OAuthTokenCredential(
                $this->getClientId(),     // ClientID
                $this->getClientSecret()      // ClientSecret
            )
        );

        $apiContext->setConfig(ArrayHelper::merge(
            [
                'mode'                      => 'sandbox', // development (sandbox) or production (live) mode
                'http.ConnectionTimeOut'    => 30,
                'http.Retry'                => 1,
                'log.LogEnabled'            => YII_DEBUG ? 1 : 0,
                'log.FileName'              => Yii::getAlias('@runtime/logs/paypal.log'),
                'log.LogLevel'              => 'FINE',
                'validation.level'          => 'log',
                'cache.enabled'             => 'true'
            ], $this->getConfig())
        );

        $payer = new Payer();
        $payer->setPaymentMethod("paypal");

        if(($subtotal = $total - $shipping - $tax) < 0) {
          throw new ErrorException('Subtotal is negative');
        }

        $item = new Item();
        $item->setName($productName)
            ->setCurrency($this->getCurrency())
            ->setQuantity(1)
            ->setPrice($subtotal);

        $itemList = new ItemList();
        $itemList->addItem($item);

        $details = new Details();
        $details->setShipping($shipping)
            ->setTax($tax)
            ->setSubtotal($subtotal);

        $amount = new Amount();
        $amount->setCurrency($this->getCurrency())
            ->setTotal($total)
            ->setDetails($details);

        $transaction = new Transaction();
        $transaction->setAmount($amount)
            ->setItemList($itemList)
            ->setDescription($transactionDescription)
            ->setInvoiceNumber(uniqid());

        $redirectUrls = new RedirectUrls();
        $redirectUrls->setReturnUrl(Url::home(true) . Url::to([$this->getReturnUrl()]))
            ->setCancelUrl(Url::home(true) . Url::to([$this->getCancelUrl()]));

        $payment = new Payment();
        $payment->setIntent($this->getIntentType())
            ->setPayer($payer)
            ->setRedirectUrls($redirectUrls)
            ->setTransactions(array($transaction));

        try {
            $payment->create($apiContext);
        } catch (Exception $ex) {

        }

        return $payment;
    }

    /**
     * @return mixed
     */
    public function getClientId()
    {
        return $this->clientId;
    }

    /**
     * @return mixed
     */
    public function getClientSecret()
    {
        return $this->clientSecret;
    }

    /**
     * @return mixed
     */
    public function getCurrency()
    {
        return $this->currency;
    }

    /**
     * @return mixed
     */
    public function getReturnUrl()
    {
        return $this->returnUrl;
    }

    /**
     * @return mixed
     */
    public function getCancelUrl()
    {
        return $this->cancelUrl;
    }

    /**
     * @return mixed
     */
    public function getIntentType()
    {
        return $this->intentType;
    }

    /**
     * @return mixed
     */
    public function getConfig()
    {
        return $this->config;
    }



}
+1
source

Source: https://habr.com/ru/post/1670483/


All Articles