3

我正在尝试在 Symfony 下的网页上集成 paypal 的支付系统。经过一些研究,我遇到了 Payum,它显然是该功能的最佳捆绑包。问题是我不太了解文档,所以我拥有的最终代码不起作用。有人已经使用过 payum 并且可以帮助我理解它吗?

我有例如: $paypalRestPaymentDetailsClass 它来自无处,我不知道这个类应该是什么

这是我的代码:

namespace PlatformBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Payum\Core\PayumBuilder;
use Payum\Core\Payum;
use PayPal\Api\Amount;
use PayPal\Api\Payer;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use Payum\Core\Request\GetHumanStatus;

class PayumController extends Controller
{
    private function config()
    {
        return (new PayumBuilder())
            ->addDefaultStorages()
            ->addGateway('gatewayName', [
                'factory'       => 'paypal_rest',
                'client_id'     => 'REPLACE IT',
                'client_secret' => 'REPLACE IT',
                'config_path'   => 'REPLACE IT',
            ])
            ->getPayum();
    }

    public function prepare()
    {
        $payum = $this->config();

        $storage = $payum->getStorage($paypalRestPaymentDetailsClass);

        $payment = $storage->create();
        $storage->update($payment);

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

        $amount = new Amount();
        $amount->currency = "USD";
        $amount->total = "1.00";

        $transaction = new Transaction();
        $transaction->amount = $amount;
        $transaction->description = "This is the payment description.";

        $captureToken = $payum->getTokenFactory()->createCaptureToken('paypalRest', $payment, 'create_recurring_payment.php');

        $redirectUrls = new RedirectUrls();
        $redirectUrls->return_url = $captureToken->getTargetUrl();
        $redirectUrls->cancel_url = $captureToken->getTargetUrl();

        $payment->intent = "sale";
        $payment->payer = $payer;
        $payment->redirect_urls = $redirectUrls;
        $payment->transactions = array($transaction);

        $storage->update($payment);

        header("Location: ".$captureToken->getTargetUrl());
    }

    public function capture()
    {
        $payum = $this->config();

        $token = $payum->getHttpRequestVerifier()->verify($_REQUEST);
        $gateway = $payum->getGateway($token->getGatewayName());


        if ($reply = $gateway->execute(new Capture($token), true)) {
            if ($reply instanceof HttpRedirect) {
                header("Location: ".$reply->getUrl());
                die();
            }

            throw new \LogicException('Unsupported reply', null, $reply);
        }

        $payum->getHttpRequestVerifier()->invalidate($token);

        header("Location: ".$token->getAfterUrl());
    }

    public function done()
    {

        $payum = $this->config();

        $token = $payum->getHttpRequestVerifier()->verify($_REQUEST);
        $gateway = $payum->getGateway($token->getGatewayName());

        // you can invalidate the token. The url could not be requested any more.
        // $payum->getHttpRequestVerifier()->invalidate($token);

        // Once you have token you can get the model from the storage directly.
        //$identity = $token->getDetails();
        //$payment = $payum->getStorage($identity->getClass())->find($identity);

        // or Payum can fetch the model for you while executing a request (Preferred).
        $gateway->execute($status = new GetHumanStatus($token));
        $payment = $status->getFirstModel();

        header('Content-Type: application/json');
        echo json_encode(array(
            'status' => $status->getValue(),
            'order' => array(
                'total_amount' => $payment->getTotalAmount(),
                'currency_code' => $payment->getCurrencyCode(),
                'details' => $payment->getDetails(),
            ),
        ));
    }
}

谢谢

4

0 回答 0