Migrating from API V1 to V2: How to getPaymentInstuctions after capture order? - paypal

I'm trying to migrate from API V1 to V2 in PHP. In V1, you could use this code to get further payment-instructions after executing a payment:
try {
$execution = new Paypal\Api\PaymentExecution();
$execution->setPayerId($sPayerId);
$payment = \Paypal\Api\Payment::get($sPaymentId, getApiContext());
$payment->execute($execution, getApiContext());
$payment = \Paypal\Api\Payment::get($sPaymentId, getApiContext()); // re-fetch payment with payment-instructions
if ($payment->getPaymentInstruction())
{ $sBankName = $payment->getPaymentInstruction()->recipient_banking_instruction->bank_name;
$sBandAccountNumber = $payment->getPaymentInstruction()->recipient_banking_instruction->international_bank_account_number;
...
}
}
catch(PayPal\Exception\PayPalConnectionException $ex) {
...
}
It's important for us to display the payment-instructions to our customer: If they use the "pay later"/"pay via invoice"-feature, they need to know to which bank and account to transfer the money to.
In API V2, you use this code to capture an authorized order:
$request = new PayPalCheckoutSdk\Orders\OrdersCaptureRequest($sOrderId);
try {
$response = getClient()->execute($request);
}
catch(PayPalHttp\HttpException $exception) {
....
}
So what are the next steps to get the payment-instructions now?

The field was not documented for the v1/payments API.
For v2/checkout/orders , a payment_instruction field is documented as an order creation request field (within purchase_units) and response field.
A similar field exists when capturing an authorization.
For Pay Upon Invoice, there is a processing_instruction field.
In all these cases, the fields do not contain information regarding "which bank and account to transfer the money to" or similar.
However, for PUI there is a response field payment_source.pay_upon_invoice.deposit_bank_details . Perhaps this is what you are looking for.
{
"id": "5O190127TN364715T",
"intent": "CAPTURE",
"status": "COMPLETED",
"processing_instruction": "ORDER_COMPLETE_ON_PAYMENT_APPROVAL",
"payment_source": {
"pay_upon_invoice": {
"birth_date": "1990-01-01",
"name": {
"given_name": "John",
"surname": "Doe"
},
"email": "buyer#example.com",
"phone": {
"national_number": "6912345678",
"country_code": "49"
},
"billing_address": {
"address_line_1": "Schönhauser Allee 84",
"admin_area_2": "Berlin",
"postal_code": "10439",
"country_code": "DE"
},
"payment_reference": "b8a1525dlYzu6Mn62umI",
"deposit_bank_details": {
"bic": "DEUTDEFFXXX",
"bank_name": "Deutsche Bank",
"iban": "DE89370400440532013000",
"account_holder_name": "Paypal - Ratepay GmbH - Test Bank Account"
}
}
},
"purchase_units": [
....
],
"links": [
....
]
}

Related

PayPal Invoicing API - VALIDATION_ERROR

I am developing an application that communicates with PayPal's API to create invoices.
This is my Request Body:
{
"detail": {
"currency_code": "USD",
"note": "Thank you for using my services!"
},
"invoicer": {
"name": {
"given_name": "Shreyas",
"surname": "Ayyengar"
},
"email_address": "{email}",
"website": "{website}"
},
"primary_recipients": [
{
"billing_info": {
"email_address": "{client_email}"
}
}
],
"items": [
{
"name": "{invoice_name}",
"description": "{invoice_description}",
"quantity": "1",
"unit_amount": {
"currency_code": "USD",
"value": "{invoice_amount}"
},
"tax": {
"name": "PayPal Service Tax",
"percent": "7.25"
}
}
],
"configuration": {
"partial_payment": {
"allow_partial_payment": false
},
"allow_tip": true
}
}
While there are placeholders like: {client_email}, I can guarantee that they are replacing properly and as expected.
However I'm thrown a VALIDATION_ERROR which I am not able to understand: {"name":"VALIDATION_ERROR","message":"Invalid request - see details.","information_link":"https://developer.paypal.com/docs/api/invoicing/#errors","details":[{"field":"merchant_info","issue":"cannot be null."},{"field":"items[0].unit_price","issue":"null"}]}
From what I can minimally understand, this error says that I have missing information like Items[].unit_price and merchant_info however I have no idea where this is supposed to be in my Request Body. I am following the direct documentationhere but I cannot see anything that mentions unit_price or merchant_info.
Submit your request to the correct API endpoint, https://api-m.sandbox.paypal.com/v2/invoicing/invoices
Note the major version number. See the Invoicing API reference for details.

