Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Shop API extensions #5

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pretty-hairs-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vendure-mpesa-plugin": patch
---

add shop api extensions to check and verify payment status
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export const config: VendureConfig = {

3. Calling the `addPaymentToOrder` mutation on the storefront with the corresponing payment method code will initiate an STK push to the customer's phone.

4. Call the `verifyMpesaTransaction` mutation periodically on the storefront to verify the transaction status.

## Reference

- [Mpesa Daraja API Docs](https://developer.safaricom.co.ke/Documentation)
7 changes: 7 additions & 0 deletions src/api/api-extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import gql from "graphql-tag"

export const shopApiExtensions = gql`
extend type Mutation {
verifyMpesaTransaction(transactionId: String!): Boolean!
}
`
21 changes: 21 additions & 0 deletions src/api/mpesa-shop.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Args, Mutation, Resolver } from "@nestjs/graphql"
import { Ctx, RequestContext } from "@vendure/core"

import { MpesaService } from "../service/mpesa.service"

@Resolver()
export class MpesaShopResolver {
constructor(private readonly mpesaService: MpesaService) {}

@Mutation()
async verifyMpesaTransaction(
@Ctx() ctx: RequestContext,
@Args() args: { transactionId: string }
): Promise<boolean> {
const isSuccessful = await this.mpesaService.verifyMpesaPayment(
ctx,
args.transactionId
)
return isSuccessful
}
}
6 changes: 6 additions & 0 deletions src/mpesa.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { PluginCommonModule, VendurePlugin } from "@vendure/core"

import { shopApiExtensions } from "./api/api-extensions"
import { CallbackWebhookController } from "./api/callback-webhook.controller"
import { MpesaShopResolver } from "./api/mpesa-shop.resolver"
import { mpesaEligibilityChecker } from "./config/mpesa-eligibility-checker"
import { mpesaPaymentMethodHandler } from "./config/mpesa.handler"
import { MPESA_PLUGIN_INIT_OPTIONS } from "./constants"
Expand Down Expand Up @@ -68,6 +70,10 @@ export interface MpesaPluginOptions {
)
return config
},
shopApiExtensions: {
schema: shopApiExtensions,
resolvers: [MpesaShopResolver]
},
providers: [
{
provide: MPESA_PLUGIN_INIT_OPTIONS,
Expand Down
10 changes: 10 additions & 0 deletions src/service/mpesa.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ export class MpesaService {
}
}

async verifyMpesaPayment(ctx: RequestContext, transactionId: string) {
const payment = await this.getPaymentByTransactionId(ctx, transactionId)

if (!payment) {
return false
}

return payment.state === "Settled"
}

async settlePayment(ctx: RequestContext, transactionId: string) {
const isTransactionSuccessful =
await this.checkTransactionStatus(transactionId)
Expand Down
Loading