使用PayPal结帐我的订单篮 [英] Checkout my order basket with PayPal

查看:128
本文介绍了使用PayPal结帐我的订单篮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,用户可以在购物篮中填写商品.

On my site user can fill their order basket with items.

完成后,他们可以单击结帐"按钮.

Once this is finished they can click the checkout button.

我希望他们使用PayPal结帐.

I want them to checkout using PayPal.

一旦用户单击结帐按钮,该用户就会被重定向到PayPal,并看到要付款的产品概述.

Once the user click on the checkout button the user is redirected to PayPal and sees an overview of the products to pay for.

如果用户执行了付款过程,则该用户将被重定向到我的成功页面.

If the user goes through the payment process the user is redirected to my success page.

但是我希望成功页面也会收到付款的交易ID,但是PayPal只发回令牌和付款人ID.

However I expect the success page to also receive the transaction id of the payment but paypal only sends back a token and a payerid.

我的结帐表格如下:

<form action="/en/checkout">
  <input type="submit" name="submit" value="Checkout">
</form>

我执行结帐的代码是:

function checkoutAction()
{
    $request = $this->getRequest();

    require_once(LIB_PATH.'/MFW/Paypal/Flows/Paypal_NVP.php');
    $paypal_nvp = new MFW_Paypal_NVP();

    // this should normally be filled by looping though the basket items
    $data = array('L_PAYMENTREQUEST_0_NAME0'=>'Single License',
                  'L_PAYMENTREQUEST_0_NUMBER0'=>'1111-2222-3333-4444-5555-6666-7777-8888',
                  'L_PAYMENTREQUEST_0_AMT0'=>39.99, // or enterprise 299.00
                  'L_PAYMENTREQUEST_0_QTY0'=>1,
                  );

    $_SESSION['Payment_Amount'] = 39.99;

    $result = $paypal_nvp->CallShortcutExpressCheckout(59.98, $data);

    $ack = strtoupper($result['ACK']);
    if($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING') {
        $paypal->RedirectToPayPal($result['TOKEN']);
        exit();
    }
}

Paypal_NCP类中的代码:

The code in the Paypal_NCP class:

function generate_nvp_string($total_value, $data = array())
{
    $params = array('PAYMENTREQUEST_0_AMT'=>$total_value,
                    'PAYMENTREQUEST_0_PAYMENTACTION'=>$this->payment_type,
                    'RETURNURL'=>$this->return_url,
                    'CANCELURL'=>$this->cancel_url,
                    'PAYMENTREQUEST_0_CURRENCYCODE'=>$this->currency,
                    );

    $params = array_merge($params, $data);

    $nvp_string = '';
    foreach($params as $name => $value) {
        $nvp_string.= '&'.$name.'='.$value;
    }

    // example string
    // &PAYMENTREQUEST_0_AMT=39.99&PAYMENTREQUEST_0_PAYMENTACTION=Sale&RETURNURL=http://return-address&CANCELURL=http://cancel-address&PAYMENTREQUEST_0_CURRENCYCODE=EUR&L_PAYMENTREQUEST_0_NAME0=Single License&L_PAYMENTREQUEST_0_NUMBER0=1111-2222-3333-4444-5555-6666-7777-8888&L_PAYMENTREQUEST_0_AMT0=39.99&L_PAYMENTREQUEST_0_QTY0=1

    return $nvp_string;
}

function CallShortcutExpressCheckout($total_value, $data = array())
{
    $_SESSION['currencyCodeType'] = $this->currency;
    $_SESSION['PaymentType'] = $this->payment_type;

    $result = $this->hash_call('SetExpressCheckout', $this->generate_nvp_string($total_value, $data));

    $ack = strtoupper($result['ACK']);
    if ($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING') {
        $_SESSION['TOKEN'] = urldecode($result['TOKEN']);
    }

    return $result;
}

那么我如何获取交易信息,以便能够在后台处理付款? (我需要一个交易ID)

So how do I get the information of the transaction for me to be able to process the payment in the backoffice? (I need an transaction ID for this)

推荐答案

您仅在调用SetExpressCheckout.为了通过Express Checkout完成交易,您还必须调用(可选)GetExpressCheckoutDetails以获取PayerID(买方的唯一标识符)和(必需)DoExpressCheckoutPayment.

You're only calling SetExpressCheckout. In order to finalize a transaction with Express Checkout, you must also call (optional) GetExpressCheckoutDetails to get the PayerID (a unique identifier of the buyer) and (required) DoExpressCheckoutPayment.

回顾:
要使用Express Checkout,您将调用SetExpressCheckout API.在API调用中,您可以指定产品,金额和RETURNURL的详细信息.这就是您在上面的代码中所做的.
将这些数据发布到PayPal的API端点后,您将收到一个令牌作为回报.然后,您将重定向买方,并将令牌附加到以下URL: https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXX

To recap:
To use Express Checkout, you would call the SetExpressCheckout API. In the API call, you specify the details of the products, amounts, and the RETURNURL. This is what you're doing in the code above.
Once you post this data to PayPal's API endpoint, you receive a token in return. You would then redirect the buyer, and append the token to the following URL: https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXX

一旦买方同意您的购买,他就会被重定向回您在RETURNURL中指定的URL.
现在,您应该显示订单确认,并调用GetExpressCheckoutDetails API **. 调用GetExpressCheckoutDetails时,请提供令牌.在GetExpressCheckoutDetails API响应中,您将找到PayerID.

Once the buyer has agreed to your purchase, he is redirected back to the URL you specified in the RETURNURL.
You should now show the order confirmation, and call the GetExpressCheckoutDetails API**. When calling GetExpressCheckoutDetails, supply the token. In the GetExpressCheckoutDetails API response you'll find a PayerID.

现在您可以致电DoExpressCheckoutPayment,并向买方收费.记住在调用DoExpressCheckoutPayment时同时包括令牌和payerID.

Now you're ready to call DoExpressCheckoutPayment, and charge the buyer. Remember to include both the token and the payerID when calling DoExpressCheckoutPayment.

关于IPN:您不再需要它了,因为您还将在对DoExpressCheckoutPayment的API响应中获得TransactionID. 如果您随后想要跟踪"交易,则IPN很有用.例如,在发生任何退款/拒付等情况时得到通知.
这只需要设置IPN脚本,并在SetExpressCheckout和DoExpressCheckoutPayment中都包含NOTIFYURL = http://....

With regards to IPN: You don't really need it anymore, as you'll also get the TransactionID back in the API response to DoExpressCheckoutPayment. IPN would be useful if you would subsequently want to 'keep track' of the transaction. E.g., get notified in case of any refunds / chargebacks, etc.
This simply requires setting up an IPN script and including NOTIFYURL=http://.... in both SetExpressCheckout and DoExpressCheckoutPayment.

** PayerID也会附加在您RETURNURL的GET中.因此,您可以根据需要跳过调用GetExpressCheckoutDetails的操作.

** The PayerID is appended in the GET of your RETURNURL as well. So you could skip calling GetExpressCheckoutDetails if you wanted to.

(我的答案的部分副本,位于为什么PayPal需要DoExpressCheckoutPayment ?)

(Partial copy of my answer at Why is DoExpressCheckoutPayment required for Paypal? )

这篇关于使用PayPal结帐我的订单篮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