pronamic/wp-pay-core

Create Payment Rest API

Closed this issue · 5 comments

Hello Team

I got requirements from some of our clients. They suggest adding the functionality of "Creat Payment Rest API". Using which client should be able to generate "Pay Redirect URL". Currently, Payment is getting created from WordPress Plugin/Extensions. So, they are looking for functionality to generate a "Pay Redirect URL" without using any WordPress plugin.

I can see there are already few APIs in WP Pay Core

  1. /wp/v2/pronamic-payments/(?P[\d]+)
  2. /pronamic-pay/v1/payments/(?P<payment_id>\d+)

Both these APIs support only GET (view functionality), I could not find and API to create Payment.

Am I missing something? Is this API already there? If it's currently not there, I would like to work on this development. Would you like to merge this with the main code if I will raise a pull request?

If you are interested, please suggest,
what features/functionality would you like to see in the first version of this API?
What route should I use

  1. /wp/v2/pronamic-payments/
  2. /pronamic-pay/v1/payments/
  3. /wp/v2/pronamic-payment/
  4. /pronamic-pay/v1/payment/
  5. or any other...
  1. /wp/v2/pronamic-payments/(?P[\d]+)

This endpoint arises from the custom post type pronamic_payment:

'show_in_rest' => true,
'rest_base' => 'pronamic-payments',


  1. /pronamic-pay/v1/payments/(?P<payment_id>\d+)

This is a custom endpoint which we currently mainly use for debugging purposes to view a payment in JSON format.

/**
* REST API init.
*
* @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
* @link https://developer.wordpress.org/reference/hooks/rest_api_init/
*
* @return void
*/
public function rest_api_init() {
\register_rest_route(
'pronamic-pay/v1',
'/payments/(?P<payment_id>\d+)',
[
'methods' => 'GET',
'callback' => [ $this, 'rest_api_payment' ],
'permission_callback' => function () {
return \current_user_can( 'edit_payments' );
},
'args' => [
'payment_id' => [
'description' => __( 'Payment ID.', 'pronamic_ideal' ),
'type' => 'integer',
],
],
]
);
}


If you want to create a custom endpoint for creating a payment, that's fine. For this we would not use the WordPress custom post type REST API endpoints (in this case: /wp/v2/pronamic-payments/…). We still use custom post types for storage in Pronamic Pay, but we don't want to be tied to that too much. A custom endpoint POST /knit-pay/v1/payments could be a good starting point. We try to follow the naming of the WordPress REST API endpoints, such as: https://developer.wordpress.org/rest-api/reference/posts/#create-a-post. A PR is always welcome, then the namespace knit-pay will have to become pronamic-pay, but this may also be configurable.

Thanks for the update. I will soon do the code and raise the pull request.

@remcotolsma

Development of the "Create Payment" REST API is almost complete and it is in the final bugfix testing phase. Could you please review the changes once before I raise the PR? If there is anything you would like to suggest, feel free to share, will make the changes accordingly.

Sample Curl request

curl --location 'https://dev.localdomain/wordpress-dev/wp-json/knit-pay/v1/payments/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic YWRtaW46djhaYSB5aVhLIEJ4bEsgbFg1ciA1WUpTIDdDVlY=' \
--data '{
    "source": {
        "key": "api",
        "value": "123213"
    },
    "order_id": "order-id-1",
    "total_amount": {
        "value": "51.43",
        "currency": "INR"
    },
    "description": "API Test Payment",
    "config_id": 714,
    "redirect_url": "https://webhook.site/0b89ec3f-890a-4b19-a3b9-d8a09c9d1995",
    "notify_url": "https://webhook.site/0b89ec3f-890a-4b19-a3b9-d8a09c9d1995",
    "payment_method": "upi",
    "customer": {
        "name": {
            "first_name": "First",
            "last_name": "Last"
        },
        "language": "en"
    }
}'

Rest Controller can be found in src/Payments/PaymentRestController.php file.
knit-pay@da48531#diff-1f9842517c4ad69e16e7b9596d883fe758ed0590ad25c13445aa736ac96ef53d

Please refer create_item and get_item function for Create Payment and Get Payment Logic.

Also, I think get_item_schema can be improved. Do you have any recommendations for the same?

I have defined redirect_url and notify_url. So that we can define custom Redirect URL and Webhook URL. Please suggest if you want me to change anything in this.

Wow, you've done a lot of work already. To get this merged, it would be nice if not too much was done in existing Pronamic Pay methods, but where possible to manage things via new classes and filters/actions hooks. We also wonder whether an entire JSON load should be posted, or whether individual parameters are preferable. More like for example Stripe:

curl https://api.stripe.com/v1/payment_intents \
  -u "sk_test_Gx4mWEgHtCMr4DYMUIqfIrsz:" \
  -d amount=1099 \
  -d currency=usd \
  -d "payment_method_types[]"=card

https://stripe.com/docs/payments/payment-intents

I'm not sure how exactly this works within the WordPress REST API, maybe both works automatically too?

I also provided some quick feedback in knit-pay@da48531#diff-1f9842517c4ad69e16e7b9596d883fe758ed0590ad25c13445aa736ac96ef53d.

You could also first set up this feature as a standalone plugin, which also forces you not to make adjustments to existing Pronamic Pay classes/methods.

Hi @remcotolsma

Thanks for the review.

Entire Json was just a sample Json, it was not mandatory to send entire json.
I have made minor change, now we can pass the parameter as form data also.

curl --location 'https://dev.localdomain/wordpress-dev/wp-json/knit-pay/v1/payments/' \
--header 'Authorization: Basic YWRtaW46djhaYSB5aVhLIEJ4bEsgbFg1ciA1WUpTIDdDVlY=' \
--form 'total_amount[value]="10"' \
--form 'total_amount[currency]="INR"'

If this code is added as a standalone plugin, it will create conflict with the existing code. For me, it will work, because my rest_base is different but for pronamic-pay, it will conflict.

https://github.com/pronamic/wp-pay-core/blob/e484e55c656cec8af3bd9a8ef34914549202cd2e/src/Payments/PaymentsModule.php#L236:L252