Paypal API/ agreement page / hide shipping address block

I have a digital product and therefore want to hide the shipping block.
I use Paypal API CURL Create agreement
My CURL post data:
{
"name": "user#user.com Agreement",
"description": "Essential 1 Plan - 19$ trial 7 days - 1$",
"start_date": "2021-11-11T00:52:01Z",
"payer": {
"payment_method": "paypal"
},
"plan": {
"id": "P-XXXXXXXXXXXXXXXXXXXXXXXX"
},
"override_merchant_preferences": {
"setup_fee": {
"value": 0,
"currency": "USD"
},
"return_url": "http://domain.local/membership/checkout/success",
"cancel_url": "http://domain.local/membership/checkout/cancel",
"auto_bill_amount": "YES",
"initial_fail_amount_action": "CONTINUE",
"max_fail_attempts": 2
}
}
I read that I need to set the
no_shipping => 1
parameter, but I don't know where.
Please help find a solution.

How to create a Subscription payment using Paypal client-side rest api?

The Client-side REST integration documentaion describes about creating a express checkout for one or more items.
How can i use the same for creating a subscription or Recurring payment? How should the following be modified?
payment: function(data, actions) {
return actions.payment.create({
transactions: [
{
amount: { total: '1.00', currency: 'USD' }
}
]
});
},
I found a similar Rest api for Node. Not sure how it would be on JS.
First you need to create a billing plan:
billing_plan_attributes = {
"name": PLAN_NAME_HERE,
"description": PLAN_DESCRIPTION,
"merchant_preferences": {
"auto_bill_amount": "yes", # yes if you want auto bill
"cancel_url": "http://www.cancel.com", # redirect uri if user cancels payment
"initial_fail_amount_action": "continue",
"max_fail_attempts": "1",
"return_url": RETURN_URL,
"setup_fee": {
"currency": CURRENCY,
"value": VALUE # how much do you want to charge
}
},
"payment_definitions": [
{
"amount": {
"currency": request.form['currency'],
"value": request.form['amount']
},
"cycles": CYCLES, # how much time this subscription will charge user
"frequency": FREQ, # month, day
"frequency_interval": INTERVAL, # per month or per three month or so on
"name": NAME,
"type": TYPE
}
],
"type": TYPE
}
billing_plan = BillingPlan(billing_plan_attributes)
if billing_plan.create():
print("success")
The attributes used have literal meaning here. Now since you have created a billing plan you need to give users some interface so that they can subscribe with it. Below is a sample code for this:
billing_agreement = BillingAgreement({
"name": "Organization plan name",
"description": "Agreement for " + request.args.get('name', ''),
"start_date": (datetime.now() + timedelta(hours=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
"plan": {
"id": request.args.get('id', '')
},
"payer": {
"payment_method": "paypal"
},
"shipping_address": {
"line1": "StayBr111idge Suites",
"line2": "Cro12ok Street",
"city": "San Jose",
"state": "CA",
"postal_code": "95112",
"country_code": "US"
}
})
if billing_agreement.create():
for link in billing_agreement.links:
if link.rel == "approval_url":
approval_url = link.href
In the last line you get the approval link which can be given to user.
Next you have to setup an endpoint which will be the callback url if user approves the payment.
billing_agreement_response = BillingAgreement.execute(payment_token)
payment_token is sent by paypal to your callback url.

Record saves, promise rejects with custom REST adapter

I'm writing an ember-data adapter for the DreamFactory services platform and am running into an issue I think is related to my adapter.
When updating an existing record the promise resulting from model.save() is ALWAYS rejected with an error of
Assertion Failed: An adapter cannot assign a new id to a record that already has an id. <App.Event311:1> had id: 1 and you tried to update it with null. This likely happened because your server returned data in response to a find or update that had a different id than the one you sent
Thing is - the request to the REST API and the response back from the REST API have the same ID!
Request (PUT)
{
"record": {
"id": "1",
"title": "Sample Event",
"date": "7/20/2013",
"type": "success",
"desc": "My first sample event."
}
}
Response
{
"record": [
{
"id": 1,
"title": "Sample Event",
"date": "7/20/2013",
"type": "success",
"desc": "My first sample event."
}
]
}
The really weird thing is the record still updates properly both in the store AND in the database!
I have a working JSBin at http://emberjs.jsbin.com/mosek/1/edit that illustrates the problem. My custom adapter is on GitHub at https://github.com/ultimatemonty/ember-data-dreamfactory-adapter. The JSBin as well as my app are using Ember 1.7.0 and ED 1.0.0-beta.9
EDIT
The JSBin is attached to my personal hosted instance of DreamFactory - I haven't done anything with it outside of allowing access from JSBin but please be gentle :)
* EDIT #2 *
The updateRecord code is accessible on GitHub at https://github.com/ultimatemonty/ember-data-dreamfactory-adapter/blob/master/lib/ember-data-dreamfactory-adapter.js#L106 but here is the full method for reference:
updateRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.typeKey);
serializer.serializeIntoHash(data, type, record);
var adapter = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
// hack to make DSP send back the full object
adapter.ajax(adapter.buildURL(type.typeKey) + '?fields=*', "PUT", { data: data }).then(function(json){
// if the request is a success we'll return the same data we passed in
resolve(json);
}, function(reason){
reject(reason.responseJSON);
});
});
}
The adapter/serializer you're using is expecting you to return a response without the type in it:
{
"id": 1,
"title": "Sample Event",
"date": "7/20/2013",
"type": "success",
"desc": "My first sample event."
}
Example: http://emberjs.jsbin.com/tigiza/1/edit
You can see it here in the extractSingle, where it tries to wrap the payload in in another object with the type specified
EmberDreamFactoryAdapter.Serializer = DS.RESTSerializer.extend({
extractArray: function(store, primaryType, payload) {
var namespacedPayload = {};
namespacedPayload[Ember.String.pluralize(primaryType.typeKey)] = payload.record;
return this._super(store, primaryType, namespacedPayload);
},
extractSingle: function (store, primaryType, payload, recordId) {
var namespacedPayload = {};
namespacedPayload[primaryType.typeKey] = payload;
return this._super(store, primaryType, namespacedPayload, recordId);
},
Your response looks like this:
{
"record": [
{
"id": 1,
"title": "Sample Event",
"date": "7/20/2013",
"type": "success",
"desc": "My first sample event."
}
]
}
Then the serializer kicks in, and it looks like this:
{
event:{
"record": [
{
"id": 1,
"title": "Sample Event",
"date": "7/20/2013",
"type": "success",
"desc": "My first sample event."
}
]
}
}
When really, the serializer should have it looking like this:
{
event:{
"id": 1,
"title": "Sample Event",
"date": "7/20/2013",
"type": "success",
"desc": "My first sample event."
}
}
You can see from the second example, the serializer wraps it in the type, then Ember Data says, hey, give me the id, so it looks at event.id which is undefined, because it lives under event.record[0].id

PayPal REST API: how to do an immediate payment and without asking for shipping address

I'm trying to use PayPal REST API instead of PayPal Classic API but it seems that the REST API is lacking two features that the Classic API has:
immediate payment: when the user goes to PayPal page show him a "Pay now" button instead of a "Continue" button and "You’re almost done. You will confirm your payment on ..." phrase.
no shipping address: avoid asking the user to confirm his shipping address while on PayPal page (in Classic API is done with NOSHIPPING=1 parameter, if I remember well)
So my question is: is it possibile do perform an immediate payment without asking for shipping address using REST API? Do I have to go back to Classic API?
I provide here a little more informations about how I'm using the PayPal REST API.
I'm using the PayPal REST Java SDK.
This is a sample request:
{
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"transactions": [
{
"amount": {
"currency": "USD",
"total": "5",
"details": {
"subtotal": "5"
}
},
"description": "This is the payment transaction description.",
"item_list": {
"items": [
{
"quantity": "1",
"name": "Item 1",
"price": "5",
"currency": "USD"
}
]
}
}
],
"redirect_urls": {
"return_url": "http://XXX/handlePayment.jsp?guid\u003dXXX",
"cancel_url": "http://XXX/cancelPayment.jsp?guid\u003dXXX"
}
}
And its response:
{
"id": "XXX",
"create_time": "2014-06-29T08:52:55Z",
"update_time": "2014-06-29T08:52:55Z",
"state": "created",
"intent": "sale",
"payer": {
"payment_method": "paypal",
"payer_info": {
"shipping_address": {}
}
},
"transactions": [
{
"amount": {
"total": "5.00",
"currency": "USD",
"details": {
"subtotal": "5.00"
}
},
"description": "This is the payment transaction description.",
"item_list": {
"items": [
{
"name": "Item 1",
"price": "5.00",
"currency": "USD",
"quantity": "1"
}
]
}
}
],
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/XXX",
"rel": "self",
"method": "GET"
},
{
"href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=XXX",
"rel": "approval_url",
"method": "REDIRECT"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/XXX/execute",
"rel": "execute",
"method": "POST"
}
]
}
While delving through the REST API I came across this
I believe this means you don't have to go about creating any "Profiles" as such, and can just pass them along with the payment call...
Further explanation as requested :)
Below is an example of passing PayPal experience parameters along with a particular payment call using the Client-side JS method for Express checkout.
payment: function(data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '1.00', currency: 'USD' }
}
]
},
experience: {
input_fields: {
no_shipping: 1
}
}
});
},
Hope that makes enough sense to you guys! :)
Unfortunately, the REST API is still a long ways behind the Classic API with features it provides. These features you mentioned are things I've seen come up quite a bit, and to my knowledge they are not yet available with the REST services.
I personally have stuck with Classic as they provide everything and I really see no advantage to using REST myself. If you happen to be working with PHP (which I always do) you may be interested in my class library for PayPal. It makes all of the Classic API calls very quick and easy for you, and it's available on Packagist so you can use with Composer easily.
The REST API now supports no-shipping with the Payment Experience APIs.
You need to create a web experience profile and supply no_shipping as an input field. Then use the profile ID when creating the payment.
The profile ID doesn't need to be recreated every time.
appreciate that this post hasn't had any activity for a while but...
i hit on the exact same problem and used this post as a start point for my own question:
paypal api: take immediate payment without a shipping address
it's taken a bit of trial and error but i you can create a one off web profile with 'no_shipping' set to 1, store the id that it creates and then pass that in with future payments that don't require a shipping address.
still haven't figured out how to get rid of the 'You're almost done. You will confirm your payment on xxx.' but my payment process is a far better place than it was!
hope this helps someone out there as this no shipping issue is one that a lot of people appear to be hitting with the paypal api...
I tried using the experience section of the API and apart from still not being able to force a locale code have been able to disable shipping and go straight to billing:
payment: {
transactions: [{
invoice_number: document.getElementById("ReqTxt").value,
amount: {
total: document.getElementById("InvoiceAmt").innerText,
currency: document.getElementById("Currency").innerText
}
}]
},
experience: {
input_fields: {
no_shipping: 1
},
flow_config: {
landing_page_type:'billing'
}
}
you can also see this page. It is possible to set the user action in the flow_config section too
For 'Pay Now' instead of 'Continue' on Paypal I use a special param in approval url:
$approvalUrl = $payment->getApprovalLink().'&useraction=commit';
Result:
https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=TOKEN&useraction=commit