最近将一个名为 GChecky 的项目移植到了 Google App Engine 上,这个项目实现了 1 级(购物车提交)和 2 级(Google Checkout 通知)两种支付方式。我们需要了解在 Google App Engine 上是否还有其他支持 2 级(通知)的支付选项,例如 PayPal。
2、解决方案
方法一:使用 PayPalX GAE 工具箱
PayPalX GAE 工具箱是 PayPal 官方提供的工具箱,它可以帮助开发者在 Google App Engine 上轻松集成 PayPal 支付功能。这个工具箱提供了多种功能,包括:
- 支持多种支付方式,包括信用卡、借记卡、PayPal 账户等;
- 支持 1 级和 2 级支付;
- 提供易于使用的 API,可以帮助开发者快速集成 PayPal 支付功能;
PayPalX GAE 工具箱的代码示例:
from paypal.standard.ipn.models import PayPalIPN
from paypal.standard.ipn.views import PayPalIPNView
def payment_confirmation(request):
ipn = PayPalIPN(request)
if ipn.form.is_valid():
# Check that the payment was successful
if ipn.form.was_successful():
# Get the payment details
amount = ipn.form.get('mc_gross')
currency = ipn.form.get('mc_currency')
transaction_id = ipn.form.get('txn_id')
# Process the payment
# ...
return HttpResponse('OK')
方法二:使用 PayPal SOAP 接口
PayPal 还有一个 SOAP 接口,开发者也可以使用这个接口来在 Google App Engine 上集成 PayPal 支付功能。不过,使用 SOAP 接口需要开发者具备一定的编程经验,并且需要注意超时问题。
PayPal SOAP 接口的代码示例:
import suds
wsdl_url = 'https://api-3t.sandbox.paypal.com/2.0/wsdl/PayPalSvc.wsdl'
client = suds.client.Client(wsdl_url)
# Make a payment
payment_details = {
'Amount': {
'currencyID': 'USD',
'value': '10.00'
},
'ReceiverOptions': {
'Receiver': {
'email': 'developer@example.com'
}
}
}
response = client.service.DoDirectPayment(
payment_details,
'your-api-username',
'your-api-password',
'your-api-signature',
'your-application-id'
)
# Check the response
if response.Ack == 'Success':
# Process the payment
# ...
方法三:使用 Django 集成 PayPal IPN
如果使用 Django 开发 Google App Engine 应用,还可以使用 Django PayPal IPN 集成库来轻松集成 PayPal 支付功能。
Django PayPal IPN 集成库的代码示例:
from django.conf import settings
from paypal.standard.ipn.forms import PayPalIPNForm
from paypal.standard.ipn.signals import payment_was_successful
def payment_confirmation(request):
form = PayPalIPNForm(request.POST)
if form.is_valid():
# Check that the payment was successful
if form.payment_status == 'Completed':
# Get the payment details
amount = form.get('mc_gross')
currency = form.get('mc_currency')
transaction_id = form.get('txn_id')
# Process the payment
# ...
return HttpResponse('OK')
# Connect the payment_was_successful signal to your payment processing function
payment_was_successful.connect(process_payment)
无论选择哪种方法,在 Google App Engine 上集成 PayPal 支付功能都是可行的。开发者可以根据自己的需求和技术实力选择合适的方法。