当支付模式为快速支付(按钮)时,IPN 和PDT都会起作用, 当以Rest Api方式创建支付时, PDT是无效的,它应该是针对网站的,IPN依然起作用。调用Rest Api 付款的步骤是这样的:
1. 注册paypal账号, 升级为高级或者企业账号;
2. 登录开发者中心后台,注册app;
3. 获取Client ID, Secret;
4. 设置ipn通知地址(profile里面设置);
5. 获取paypal SDK, 在sandbox环境测试, 支付返回的url是在调用代码里面设置的;
POST /v1/payments/payment
POST /v1/payments/payment/<Payment-Id>/execute
主要是这两个APi
6. 替换代码sandbox为正式url,上线。
需要注意的问题:
1.如果卖家退款, 资金会立刻结算,并向paypal支付一定的手续费,程序会收到ipn通知;
2.如果买家提出争议,争议类型是未经授权的交易时,资金会立刻冻结,程序会收到ipn通知;
3.如果买家提出争议,争议类型不是未经授权的交易时,资金会立刻冻结,程序不会收到ipn通知;
4. 买家和卖家的唯一交易号是完全不一致的;
5.买家和卖家的发票号(账单号)是一致的;
6.支付返回的url所在的程序,收到的数据不可信, 可以把一切本地的逻辑放在处理ipn的程序里面进行。ipn消息不是实时的,差不多秒级的延迟。
用户支付完成时,返回的url会收到如下请求数据:
‘success‘ => ‘true‘,
‘paymentId‘ => ‘PAY-1BV5911839643144YKSCUA7Q‘,
‘token‘ => ‘EC-8684120785130974D‘,
‘PayerID‘ => ‘JARYJK2TES6C6‘,
调用execute接口返回:
{ "id": "PAY-1BV5911839643144YKSCUA7Q", "create_time": "2014-12-08T06:09:02Z", "update_time": "2014-12-08T06:10:51Z", "state": "approved", "intent": "sale", "payer": { "payment_method": "paypal", "payer_info": { "email": "[email protected]", "first_name": "Test", "last_name": "Buyer", "payer_id": "JARYJK2TES6C6", "shipping_address": { "line1": "NO 1 Nan Jin Road", "city": "Shanghai", "state": "Shanghai", "postal_code": "200000", "country_code": "CN", "recipient_name": "Buyer Test" } } }, "transactions": [ { "amount": { "total": "20.00", "currency": "USD", "details": { "subtotal": "17.50", "tax": "1.30", "shipping": "1.20" } }, "description": "Payment description", "item_list": { "items": [ { "name": "Ground Coffee 40 oz", "price": "7.50", "currency": "USD", "quantity": "1" }, { "name": "Granola bars", "price": "2.00", "currency": "USD", "quantity": "5" } ], "shipping_address": { "recipient_name": "Buyer Test", "line1": "NO 1 Nan Jin Road", "city": "Shanghai", "state": "Shanghai", "postal_code": "200000", "country_code": "CN" } }, "related_resources": [ { "sale": { "id": "74V52932AB811673C", "create_time": "2014-12-08T06:09:02Z", "update_time": "2014-12-08T06:10:51Z", "amount": { "total": "20.00", "currency": "USD" }, "payment_mode": "INSTANT_TRANSFER", "state": "completed", "protection_eligibility": "ELIGIBLE", "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE", "parent_payment": "PAY-1BV5911839643144YKSCUA7Q", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/sale/74V52932AB811673C", "rel": "self", "method": "GET" }, { "href": "https://api.sandbox.paypal.com/v1/payments/sale/74V52932AB811673C/refund", "rel": "refund", "method": "POST" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-1BV5911839643144YKSCUA7Q", "rel": "parent_payment", "method": "GET" } ] } } ] } ], "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-1BV5911839643144YKSCUA7Q", "rel": "self", "method": "GET" } ] }
同时服务器会收到ipn消息, 类似:
array ( 'act' => 'ipn', 'mc_gross' => '20.00', 'invoice' => '548531d624f59', 'protection_eligibility' => 'Eligible', 'address_status' => 'unconfirmed', 'item_number1' => '', 'tax' => '1.30', 'item_number2' => '', 'payer_id' => 'JARYJK2TES6C6', 'address_street' => 'NO 1 Nan Jin Road', 'payment_date' => '21:04:35 Dec 07, 2014 PST', 'payment_status' => 'Completed', 'charset' => 'gb2312', 'address_zip' => '200000', 'mc_shipping' => '1.20', 'mc_handling' => '0.00', 'first_name' => 'Test', 'mc_fee' => '0.98', 'address_country_code' => 'CN', 'address_name' => 'Buyer Test', 'notify_version' => '3.8', 'custom' => '', 'payer_status' => 'unverified', 'address_country' => 'China', 'num_cart_items' => '2', 'mc_handling1' => '0.00', 'mc_handling2' => '0.00', 'address_city' => 'Shanghai', 'verify_sign' => 'AomRS5l2W2xlt2An.GaSrAzpCl-NACIvh3Pz0HtrSBZzfcIeqDPGrXSk', 'payer_email' => '[email protected]', 'mc_shipping1' => '0.00', 'mc_shipping2' => '0.00', 'tax1' => '0.00', 'tax2' => '0.00', 'txn_id' => '5CS19517SJ894934R', 'payment_type' => 'instant', 'last_name' => 'Buyer', 'address_state' => 'Shanghai', 'item_name1' => 'Ground Coffee 40 oz', 'receiver_email' => '[email protected]', 'item_name2' => 'Granola bars', 'payment_fee' => '0.98', 'quantity1' => '1', 'quantity2' => '5', 'receiver_id' => '937CP9PSMDS2A', 'txn_type' => 'cart', 'mc_gross_1' => '7.50', 'mc_currency' => 'USD', 'mc_gross_2' => '10.00', 'residence_country' => 'CN', 'test_ipn' => '1', 'transaction_subject' => '', 'payment_gross' => '20.00', 'ipn_track_id' => 'a9059421a1dd7', ) 2014-12-08 13:04:35
卖家退款收到的ipn:
array ( 'act' => 'ipn', 'mc_gross' => '-55.90', 'protection_eligibility' => 'Eligible', 'payer_id' => 'JARYJK2TES6C6', 'address_street' => 'NO 1 Nan Jin Road', 'payment_date' => '18:17:29 Dec 07, 2014 PST', 'payment_status' => 'Refunded', 'charset' => 'gb2312', 'address_zip' => '200000', 'first_name' => 'Test', 'mc_fee' => '-1.90', 'address_country_code' => 'CN', 'address_name' => 'Buyer Test', 'notify_version' => '3.8', 'reason_code' => 'refund', 'custom' => '', 'business' => '[email protected]', 'address_country' => 'China', 'address_city' => 'Shanghai', 'verify_sign' => 'AIkKNboJiyuxWLOHUlzTd3lpqCSxA1j2G80g.Gf68usTx7dykHwVOx4g', 'payer_email' => '[email protected]', 'parent_txn_id' => '78V79646KB938951Y', 'txn_id' => '03C86087BS569332H', 'payment_type' => 'instant', 'btn_id' => '3042078', 'last_name' => 'Buyer', 'address_state' => 'Shanghai', 'receiver_email' => '[email protected]', 'payment_fee' => '-1.90', 'shipping_discount' => '0.00', 'receiver_id' => '2XP27KEMUVN8A', 'insurance_amount' => '0.00', 'item_name' => 'sandbox_item111', 'discount' => '0.00', 'mc_currency' => 'USD', 'item_number' => '', 'residence_country' => 'CN', 'test_ipn' => '1', 'handling_amount' => '0.00', 'shipping_method' => 'Default', 'transaction_subject' => '', 'payment_gross' => '-55.90', 'shipping' => '10.00', 'ipn_track_id' => '2d16876d72c70', ) 2014-12-08 10:17:27
资金冻结时候收到的ipn:
array ( 'act' => 'ipn', 'mc_gross' => '-19.02', 'invoice' => '548541570184f', 'protection_eligibility' => 'Eligible', 'item_number1' => '', 'item_number2' => '', 'payer_id' => 'JARYJK2TES6C6', 'address_street' => 'NO 1 Nan Jin Road', 'payment_date' => '22:23:36 Dec 07, 2014 PST', 'payment_status' => 'Reversed', 'charset' => 'gb2312', 'address_zip' => '200000', 'mc_shipping' => '1.20', 'mc_handling' => '0.00', 'first_name' => 'Test', 'mc_fee' => '-0.98', 'address_country_code' => 'CN', 'address_name' => 'Buyer Test', 'notify_version' => '3.8', 'reason_code' => 'unauthorized_claim', 'custom' => '', 'address_country' => 'China', 'mc_handling1' => '0.00', 'mc_handling2' => '0.00', 'address_city' => 'Shanghai', 'verify_sign' => 'A.7n6Acd75CB8FdbeZyRGF.BoVPlArI2BzuxGNUFbCI-qvgcHJe4LgLp', 'payer_email' => '[email protected]', 'mc_shipping1' => '0.00', 'mc_shipping2' => '0.00', 'tax1' => '0.00', 'tax2' => '0.00', 'parent_txn_id' => '74V52932AB811673C', 'memo' => '14.13', 'txn_id' => '2Y80904055783821A', 'payment_type' => 'instant', 'last_name' => 'Buyer', 'address_state' => 'Shanghai', 'item_name1' => 'Ground Coffee 40 oz', 'receiver_email' => '[email protected]', 'item_name2' => 'Granola bars', 'payment_fee' => '-0.98', 'quantity1' => '1', 'quantity2' => '5', 'receiver_id' => '937CP9PSMDS2A', 'mc_gross_1' => '7.50', 'mc_currency' => 'USD', 'mc_gross_2' => '10.00', 'residence_country' => 'CN', 'test_ipn' => '1', 'transaction_subject' => '', 'payment_gross' => '-19.02', 'ipn_track_id' => '5df4cee26f6d3', ) 2014-12-08 14:23:33