pronamic / wp-pay-core

Core components for the WordPress payment processing library. This library is used in the WordPress plugin Pronamic Pay: https://www.pronamicpay.com/, but also allows other plugin developers to set up a payment plugin.
https://www.wp-pay.org/
GNU General Public License v3.0
27 stars 3 forks source link

Create Payment Rest API #168

Closed knit-pay closed 8 months ago

knit-pay commented 8 months ago

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\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...
remcotolsma commented 8 months ago
  1. /wp/v2/pronamic-payments/(?P[\d]+)

This endpoint arises from the custom post type pronamic_payment:

https://github.com/pronamic/wp-pay-core/blob/c743a1506d01274286f51d23debf765004b17d6f/src/Payments/PaymentPostType.php#L84-L85


  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.

https://github.com/pronamic/wp-pay-core/blob/c743a1506d01274286f51d23debf765004b17d6f/src/Payments/PaymentsModule.php#L227-L253


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.

knit-pay commented 8 months ago

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

knit-pay commented 8 months ago

@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. https://github.com/knit-pay/wp-pay-core/commit/da48531f69a7b4040b900bf057932572ca464415#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.

remcotolsma commented 7 months ago

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 https://github.com/knit-pay/wp-pay-core/commit/da48531f69a7b4040b900bf057932572ca464415#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.

knit-pay commented 7 months ago

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