javascript 集成 PayPal 快速结账的更简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15626831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:27:10  来源:igfitidea点击:

Easier way to integrate PayPal express checkout?

phpjavascriptwordpresscurlpaypal

提问by Jon

I have built a product generation and display plugin for the Wordpress CMS and I am now trying to integrate some form of PayPal integration for the checkout process.

我已经为 Wordpress CMS 构建了一个产品生成和显示插件,现在我正在尝试为结帐流程集成某种形式的 PayPal 集成。

I have the cart, the products, the shipping, totals, all that figured out on my end and I was hoping someone could point me in the simplest direction of sending this information to PayPal. I understand some methods of doing this are not that secure and others make you jump through hoops like some sort of show dog. I've been trying to learn how to use cURL and then how to get it to work with PHP - it really seems like a bit of a mess. I do now have cURL working on my WAMP server ... but..

我有购物车、产品、运输、总计,所有这些都是我想出来的,我希望有人能指出我将这些信息发送到 PayPal 的最简单的方向。我知道一些这样做的方法并不那么安全,而另一些方法会让你像某种表演犬一样跳过箍。我一直在尝试学习如何使用 cURL,然后如何让它与 PHP 一起工作——这看起来真的有点混乱。我现在有 cURL 在我的 WAMP 服务器上工作......但是......

Is there a better way or should I continue to learn cURL?

有没有更好的方法还是我应该继续学习cURL?

I can format the data however it needs to be to send off to PayPal and would not mind doing this with JavaScript - this is not a pay-wall and every order is checked for accuracy by a human - so someone messing with the client-side script will not bother me. I also definitely want to send them to PayPal, I want no part of storing/processing their credit card information. It would, however, be nice to have IPN. Can someone point me in the right direction or assure me that I already am headed that way?

我可以格式化数据,但需要将数据发送到 PayPal,并且不介意使用 JavaScript 执行此操作-这不是付费墙,每个订单都由人工检查其准确性-因此有人与客户端搞混了脚本不会打扰我。我也绝对想将它们发送到 PayPal,我不想存储/处理他们的信用卡信息。但是,拥有 IPN 会很好。有人能指出我正确的方向或向我保证我已经朝着那个方向前进了吗?

Thanks alot.

非常感谢。

回答by Harry Beasant

This is how i automatically redirect to PayPal with all the form details;

这就是我如何使用所有表单详细信息自动重定向到 PayPal;

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="paypal">

 <input type="hidden" name="cmd" value="_xclick" />
  <input type="hidden" name="cbt" value="Return to example" />
 <input type="hidden" name="business" value="email" />
 <input type="hidden" name="item_name" value="example Purchase" />
 <input type="hidden" name="amount" value="9.99">
 <input type="hidden" name="button_subtype" value="services" />
 <input type="hidden" name="no_shipping" value="1">
 <input type="hidden" name="return" value="URL" />
 <input type="hidden" name="notify_url" value="URL"/>
 <input type="hidden" name="cancel_return" value="URL" />
<input type="hidden" name="currency_code" value="USD"/>
<input type="hidden" name="image_url" value="" />
<input type="hidden" id="custom" name="custom" value="invoice_id to track"/>
 <input type="hidden" class="btn btn-primary" style="width:100%" alt="PayPal - The safer, easier way to pay online!"/>

</form>

For multiple products, you can simply add more products to the form, example;

对于多个产品,您可以简单地在表单中添加更多产品,例如;

<input type="hidden" name="item_name_1" value="Item #1">
<input type="hidden" name="amount_1" value="1.00">
<input type="hidden" name="item_name_2" value="Item #2">
<input type="hidden" name="amount_2" value="2.00">

However, using this method is not all great

然而,使用这种方法并不是很好

All the data would need to be generated with PHP and input into the page, you would also need to check the transaction when the IPN calls back to ensure its been paid.

所有数据都需要用 PHP 生成并输入到页面中,您还需要在 IPN 回调时检查交易以确保其已支付。

<script type="text/javascript">
  function myfunc () {
     var frm = document.getElementById("paypal");
     frm.submit();
  }
  window.onload = myfunc;
</script>

回答by Ken Johnson

You may want to use the new PayPal SDK. They have a good set of sample code, including code for express checkout and IPN. Try here https://www.x.com/developers/paypal/documentation-tools/paypal-sdk-indexGet the SDK for Express checkout. At this time, they should be at SDK 98 for PHP.

您可能想要使用新的 PayPal SDK。他们有一套很好的示例代码,包括快速结账和 IPN 的代码。在此处尝试 https://www.x.com/developers/paypal/documentation-tools/paypal-sdk-index获取用于快速结帐的 SDK。此时,它们应该在 SDK 98 for PHP 上。

You won't have to worry about the Curl, the SDK takes care of all that for you. A typical call might be something like this.

您不必担心 Curl,SDK 会为您处理所有这些。典型的呼叫可能是这样的。

$setECResponse = $paypalService->SetExpressCheckout($setECReq);

This line of code is modeled after the samples. It's all object oriented. They provide you with classes. In this case there is a request object you fill out, the examples show exactly how to do it; just use the samples as your template.

这行代码是在示例之后建模的。都是面向对象的。他们为您提供课程。在这种情况下,您填写了一个请求对象,示例显示了如何做到这一点;只需使用示例作为您的模板。

It sounds like you want to do PayPal Express checkout, this way you won't have to handle credit cards or anything like that. The user is redirected to the PayPal website and all the financial transactions happen there. The user is redirected back to your site. Then you have a page where the user can review the order and click submit if they approve. When the user clicks submit, you call a PayPal API telling PayPal that the transaction is approved. PayPal then executes the transaction and sends you back a confirmation with a transaction id. You can then call getTransactionDetails and display the confirmation to the customer. You can additionally put those transaction details into a database.

听起来您想要进行 PayPal Express 结账,这样您就不必处理信用卡或类似的事情。用户被重定向到 PayPal 网站,所有金融交易都在那里进行。用户被重定向回您的网站。然后您有一个页面,用户可以在其中查看订单并在他们批准后单击提交。当用户单击提交时,您将调用 PayPal API,告诉 PayPal 交易已获批准。然后 PayPal 执行交易并向您发送带有交易 ID 的确认信息。然后您可以调用 getTransactionDetails 并向客户显示确认信息。您还可以将这些交易详细信息放入数据库中。

Here are the APIs you can call for this. These are modeled closely to the sample code they provide

以下是您可以为此调用的 API。这些模型与它们提供的示例代码密切相关

$paypalService->SetExpressCheckout($setECReq);

control goes to PayPal URL, and the user goes through a few pages there. control returns to you.

控制转到 PayPal URL,用户在那里浏览几页。控制权还给你。

your order review page $paypalService->GetExpressCheckoutDetails($getExpressCheckoutReq);

您的订单审核页面 $paypalService->GetExpressCheckoutDetails($getExpressCheckoutReq);

your order confirmation page

您的订单确认页面

$paypalService->GetExpressCheckoutDetails($getECReq);

$paypalService->DoExpressCheckoutPayment($DoECReq);

Tells PayPal to do the transaction.

告诉 PayPal 进行交易。

$paypalService->GetTransactionDetails($request);

Here you can put transaction details into a database. You can also send yourself a mail with all the details, that way you will know whenever a transaction occurs.

您可以在此处将交易详细信息放入数据库。您还可以向自己发送一封包含所有详细信息的邮件,这样您就可以知道何时发生交易。

IPN can be a bit tricky. There is a sample IPN listener that they provide, that will help. You will need to set up your listener URL on the PayPal website. You will also need to set up an SSL certificate.

IPN 可能有点棘手。他们提供了一个示例 IPN 侦听器,这将有所帮助。您需要在 PayPal 网站上设置您的侦听器 URL。您还需要设置 SSL 证书。

The SDKs are fairly new, but PayPal is working on an even newer way to do things, developer.paypal.com. It just came out within the last month or so. You may want to look into that too.

SDK 是相当新的,但 PayPal 正在开发一种更新的方法,developer.paypal.com。它刚刚在上个月左右发布。你可能也想研究一下。